__ __ _
/ /_ ____ / / ()
/ __ / __ `/ / __ \ / / /
/ / / / // ( ) / / / / ( )
// //_,/// /()/ // v0.1.0
//
hash.js is the minimal way to safely use JavaScript objects as hashes.
Fast: Usage of a regular object as the hash (Iterating over a list is slow)
Safe: Any key can be used. Keys with names that start with "__" are implicitly escaped to circumvent the mess that JS implementations induce with "magic" properties like proto, count, parent
Minimal: No constructor is used, just pass a regular object to the functions, so that you do not need to rewrite great parts of your code. The data is transparent. Passing it around is as easy as before.
Installation
npm install hash
Example in node.js
var hash = require('hash')
var myHash = {}
hash.set(myHash, 'a', 123)
hash.set(myHash, 'proto', 'value')
hash.get(myHash, 'a') // 123
hash.get(myHash, 'proto') // 'value'
hash.has(myHash, 'constructor') // false
hash.del(myHash, 'a')
/*
myHash is now { "proto%": 'value' }. As you can see, proto was escaped.
If you want to loop fast over the object, do it like this, since some keys could have been escaped:
*/
for (var key in myHash) {
key = hash.unescapeKey(key)
if (!hash.has(key))
continue
console.log(key, hash[key])
}
Tested in
node.js
IE 5.5+, FF 3+, Chrome 1+, Opera 10+, Safari 4+
Mobile Safari 4.0
Further reads
http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/
http://www.2ality.com/2012/01/objects-as-maps.html