hashMap

Library for working with JavaScript hashmaps.

Note: JavaScript Maps can sometimes be faster than using Objects, and sometimes slower.

(Current understanding is that Maps do better with more updates made)

There are many searches such as javascript map vs object performance with many interesting links to come across.

Methods

(static) add(map, key, value) → {Map}

Set a Map in a functional manner (adding a value and returning the map)

Example
const objectToMap = { key1: 1, key2: 2, key3: 3 };
const keys = [...Object.keys(objectToMap)];
// ['key1', 'key2', 'key3'];

const result = keys.reduce(
 (result, key) => utils.hashMap.add(result, key, objectToMap[key]),
 new Map()
);
// Map([[ 'key1',1 ], ['key2', 2], ['key3', 3]]);
Parameters:
Name Type Description
map Map

the map to be updated

key any
value any
Returns:
  • the updated map value
Type
Map

(static) clone(target) → {Map}

Clones a Map

Example
const sourceMap = new Map();
sourceMap.set('first', 1);
const mapClone = utils.hashMap.clone(sourceMap);
mapClone.has('first'); // true
Parameters:
Name Type Description
target Map

Map to clone

Returns:
  • clone of the target map
Type
Map

(static) fromObject(target) → {Map.<String, any>}

See:
  • hashMap.toObject - to reverse the process

Creates a Map from the properties of an Object

For example, say we have an object:

const targetObject = { first: 1, second: 2, third: 3 };

We can convert it to a Map as follows:

const targetMap = utils.hashMap.fromObject(targetObject)
// new Map([['first', 1], ['second', 2], ['third', 3]]);
Parameters:
Name Type Description
target Object

target object with properties that should be considered keys

Returns:
  • converted properties as keys in a new map
Type
Map.<String, any>

(static) getSet(map, key, functor) → {Map}

Use this for times where you want to update a value

key = 'somethingToIncrement';
defaultValue = null;

const initialMap = new Map([
  [key, defaultValue]
]);
// Map([[ 'somethingToIncrement', null ]]);

const functor = (value) => { //, key, map) => {
  if (!value) return 1;
  return value + 1;
};

HashMapUtil.getSet(initialMap, key, functor);
HashMapUtil.getSet(initialMap, key, functor);
HashMapUtil.getSet(initialMap, key, functor);
HashMapUtil.getSet(initialMap, key, functor);
HashMapUtil.getSet(initialMap, key, functor);

initialMap.get(key); // 5
Parameters:
Name Type Description
map Map

map to get and set values from

key any

they key to GET and SET the value (unless setKey is provided)

functor function

the function called with the arguments below - returning the value to set

Properties
Name Type Description
value any

the first argument is the current value

key any

the second argument is the key passed

map any

the third argument is the map being acted upon

Returns:
Type
Map

(static) mappingFn(map, defaultValueopt) → {function}

Simple and safe Map accessing function.

styleMap = new Map(['1', 'background-color: #FF0000'], ['2', 'background-color: #00FF00']]);
styleFn = utils.map.mappingFn(styleMap, 'background-color: #aaaaaa');

styleFn('1'); // 'background-color: #FF0000';
styleFn('2'); // 'background-color: #00FF00';
styleFn('somethingElse'); // 'background-color: #aaaaaa' - because it was not found
Parameters:
Name Type Attributes Default Description
map Map

map to use when checking the subsequent function

defaultValue any <optional>
''

default value to return if key is not found

Returns:
  • (key) => map.get(key) || defaultValue
Type
function

(static) reverse(map) → {Map.<any, any>}

Flip the key and value of the map

encodingMap = new Map([[' ', '%20'],['\\n', '%0A'],['\\t', '%09']]);
encodingMap.get(' '); // '%20'

decodingMap = utils.map.reverse(encodingMap);
decodingMap.get('%20'); // ' '
Parameters:
Name Type Description
map Map.<any, any>

map to reverse

Returns:
  • new map with the keys and values reversed
Type
Map.<any, any>

(static) stringify(target, indentation) → {String}

Serializes a hashMap (plain javascript Map) to a string

const target = new Map([['first', 1], ['second', 2]]);
HashMapUtil.stringify(target);
// '{"dataType":"Map","value":[["first",1],["second",2]]}'

Note, that passing indent will make the results much more legible.

{
  "dataType": "Map",
  "value": [
    [
      "first",
      1
    ],
    [
      "second",
      2
    ]
  ]
}
Parameters:
Name Type Description
target Map

the Map to be serialized

indentation Number

the indentation passed to JSON.serialize

Returns:
  • JSON.stringify string for the map
Type
String

(static) toObject(target) → {Object}

See:
  • hashMap.fromObject - to reverse the process

Converts a map to an object

For example, say we have a Map:

const targetMap = new Map([['first', 1], ['second', 2], ['third', 3]]);

We can convert it to an Object as follows:

const targetMap = utils.hashMap.toObject(targetObject)
// { first: 1, second: 2, third: 3 };
Parameters:
Name Type Description
target Map

map to be converted

Returns:
  • object with the properties as the target map's keys.
Type
Object

(static) union(targetMap, additionalMap, allowOverwriteopt) → {Map}

Creates a new map that includes all entries of targetMap, and all entries of additionalMap.

If allowOverwrite is true, then values found in additionalMap will take priority in case of conflicts.

const targetMap = new Map([['first', 'John'], ['amount': 100]]);
const additionalMap = new Map([['last': 'Doe'], ['amount': 200]]);

utils.hashMap.union(targetMap, additionalMap, true);
// Map([['first', 'John'], ['last', 'Doe'], ['amount', 200]]);

If allowOverwrite is false, then values found in targetMap will take priority in case of conflicts.

const targetMap = new Map([['first', 'John'], ['amount': 100]]);
const additionalMap = new Map([['last': 'Doe'], ['amount': 200]]);

utils.hashMap.union(targetMap, additionalMap);
utils.hashMap.union(targetMap, additionalMap, false);
// Map([['first', 'John'], ['last', 'Doe'], ['amount', 100]]);
Parameters:
Name Type Attributes Default Description
targetMap Map
additionalMap Map
allowOverwrite Boolean <optional>
false

whether targetMap is prioritized (false) or additional prioritized (true)

Returns:
Type
Map