Skip to content

Commit

Permalink
- Safer usage of Object.hasOwnProperty()
Browse files Browse the repository at this point in the history
- Minor code formatting/cleanup/refactoring
  • Loading branch information
sebdeckers committed Oct 15, 2011
1 parent 24e90a4 commit 51375d4
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 28 deletions.
13 changes: 7 additions & 6 deletions core/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@
var counters = {};
return {
increment: function (name) {
if (counters.hasOwnProperty(name)) {
if (Object.hasOwnProperty.call(counters, name)) {
counters[name]++;
} else {
counters[name] = 1;
}
},
decrement: function (name) {
if (counters.hasOwnProperty(name)) {
if (Object.hasOwnProperty.call(counters, name)) {
counters[name]--;
if (counters[name] === 0) {
delete counters[name];
}
}
},
isEmpty: function (name) {
return !(counters.hasOwnProperty(name) && counters[name] !== 0);
return !Object.hasOwnProperty.call(counters, name)
|| counters[name] === 0;
}
};
})();

var events = {
subscribe: function (name, callback) {
if (!callbacks.hasOwnProperty(name)) {
if (!Object.hasOwnProperty.call(callbacks, name)) {
callbacks[name] = [];
}
if (callbacks[name].indexOf(callback) === -1) {
callbacks[name].push(callback);
}
},
unsubscribe: function (name, callback) {
if (callbacks.hasOwnProperty(name)) {
if (Object.hasOwnProperty.call(callbacks, name)) {
var position = callbacks[name].indexOf(callback);
if (position !== -1) {
if (iterators.isEmpty(name)) {
Expand All @@ -56,7 +57,7 @@
publish: function (name) {
var payload = Array.prototype.slice.call(arguments, 1);
console.log.apply(console, ["[Events]", name].concat(payload));
if (callbacks.hasOwnProperty(name)) {
if (Object.hasOwnProperty.call(callbacks, name)) {
// Count recursive publish/unsubscribe calls
iterators.increment(name);

Expand Down
7 changes: 5 additions & 2 deletions core/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ function (events) {
case "string":
case "number":
case "boolean":
if (!target.hasOwnProperty(key)) {
if (!Object.hasOwnProperty.call(target, key)) {
target[key] = template[key];
}
break;
case "object":
if (!target.hasOwnProperty(key) || (template[key] instanceof Array && !(target[key] instanceof Array))) {
if (!Object.hasOwnProperty.call(target, key)
|| (template[key] instanceof Array
&& !(target[key] instanceof Array))
) {
target[key] = template[key] instanceof Array ? [] : {};
}
stubCopy(target[key], template[key]);
Expand Down
2 changes: 1 addition & 1 deletion core/ui/dock.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function (ui, css, mustache, dockTemplate) {
}
}, false);
Object.keys(defaults).forEach(function (key) {
if (descriptor.hasOwnProperty(key)) {
if (Object.hasOwnProperty.call(descriptor, key)) {
api[key] = descriptor[key];
} else {
api[key] = defaults[key];
Expand Down
2 changes: 1 addition & 1 deletion core/ui/gadget.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
document.querySelector("#gadgets menu").appendChild(container);

"title content".split(" ").forEach(function (key) {
if (descriptor.hasOwnProperty(key)) {
if (Object.hasOwnProperty.call(descriptor, key)) {
api[key] = descriptor[key];
}
});
Expand Down
2 changes: 1 addition & 1 deletion core/ui/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
};

"title tooltip callback target url path".split(" ").forEach(function (key) {
if (descriptor.hasOwnProperty(key)) {
if (Object.hasOwnProperty.call(descriptor, key)) {
api[key] = descriptor[key];
}
});
Expand Down
11 changes: 5 additions & 6 deletions core/xmpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
uri = handler.xmlns(prefix);
}
if (uri === null) {
if (handler.xmlns && handler.xmlns.hasOwnProperty(prefix)) {
if (handler.xmlns && Object.hasOwnProperty.call(handler.xmlns, prefix)) {
uri = handler.xmlns[prefix];
} else if (xpathXmlns.hasOwnProperty(prefix)) {
} else if (Object.hasOwnProperty.call(xpathXmlns, prefix)) {
uri = xpathXmlns[prefix];
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@
filter: function (stanza) {
var id = stanza.getAttributeNS("", "id");
return (stanza.tagName === "iq" // IQ stanza
&& id !== null && id !== "" && iqCallbacks.hasOwnProperty(id) // Match the ID attribute
&& id !== null && id !== "" && Object.hasOwnProperty.call(iqCallbacks, id) // Match the ID attribute
&& iqCallbacks[id].from.indexOf(stanza.getAttributeNS("", "from")) !== -1 // Match the from address
&& iqCallbacks[id].type.indexOf(stanza.getAttributeNS("", "type")) !== -1 // Match the response type
);
Expand Down Expand Up @@ -143,9 +143,8 @@
if (callback instanceof Function) {
var id = iq.getAttributeNS("", "id");
if (id === null || id === "") {
do {
id = Math.floor(Math.random() * 0xffffffff).toString(16);
} while (iqCallbacks.hasOwnProperty(id));
do { id = Math.floor(Math.random() * 0xffffffff).toString(16); }
while (Object.hasOwnProperty.call(iqCallbacks, id));
iq.setAttribute("id", id);
}
iqCallbacks[id] = {
Expand Down
4 changes: 3 additions & 1 deletion core/xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ define(function () {return function (node, xpath, type, xmlns) {
} else if (!(xmlns instanceof Function)) {
var _xmlns = xmlns;
xmlns = function (prefix) {
return _xmlns.hasOwnProperty(prefix) ? _xmlns[prefix] : null;
return Object.hasOwnProperty.call(_xmlns, prefix)
? _xmlns[prefix]
: null;
};
}

Expand Down
2 changes: 1 addition & 1 deletion modules/contactList.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function (events, dock, roster, template, settings) {
});
// Remove from old groups
Object.keys(htmlCache).filter(function (group) {
return htmlCache[group].hasOwnProperty(jid) &&
return Object.hasOwnProperty.call(htmlCache[group], jid) &&
(changedContacts[jid].groups.indexOf(group) === -1);
}).forEach(function (group) {
removeContactFromCache(group, jid);
Expand Down
17 changes: 10 additions & 7 deletions modules/roster.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ function (events, xmpp, xpath, rosterCache, jidParser) {
callback: function (presence) {
var jid = jidParser(presence.getAttribute("from"));
if (jid && Object.hasOwnProperty.call(roster.contacts, jid.bare)) {
var contact = roster.contacts[jid.bare];
var resource = Object.hasOwnProperty.call(contact.resources, jid.resource) ?
contact.resources[jid.resource] :
(contact.resources[jid.resource] = {});
var type = presence.hasAttribute("type") ? presence.getAttribute("type") : "";
var contact = roster.contacts[jid.bare],
resource = Object.hasOwnProperty.call(contact.resources, jid.resource)
? contact.resources[jid.resource]
: (contact.resources[jid.resource] = {}),
type = presence.hasAttribute("type")
? presence.getAttribute("type")
: "";
switch (type) {
case "": // Available is indicated by lack of "type" attribute
["show", "status", "priority"].forEach(function (tagName) {
Expand Down Expand Up @@ -255,8 +257,9 @@ function (events, xmpp, xpath, rosterCache, jidParser) {
contact[property] = item.getAttribute(property) || "";
});
contact.groups = [];
contact.resources = Object.hasOwnProperty.call(roster.contacts, jid) ?
roster.contacts[jid].resources : {};
contact.resources = Object.hasOwnProperty.call(roster.contacts, jid)
? roster.contacts[jid].resources
: {};
xpath(item, "roster:group", Array, xmlns).forEach(function (group) {
if (contact.groups.indexOf(group.textContent) === -1) {
contact.groups.push(group.textContent);
Expand Down
8 changes: 6 additions & 2 deletions modules/rosterCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@
if (!rosterCache) {
loadFromStorage();
}
return Object.hasOwnProperty.call(rosterCache, jid) ? rosterCache[jid] : {version: "", contacts: {}};
return Object.hasOwnProperty.call(rosterCache, jid)
? rosterCache[jid]
: {version: "", contacts: {}};
},

version: function (jid) {
if (!rosterCache) {
loadFromStorage();
}
return Object.hasOwnProperty.call(rosterCache, jid) ? rosterCache[jid].version : "";
return Object.hasOwnProperty.call(rosterCache, jid)
? rosterCache[jid].version
: "";
}
};
});

0 comments on commit 51375d4

Please sign in to comment.