UNPKG

@paosder/vector-map

Version:

vector-like map class using es6 map with array.

149 lines 4.61 kB
/** * MapSource object. * * This is the inner object which VectorMap uses to manage data. * Some methods return MapSource object reference directly, * this means you can modify it yourself if you needed. * But modifying `key` is strongly discouraged because * it could make a mangling pointer. */ export declare type MapSource<U, V> = { key: U; value: V; }; export declare class VectorMap<U, V> { /** * Pointer map. * * It contains an index of the source array to access the target directly. */ pointer: Map<U, number>; /** * Data source array. */ source: Array<MapSource<U, V>>; /** * Create a new VectorMap. * @param arr Initial data which has pairs of key and value. */ constructor(arr?: Array<[U, V]>); /** * Iterate items. Same to Array.forEach in source array. * @param callback callback function to execute every iteration. */ forEach(callback: (value: V, key: U, index: number, arr: Array<MapSource<U, V>>) => any): void; /** * Generate a new array. Same to Array.map in source array. * @param mapFunc map function to execute every iteration. * @returns Array<T>. */ map<T>(mapFunc: (value: V, key: U, index: number, arr: Array<MapSource<U, V>>) => T): Array<T>; /** * Equivalent to Array.some. * @param someFunc function to execute every iteration. Must return boolean type. * @returns boolean. */ some(someFunc: (value: V, key: U, index: number, arr: Array<MapSource<U, V>>) => boolean): boolean; /** * Get head of map. */ get head(): MapSource<U, V> | undefined; /** * Get tail of map. */ get tail(): MapSource<U, V> | undefined; /** * Reduce map. Same to Array.reduce in source array. * @param reducer reducer function to execute every iteration. * @param accumulator default value of accumulator. * @returns T */ reduce<T>(reducer: (acc: T, value: V, key: U, index: number, arr: Array<MapSource<U, V>>) => T, accumulator: T): T; /** * Reduce map via filter function. * @param filterFunc filter function to execute every iteration. * If returns undefined, it won't be added in result array. * @returns T */ filterMap<T>(filterFunc: (value: V, key: U, index: number, arr: Array<MapSource<U, V>>) => T | undefined): Array<T>; [Symbol.iterator](): Generator<V, void, unknown>; /** * Swap item with tail and remove it from map. * @param key key. * @param callback Calls callback when item swapped with tail. Arguments are reference of each item, * and 'key' is the key of map that indicates array index(pointer), * therefore should be careful to not modify 'key' or it could make mangling pointer. */ delete(key: U, callback?: (swapped: MapSource<U, V>, deleted: MapSource<U, V>) => any): boolean; /** * Clear map. */ clear(): void; /** * Swap item without preserve its order. * Equivalent to `swapPointer` + `swapOrder`, but more simple & efficient. * @param key1 key 1. * @param key2 key 2. */ swap(key1: U, key2: U): void; /** * Swap item with preserve its order. * @param key1 key 1. * @param key2 key 2. */ swapPointer(key1: U, key2: U): void; /** * Swap source order. * @param key1 key 1. * @param key2 key 2. */ swapIndex(key1: U, key2?: U): void; /** * Check if item exists. * @param key key. */ has(key: U): boolean; /** * Get item from map. * @param key key. */ get(key: U): V | undefined; /** * Get index(pointer) of item. * @param key key. */ getIndex(key: U): number | undefined; /** * Add item in map and return its index. * @param key key. * @param value value. */ set(key: U, value: V): number; /** * Pop out last item like array. */ pop(): MapSource<U, V> | undefined; /** * Reverse order of source array. */ reverse(): void; /** * shallow clone itself. Useful when use with immutable state management (ex. React state). */ clone(): VectorMap<U, V>; /** * Size of map. */ get size(): number; /** * insert data into target map. * @param target target map reference. */ insertInto(target: Map<U, V>): void; /** * set data from target map reference. * @param target target map reference. */ from(target: Map<U, V>): void; } //# sourceMappingURL=map.d.ts.map