dictionaryjs
v1.0.10
Published
A simple dictionary wrapper for JavaScript providing hashmap functionality with the added benefit of accessing keys using box and dot operators!
Downloads
10,272
Maintainers
Readme
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 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.
install
Requirements
- ECMAScript 2017 (ES2017)
- Node.JS 8.x or later (tested on 8.1.3)
See branch "classic" for earlier versions of Node.JS.
API
Setup
To use simply include at the top of your script:
TypeScript example:
Set
Store values to a key:
Get
Get a value from a key:
Get Default
Get the value of a key or return the default value.
If key is not contained within dictionary then the default value will be returned.
Remove
Remove an entry by key:
Size
Determine how many entries are in the dictionary, returns an integer.
Has
Check if key is in the dictionary, returns boolean.
Contains
Check if value is in the dictionary, returns boolean.
Empty
Removes all dictionary entries. This method is blocking.
Async Empty
Removes all dictionary entries. This method is non-blocking.
Or as a promise:
For Each
To loop over each entry in the dictionary use: This method is blocking.
To break and end looping:
Async For Each
To loop over each entry in a non-blocking manner:
To break and end looping:
(Optional) You may also call a function once the asyncForEach loop is complete:
Or as a promise:
Using the (for ... of) loop:
Loop through all entries. This is blocking.
To loop through each key within the collection you may use the for...in loop. This is blocking.
Entries Iterator
Loop through all key and value pairs at once:
Get Keys
Returns an array of keys:
Get Values
Returns an array of values:
Initial Key/Values
Declare the Dictionary to have initial key/values with the constructor:
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:
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.
In the event you wish to still use the dot or box operators you can manually invalidate the cache yourself...