DefaultMap
This aims to provide a simple Map prototype with default value / value constructor.
Installation
npm install --save default-map
Usage
Requiring
var DefaultMap = ;
Initialization
Constructor signature: DefaultMap(options)
.
With a default value
var map = defaultValue: 'my default value' ;
With a default generator function
var map = { return 'the default value for the key: ' + key;} ;
With initial data
var map = data: the: 'initial data' defaultValue: 'the default value';
Checking key existence
map;
Setting a value to a key
map;
Getting the value of key
map;
If map
has no key 'foo'
, it will be created using the default value / the default generator.
Deleting a key
map;
Getting the entry set (useful for JSON serialization)
map;
Checking if the map is empty
map;
Iterating over the whole entry set
map;
Note that:
- Giving
this
as the second argument ofDefaultMap#forEach
preserves the calling context'sthis
. - The third argument of the callback contains a reference to the iterated map.