A simple dictionary wrapper for JavaScript providing hashmap functionality with the added benefit of accessing keys using box and dot operators!
- 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 property 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.
npm install dictionaryjs
- ECMAScript 2017 (ES2017)
- Node.JS 8.x or later (tested on 8.1.3)
See branch "classic" for earlier versions of Node.JS.
To use simply include at the top of your script:
const Dictionary = require('dictionaryjs'); let dict = new Dictionary();
TypeScript example:
import {Dictionary} from "dictionaryjs"; let dict:Dictionary<string,string> = new Dictionary<string,string>();
Store values to a key:
dict.set("key",value); //--or-- dict["key"] = value; //--or-- dict.key = value;
Get a value from a key:
dict.get("key"); //--or-- dict["key"]; //--or--- dict.key;
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 an entry by key:
dict.remove("key"); //--or-- delete dict["key"];
Determine how many entries are in the dictionary, returns an integer.
dict.size; //--or-- dict.length;
Check if key is in the dictionary, returns boolean.
dict.has("key");
Check if value is in the dictionary, returns boolean.
dict.contains(obj);
Removes all dictionary entries. This method is blocking.
dict.empty(); //--or-- dict.clear();
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
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; });
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
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]); }
Loop through all key and value pairs at once:
for (let [key,value] of dict.entries()) { console.log(key,value); }
Returns an array of keys:
dict.keys();
Returns an array of values:
dict.values();
Declare the Dictionary to have initial key/values with the constructor:
let dict = new Dictionary({"key":"value"});
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(null,{cacheKeys: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();