A Library for adding array-like functionality to Javascript Maps.
I love Javascript Maps, but they need some additional functionality to make them more useful. This library is an attempt to provide that functionality.
install via npm
npm install --save map-helper
Then in your application
import * as mapHelper from 'map-helper';
Takes and a Map and provides a new Map after applying a callback.
mapHelper.mapMap(map,callback)
map (Map)
callback (function) [Key, value, index]
(Map)
let foo = new Map([
["stuff",0],
["junk",1],
["gubbins",2]
]);
let bar = mapHelper.mapMap(foo, (key,value, index) => [key , value + 1]);
// result: Map(3) {"stuff" => 1, "junk" => 2, "gubbins" => 3}
Reduces a map based on a callback and an initial value.
mapHelper.mapMap(map,callback,inital)
map (Map)
callback (function) [Key, value, index]
initial (any)
(any)
let foo = new Map([
["stuff",0],
["junk",1],
["gubbins",2]
]);
let baz = mapHelper.reduceMap(foo, (current, key, value, index) => current + value , 0);
// result: 3
Merges an array of maps, aggregating key collisions as default behavior. Accepts an optional callback to handle collisions
mapHelper.mergeMap(maps,callback)
maps (Array)
callback (function) [Key, oldValue, newValue, numberOfCollisions]
(Map)
let foo = new Map([
["junk",1],
["gubbins",2]
]);
let anotherMap = new Map([
["mary", 1],
["gubbins", 4]
]);
let baz = mapHelper.reduceMap(foo, (current, key, value, index) => current + value , 0);
// result: Map(3) {"junk" => 1, "gubbins" => (2) [2, 4], "mary" => 1}
let mergedWithCallback = mapHelper.mergeMaps([foo, anotherMap], (key, oldVal, newVal, collisions) =>{
return oldVal + newVal;
});
//result: Map(3) {"junk" => 1, "gubbins" => 6, "mary" => 1}
Inverts the keys and values of a map, aggregating values with key collisions as default behaviour. Accepts an options callback to handle key collisions.
mapHelper.invertMap(map,callback)
map (Map)
callback (function) [Key, oldValue, newValue, numberOfCollisions]
(Map)
let anotherMap = new Map([
["peter", 0],
["paul", 1],
["mary", 1],
["gubbins", 4]
]);
let invertedMap = mapHelper.invertMap(anotherMap);
// result: Map(3) {0 => "peter", 1 => (2) ["paul", "mary"], 4 => "gubbins"}
Converts a plain object to a map, using only own properties.
mapHelper.mapFromObject(object)
object (Object)
(Map)
let objectToConvert = {
key1:"foo",
key2:"bar",
key3:"baz"
}
let convertedObject = mapHelper.mapFromObject(objectToConvert);
// result: Map(3) {"key1" => "foo", "key2" => "bar", "key3" => "baz"}