Library for working with JavaScript hashmaps.
- Modifying
- hashMap.add(map, key, value):Map - Add a value to a map and return the Map
- hashMap.union(targetMap, additionalMap, canOverwrite) - merges two maps and ignores or overwrites with conflicts
- Cloning
- hashMap.clone(map):Map - Clones a given Map
- Conversion
- hashMap.stringify(map, indent) - converts a Map to a string representation
- hashMap.toObject(map) - converts a hashMap to an Object
- hashMap.fromObject(object) - converts an object's properties to hashMap keys
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) 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