
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
dictionaryjs
Advanced tools
A simple dictionary wrapper for JavaScript providing hashmap functionality with the added benefit of accessing keys using box and dot operators!
A simple dictionary wrapper for JavaScript providing hashmap functionality with the added benefit of accessing keys using box and dot operators!
npm install dictionaryjs
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();
FAQs
A simple dictionary wrapper for JavaScript providing hashmap functionality with the added benefit of accessing keys using box and dot operators!
The npm package dictionaryjs receives a total of 372 weekly downloads. As such, dictionaryjs popularity was classified as not popular.
We found that dictionaryjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.