项目作者: TylorS

项目描述 :
Persistent HashMap for TypeScript
高级语言: TypeScript
项目地址: git://github.com/TylorS/typed-hashmap.git
创建时间: 2017-01-23T04:29:58Z
项目社区:https://github.com/TylorS/typed-hashmap

开源协议:MIT License

下载


@typed/hashmap

Immutable HashMap for TypeScript

A fast and persistent (immutable) Hash Array Map Trie for TypeScript.

This is heavily based off of hamt and
was mainly done by me for learning purposes, but it is likely very useful, and I
plan to use it as well.

Once main difference to the hamt library is that keys can be of any type
including objects or arrays much like ES2015 Maps.
Another notable change is there are not prototype methods to allow
for ES2015 modules to be tree-shaken for smaller builds :fire:

There is a heavy emphasis on typing this library, and it’s highly recommended
to be used with TypeScript to reap the full benefits of type guarantees.

There is more to be done but the bare minimum is definitely present.

Let me have it!

  1. npm install --save @typed/hashmap

API

All multi-parameter functions are curried!

Creating a HashMap

empty<K, V>(): HashMap<K, V>

Creates an empty HashMap that will accept type K as keys and V as values.

  1. import { empty } from '@typed/hashmap';
  2. const map = empty<string, number>();

fromObject<V>(object: { [key: string]: V }): HashMap<K, V>

Creates a HashMap from an object.

  1. import { fromObject } from '@typed/hashmap';
  2. const map = fromObject<number>({ a: 1, b: 2 });

fromArray<K, V>(array: Array<[K, V]>): HashMap<K, V>

Creates a HashMap from an array of tuples.

  1. import { fromArray } from '@typed/hashmap';
  2. const map = fromArray<string, number>([ ['a', 1], ['b', 2] ]);

fromIterable<K, V>(iterable: Iterable<[K, V]>): HashMap<K, V>

Creates a HashMap from an Iterable.

Warning: this method using Array.from internally, and will require a polyfill
if not in an environment that supports this feature.

  1. import { fromIterable } from '@typed/hashmap';
  2. const map = fromIterable(someIterable);

Using a HashMap

set<K, V>(key: K, value: V, map: HashMap<K, V>): HashMap<K, V>

Returns a new HashMap containing the key and value passed to set.
This operation is immutable and will not alter the map passed to it.

  1. import { set, get, empty } from '@typed/hashmap';
  2. const map = empty<string, number>();
  3. const a = set('a', 1, map);
  4. console.log(get('a', a)) // 1

get<K, V>(key: K, map: HashMap<K, V>): V | null

Attempts to find a value in a given HashMap. Returns null if none can be found.

  1. import { set, get, empty } from '@typed/hashmap';
  2. const map = empty<string, number>();
  3. const a = set('a', 1, map);
  4. console.log(get('a', a)) // 1

has<K, V>(key: K, map: HashMap<K, V>): boolean

Returns true if a map contains a particular key and false if it does not.

  1. import { empty, has, set } from '@typed/hashmap';
  2. const hasA = has('a');
  3. const map = empty<string, number>();
  4. hasA(map) // false
  5. hasA(set('a', 1, map)) // true

size<K, V>(map: HashMap<K, V>): number

Returns the number of key value pairs a given map contains

  1. import { size, empty, fromObject } from '@typed/hashmap';
  2. size(empty()) // 0
  3. size(fromObject({ a: 1, b: 2 })) // 2

remove<K, V>(key: K, map: HashMap<K, V>): HashMap<K ,V>

Returns a HashMap that no longer contains a value for key.

  1. import { remove, fromObject, has } from '@typed/hashmap';
  2. const map = fromObject({ a: 1, b: 2, c: 3})
  3. const hasB = has('b')
  4. hasB(map) // true
  5. hasB(remove('b', map)) // false

entries<K, V>(map: HashMap<K, V>): Iterator<[K, V]>

Guaranteeing no order creates an iterator of keys and values held within
a given HashMap.

  1. import { entries, fromObject } from '@typed/hashmap';
  2. const map = fromObject({ a: 1, b: 2, c: 3 })
  3. for (let entry of entries(map)) {
  4. console.log(entry) // ['a', 1] ['b', 2] ['c' 3]
  5. }
  6. // manually using iterator
  7. const iterator = entries(map)
  8. console.log(iterator.next().value) // ['a', 1]
  9. console.log(iterator.next().value) // ['c', 3]
  10. console.log(iterator.next().value) // ['b', 2]
  11. console.log(iterator.next().value) // null

keys<K, V>(map: HashMap<K, V>): Iterator<K>

Guaranteeing no order creates an iterator of keys held within
a given HashMap.

  1. import { keys, fromArray } from '@typed/hashmap';
  2. const map = fromArray([ ['a', 1], ['b', 2], ['c', 3] ])
  3. const iterator = keys(map)
  4. console.log(iterator.next().value) // 'a'
  5. console.log(iterator.next().value) // 'b'
  6. console.log(iterator.next().value) // 'c'
  7. console.log(iterator.next().value) // null

values<K, V>(map: HashMap<K, V>): Iterator<V>

Guaranteeing no order creates an iterator of keys held within
a given HashMap.

  1. import { keys, fromArray } from '@typed/hashmap';
  2. const map = fromArray([ ['a', 1], ['b', 2], ['c', 3] ])
  3. const iterator = keys(map)
  4. console.log(iterator.next().value) // 1
  5. console.log(iterator.next().value) // 2
  6. console.log(iterator.next().value) // 3
  7. console.log(iterator.next().value) // null

reduce<K, V, R>(f: (accum: R, value: V, key?: K) => R, seed: R, map: HashMap<K, V>): R

Fold over the values held in a HashMap, similar to Array.prototype.reduce.

  1. import { reduce, fromIterable } from '@typed/hashmap';
  2. const iterable = new Map([ [1, 1], [2, 2], [3, 3] ]);
  3. const map = fromIterable(iterable);
  4. const sum = (x: number, y: number) => x + y;
  5. console.log(reduce(sum, 0, map)) // 6

forEach<K, V>(f: (value: V, key?: K) => any, map: HashMap<K, V>): HashMap<K, V>

Perform side effects on each value contained in a HashMap, returning the original
HashMap.

  1. import { forEach, fromObject } from '@typed/hashmap';
  2. const map = fromObject({ a: 1, b: 2, c: 3 })
  3. const map2 = forEach(x => console.log(x), map) // 1, 2, 3
  4. map === map2 // true

map<K, V, R>(f: (value: V, key?: K) => R, map: HashMap<K, V>): HashMap<K, R>;

Creates a new HashMap of the same keys, but new values as the result of calling
the provided function on each value contained in the given HashMap, similar to
Array.prototype.map.

  1. import { map, forEach, fromObject } from '@typed/hashmap';
  2. const a = map(x => x + 1, fromObject({ a: 1, b: 2, c: 3 }))
  3. forEach((value, key) => console.log(value, key), a) // 'a' 2 , 'b' 3, 'c' 4

filter<K, V>(predicate: (value: V, key?: K) => boolean, map: HashMap<K, V>): HashMap<K, V>

Creates a new HashMap containing only values that return true when the predicate
function is called with a given value, similar to Array.prototype.filter.

  1. import { filter, forEach, fromObject } from '@typed/hashmap';
  2. const a = filter(x => x % 2 === 0, fromObject({ a: 1, b: 2, c: 3 }))
  3. forEach((value, key) => console.log(value, key), a) // 'b' 2