0.1.3 • Published 4 months ago

@ryanmorr/hashmap v0.1.3

Weekly downloads
1
License
Unlicense
Repository
github
Last release
4 months ago

hashmap

Version Badge License Build Status

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.

0.1.3

4 months ago

0.1.2

5 years ago

0.1.1

5 years ago