@ryanmorr/hashmap
v0.1.3
Published
A superior alternative to object literals for basic hash maps
Downloads
4
Readme
hashmap
A superior alternative to object literals for basic hash maps
Install
Download the CJS, ESM, UMD versions or install via NPM:
npm install @ryanmorr/hashmap
Usage
Use just like an object literal:
import hashmap from '@ryanmorr/hashmap';
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.