oz-hash-map
v0.0.6
Published
Map which can handles objects as keys
Downloads
7
Readme
hash-map
Map which can handles objects as keys.
HashMap
let you decide how to search for key:
- selectKey: Function(obj): any function to select what to use as key
HashMap
implements all Map interface:
- clear() Removes all key/value pairs from the Map object.
- delete(key) Returns true if an element in the Map object existed and has been removed, or false if the element does not exist. has(key) will return false afterwards.
- entries() Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
- forEach(callbackFn[, thisArg]) Calls callbackFn once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
- get(key) Returns the value associated to the key, or undefined if there is none.
- has(key) Returns a boolean asserting whether a value has been associated to the key in the Map object or not.
- keys() Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
- set(key, value) Sets the value for the key in the Map object. Returns the Map object.
- values() Returns a new Iterator object that contains the values for each element in the Map object in insertion order.
- [@@iterator]() Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
install
$ npm install oz-hash-map
require / import
typescript
import HashMap from 'oz-hash-map'
typescript
var HashMap = require('oz-hash-map');
Usage
let map = new HashMap()
map.set({ x:1 }, 'some value') // map.size = 1
let res1 = map.get({ x:1 }) // res1 will be 'some value'
map.set({ x:1 }, 'override value') // map.size = 1
let res2 = map.get({ x:1 }) // res1 will be 'override value'
HashMap
let's you iterate it's keys and/or values:
let map = new HashMap()
map.set({ x:1 }, 'some value')
map.set({ x:2 }, 'other value')
for (let [k, v] of map){
console.log(k, v)
}
// output:
// Object {x: 1}
// some value
// Object {x: 2}
// other value
for (let k of map.keys()){
console.log(k)
}
// output:
// Object {x: 1}
// Object {x: 2}
for (let v of map.values()){
console.log(v)
}
// output:
// some value
// other value
HashMap
let's you decide what to use as key:
let map = new HashMap()
let select = obj => { return { x: obj.x } }
map.selectKey = select
map.set({ x:1, y: 2 }, 'some value')
map.set({ x:1, z: 3 }, 'override value')
// map will have only 1 item: { x:1, z: 3 }, 'override value'