dictionaryjs-es6
v1.0.4
Published
A Dictionary (Associative Array like) Class for ES6. Now supporting dot and bracket access through proxy, and generator/yield control flows.
Downloads
1
Maintainers
Readme
WARNING
This module has been deprecated. Please use the latest release of dictionaryjs which now supports all es6 features.
See: https://www.npmjs.com/package/dictionaryjs https://github.com/phanxgames/dictionaryjs
A Dictionary (Associative Array like) Class for ES6. Now supporting dot and bracket access through proxy, and generator/yield control flows.
Features
- Stores key/value pairs within a Associative Array like collections (Dictionaries).
- NEW! Use dot operator and brackets to access keys.
- NEW! Remove key using the DELETE operator.
- NEW! Iterator to support for..of looping.
- NEW! hasOwnProperty method
- NEW! Generator/yield async control flow (via suspend module) support.
- 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
Requirements
- ECMAScript 2015 (ES6)
- Node.JS 6.2.2 or later (tested on 6.2.2)
You may find the older version (with less features) but will work with older version of Node.js: dictionaryjs
Basic Example
Accessing Keys
You may access keys using the get/set methods, but also using the dot operator and brackets.
You may also use the getDefault method to return the default value if key was not found (or is null):
Checking if Key exists
Deleting Keys
To remove a key, you may use the remove method, or the "delete" operator command:
To empty out the collection:
Looping through keys
ForEach (Blocking)
AsyncForEach (Non-Blocking)
Using the (for...of) loop:
Each item (as just the value) within the collection can be accessed in a for...of loop. This is blocking. This will not return the key, if you need the key use another looping method.
Getting the Collection Size:
Generator/yield Async control flow using Suspend (recommended)
Using the suspend module you may write async code using the new yield command. Here is an example that will show how you can write async code in a very clean manner:
Other methods
These methods you may not find useful, but are documented in case you do:
- getKeys() - Returns an array of keys
- invalidate() - Invalidates the keys, used internally.
- setResume(resume) - used to set the suspend.resume reference, usually set in the constructor.