mauriciosantos / Buckets-JS

A complete, fully tested and documented data structure library written in pure JavaScript.
MIT License
1.25k stars 112 forks source link

Various Dictionary bugs #6

Closed cspotcode closed 9 years ago

cspotcode commented 11 years ago
var dict = new buckets.Dictionary();

dict.set("toString", 42);
dict.size() === 0; // wrong, should be 1
dict.remove("toString");
dict.size() === -1; // wrong, should be 0

dict.set("hasOwnProperty", "foo");
try {
    dict.keys(); // throws an error trying to invoke "foo" as a function
} catch(e) {
    dict.remove("hasOwnProperty");
}

dict.set("__proto__", {value: 123});
dict.get("value") === 123; // wrong, should be undefined

The __proto__ issue can be prevented by prefixing all keys with a weird character like "~" before storing them into table. this.table["~" + key] = {.....}

Instead of this.table.hasOwnProperty, do:

var has = function(obj, key) {
    Object.prototype.hasOwnProperty.call(obj, key);
}
has(table, key); // instead of table.hasOwnProperty(key)

When checking whether or not the dictionary has a certain key, always use has().

mauriciosantos commented 9 years ago

Fixed. Thank you.