Skip to content

ryanmorr/hashmap

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

hashmap

Version Badge Build Status License

A superior alternative to object literals for basic hash maps

Install

Download the development or minified version, or install via NPM:

npm install @ryanmorr/hashmap

Usage

Use just like an object literal:

const map = hashmap();

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

{}.toString.call(map); //=> "[object Object]"
JSON.stringify(map); //=> "{\"foo\":1,\"bar\":2}"

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

'toString' in {}; //=> true
'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});

map.foo; //=> 10
map.bar; //=> 2
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
}

License

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

About

A superior alternative to object literals for basic hash maps

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published