A superior alternative to object literals for basic hash maps
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
src
test
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.travis.yml
README.md
UNLICENSE
gulpfile.babel.js
package.json

README.md

hashmap

GitHub version Build Status Size

A superior alternative to object literals for basic hash maps

Usage

Use just like an object literal:

const map = hashmap();

map.foo = 1;
map.bar = 2;

console.log({}.toString.call(map)); // [object Object]

console.log(JSON.stringify(map)); // {"foo":1,"bar":2}

Unlike object literals, there is no chance of name collisions:

console.log('toString' in {}); // true
console.log('toString' in hashmap()); // false

for (const key in map) {
    // `hasOwnProperty` check is unnecessary
}

Provide objects as arguments to pre-populate the map:

const map = hashmap({foo: 1}, {bar: 2}, {foo: 10, baz: 3});

console.log(map.foo) // 10
console.log(map.bar) // 2
console.log(map.baz) // 3

Implements the iterable protocol, enabling for...of loops:

const map = hashmap({foo: 1, bar: 2, baz: 3});

for (const [key, value] of map) {
    // do something
}

Installation

Hashmap is CommonJS and AMD compatible with no dependencies. You can download the development or minified version, or install it in one of the following ways:

npm install ryanmorr/hashmap

bower install ryanmorr/hashmap

Tests

Run unit tests in the command line by issuing the following commands:

npm install
npm install -g gulp
gulp test

License

This project is dedicated to the public domain as described by the Unlicense.