Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including hash with all npm packages installed. Try it out:

var hash = require("hash")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

hash v0.2.1

Safely use a JavaScript object as hash

__               __        _

/ /_ ____ ______/ /_ (_)____ / __ / __ `/ ___/ __ \ / / __/ / / / / /_/ (__ ) / / / / (_ ) /_/ /_/__,_/____/_/ /_(_)_/ /____/ v0.2.1 /___/

hash.js is a simple way to safely use a JavaScript object as hash.

Fast: Usage of a regular object as the hash

Safe: Any key can be used. Keys with names that start with "__" are internally escaped to circumvent the mess that JS implementations induce with "magic" properties like proto, count, parent

Installation

npm install hash

Example in node.js

var hash = require('hash')

var existingDataWithEscapedKeys = { __: 1, b: 2, 'parent%': 3 }

var myHash = new hash(existingDataWithEscapedKeys /* optional */)

myHash.set('a', 123) myHash.set('proto', 'value')

myHash.get('parent') // 3 myHash.get('a') // 123 myHash.get('proto') // 'value'

myHash.has('constructor') // false

myHash.del('a')

myHash.get('a') // undefined

myHash.del('parent')

myHash.getData() // { __: 1, b: 2, 'proto%': 'value' }

myHash.forEach(function iterator (value, key) {

// ...

} /*, optionalThisArg */)

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free