Multimap - Map which Allow Multiple Values for the same Key
Install
npm install multimap --save
Usage
If you'd like to use native version when it exists and fallback to polyfill if it doesn't, but without implementing Map
on global scope, do:
var Multimap = ;var m = ;
If the global es6 Map
exists or Multimap.Map
is set, Multimap
will use the Map
as inner store, that means Object can be used as key.
var Multimap = ; // if harmony is on/* nothing need to do */// or if you are using es6-shimMultimapMap = ShimMap; var m = ;var key = {};m;
Otherwise, an object will be used, all the keys will be transformed into string.
In Modern Browser
Just download the index.js
as Multimap.js
.
<script src=Multimap.js"></script>
<script>
var map = new Multimap([['a', 1], ['b', 2], ['c', 3]]);
map = map.set('b', 20);
map.get('b'); // [2, 20]
</script>
Or use as an AMD loader:
require(['./Multimap.js'], function (Multimap) {
var map = new Multimap([['a', 1], ['b', 2], ['c', 3]]);
map = map.set('b', 20);
map.get('b'); // [2, 20]
});
- Browsers should support
Object.defineProperty
andArray.prototype.forEach
.
API
Following shows how to use Multimap
:
var Multimap = ; var map = 'a' 'one' 'b' 1 'a' 'two' 'b' 2; mapsize; // 4mapcount; // 2 map; // ['one', 'two']map; // [1, 2] map; // truemap; // false map; // truemap; // false map;mapsize; // 5mapcount; // 2map; // ['one', 'two', 'three'] map;mapsize; // 7mapcount; // 2 map; // truemap; // falsemap; // falsemap; // true mapsize; // 2mapcount; // 1 map;mapsize; // 4mapcount; // 2 map; map; var keys = map; // iterator with ['a', 'b']keysnextvalue; // 'a'var values = map; // iterator ['one', 'two', 1, 2] mapclear; // undefinedmapsize; // 0mapcount; // 0
License
(The MIT License)
Copyright (c) 2013, Villa.Gao jky239@gmail.com; All rights reserved.