JavaScript ES6 Map and Set polyfill and improvements.
JavaScript introduced Map and Set objects with new ES6.
Map provides functionality to use objects as object key.
Set is a unique collection of items.
This implementation uses hash tables to identify items in Set an Map and gives extra features.
Now available with bower with name 'es6-set-and-map'
Usage.
To use with NodeJS.
npm install es6-set-and-map.
Map
var Map = map obj = blank_obj = {} span_el = document; // Sets values.// Set method returns it's map, so this call can be chained.// You can use anything as key.obj // You can specify a new item's index too. ; // Adds values.// Will work only if key does not exist in the map.// Returns true if values added.obj === true; // blank_obj is already used, so this will return false.// Position argument can be used with add too.obj === false; // Increments numeric value.// If value is not defined, creates with initial value 0, than increments it.// Works like i++, so will return previous value.obj'++' 'something' === 0; // Decrements numeric value.// Same as ++, but works like --i, so will return the new value.obj;obj'--' 'something' === 11; // Array manipulation methods.// If value is not array, returns false.// If value is not defined, creates empty array and does operations on it.// Returns result of the operation.obj === 3;obj === 4; obj == 'item2';obj == 'firstitem';// This two returns changed value.obj;obj; // Object property manipulation methods.// If value is not an object, returns false.// If value is not defined, creates empty object and does operations on it.obj === true;obj === true;obj === 'changed value';obj === true;obj === false; // Gives value for given key.console; // Checks if given key is used.console; // Deletes value of this key.// Returns true if value existed.console; // Elements count.console; // Array of keys and objects.console;console; // Iterating.objstart;while objnext // use obj.key, obj.value // Iterating back.obj;while obj // use obj.key, obj.value // ES6 "for of" iterationfor let key value of obj // ...
Set
var Set = set obj = blank_obj = {} span_el = document; // Adds values.obj === true;obj === true;obj === false; // Adds multiple values and returns added items count.obj; // Checks if item exists.console; // Removes item.// Returns true if item existed.console; // Elements count.console; // Array of items.console; // Iterating.objstart;while objnext // use obj.value // Iterating back.obj;while obj // use obj.value // ES6 "for of" iterationfor let value of obj // ...
Browser support
Firefox | Chrome | IE | Opera | Safari |
---|---|---|---|---|
5 | 5 | 9 | 11.60 | 5.1 |