What is es6-map?
The es6-map npm package provides an implementation of the Map data structure as specified in ECMAScript 6 (ES6). This package is particularly useful for environments that do not fully support ES6 features, allowing developers to use Map functionalities such as maintaining key-value pairs where keys can be of any type and preserving the insertion order of keys.
What are es6-map's main functionalities?
Creating and initializing a Map
This feature allows the creation of a Map and initialization with key-value pairs. The 'get' method retrieves the value associated with a key.
const ES6Map = require('es6-map');
const map = new ES6Map([['key1', 'value1'], ['key2', 'value2']]);
console.log(map.get('key1')); // Outputs: 'value1'
Checking existence of a key
This feature checks whether a specific key exists in the Map using the 'has' method.
const map = new ES6Map([['key1', 'value1']]);
console.log(map.has('key1')); // Outputs: true
console.log(map.has('key2')); // Outputs: false
Iterating over Map entries
This feature demonstrates how to iterate over the entries of the Map, accessing both keys and values.
const map = new ES6Map([['key1', 'value1'], ['key2', 'value2']]);
for (let [key, value] of map) {
console.log(key + ': ' + value);
}
Other packages similar to es6-map
immutable
The 'immutable' package provides persistent immutable data structures including Map, List, Set, etc. Unlike es6-map, which only offers a Map polyfill, 'immutable' offers a wide range of data structures and features like lazy operation chaining, deep persistence, and complex data nesting.
collections
The 'collections' package offers data structures with performance in mind, including Map, Set, List, and others. It provides additional utilities compared to es6-map, such as sorted maps and sets, and specialized collections like Deque.
es6-map
Map collection as specified in ECMAScript6
Warning:
v0.1 version does not ensure O(1) algorithm complexity (but O(n)). This shortcoming will be addressed in v1.0
Usage
It’s safest to use es6-map as a ponyfill – a polyfill which doesn’t touch global objects:
var Map = require('es6-map');
If you want to make sure your environment implements Map
globally, do:
require('es6-map/implement');
If you strictly want to use the polyfill even if the native Map
exists, do:
var Map = require('es6-map/polyfill');
Installation
$ npm install es6-map
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
API
Best is to refer to specification. Still if you want quick look, follow examples:
var Map = require('es6-map');
var x = {}, y = {}, map = new Map([['raz', 'one'], ['dwa', 'two'], [x, y]]);
map.size;
map.get('raz');
map.get(x);
map.has('raz');
map.has(x);
map.has('foo');
map.set('trzy', 'three');
map.size
map.get('trzy');
map.has('trzy');
map.has('dwa');
map.delete('dwa');
map.size;
map.forEach(function (value, key) {
});
for (value of map) {
}
var iterator = map.values();
iterator.next();
iterator.next();
iterator.next();
iterator.next();
map.clear();
map.size;
Tests
$ npm test