Socket
Socket
Sign inDemoInstall

hash

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hash

The minimal way to safely use JavaScript objects as hashes


Version published
Maintainers
1
Created
Source
__               __        _

/ /_ ____ / / () / __ / __ `/ / __ \ / / / / / / / // ( ) / / / / ( ) // //_,/// /()/ // 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

Keywords

FAQs

Package last updated on 25 Jan 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc