node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org ยป

dictionaryjs

A simple dictionary wrapper for JavaScript providing hashmap functionality with the added benefit of accessing keys using box and dot operators!

Features

  • Typescript source-code.
  • Stores key/value pairs within a Associative Array like collections.
  • Use dot operator and brackets to access keys.
  • Remove key using the DELETE operator.
  • Iterator to support for..of looping.
  • Async/await support with Promises.
  • Set and Get methods for accessing keys.
  • GetDefault method with default value if value is not found.
  • Remove method to remove key from collection.
  • Size method to get total key count within collection.
  • Built-in forEach and asyncForEach methods for looping.
  • Empty and asyncEmpty to remove all entries from collection.
  • Has method checking if key is within collection.

install

npm install dictionaryjs

Requirements

  • ECMAScript 2016 (ES6)
  • Node.JS 6.x or later (tested on 6.11)

See branch "classic" for earlier versions of Node.JS.

API

Setup

To use simply include at the top of your script:

const Dictionary = require('dictionaryjs');

let dict = new Dictionary();

Set

Store values to a key:

dict.set("key",value);
//--or--
dict["key"] = value;
//--or--
dict.key = value;

Get

Get a value from a key:

dict.get("key");
//--or--
dict["key"];
//--or---
dict.key;

Get Default

Get the value of a key or return the default value.

dict.getDefault("key",default);

If key is not contained within dictionary then the default value will be returned.

Remove

Remove an entry by key:

dict.remove("key");
//--or--
delete dict["key"];

Size

Determine how many entries are in the dictionary, returns an integer.

dict.size();
//--or--
dict.length;

Has

Check if key is in the dictionary, returns boolean.

dict.has("key");

Empty

Removes all dictionary entries. This method is blocking.

dict.empty();
//--or--
dict.clear();

Async Empty

Removes all dictionary entries. This method is non-blocking.

dict.asyncEmpty(() => {
  //called after dictionary has been emptied
});

Or as a promise:

await dict.asyncEmpty();
//after dictionary has been emptied

For Each

To loop over each entry in the dictionary use: This method is blocking.

dict.forEach((key,value) => {
  //returns key and value of each
});

To break and end looping:

dict.forEach(function(key,value) {
  if (..logic..) return false;
});

Async For Each

To loop over each entry in a non-blocking manner:

dict.asyncForEach((key,value,next) => {
  //returns key and value of each
  next();
});

To break and end looping:

dict.asyncForEach((key,value,next) => {
  if (..logic..) return false;
  next();
});

(Optional) You may also call a function once the asyncForEach loop is complete:

dict.asyncForEach((key,value,next) => {
  next();
}, () => {
  //called once loop is complete
});

Or as a promise:

await dict.asyncForEach((key,value,next) => {
  next();
});
//once loop is complete

Using the (for ... of) loop:

Loop through all entries. This is blocking.

for (let value of dict) {
    console.log(value);
}

To loop through each key within the collection you may use the for...in loop. This is blocking.

for (let key in dict) {
    console.log(key + "=" + dict[key]);
}

Entries Iterator

Loop through all key and value pairs at once:

for (let [key,value] of dict.entries()) {
    console.log(key,value);
}

Get Keys

Returns an array of keys:

dict.keys();

Get Values

Returns an array of values:

dict.values();

Caching Keys

An option in the constructor (defaults to false) allows you to have the keys cached so they are not recalculated each time you begin to iterate through the collection.

First enable caching by passing true in the constructor:

let dict = new Dictionary(true);

Then as you interact with the collection the cache will invalidate on its own as long as you use the methods built into the class. In other words using the dot or box operators to set or delete keys will not invalidate the key cache.

//will invalidate cache on its own:
dict.set("key",value);

//will NOT invalidate cache on its own:
dict["key"] = value;

In the event you wish to still use the dot or box operators you can manually invalidate the cache yourself...

dict["key"] = value;
dict.invalidate();