hd-utils
Version:
A handy utils for modern JS developers
63 lines (62 loc) • 3.34 kB
TypeScript
/**
* @description HMap is an extension of Map object, that adds more functionalities to the Map object, such as .from, .filter, .map, .mapFields and others.
* its great utility to deal with Map and normal JS objects.
* @example HMap.from({ a: 1, b: 2 }).mapArray((val) => val); // [ 1, 2 ]
*/
export default class HMap<K extends string | symbol, V> extends Map<K, V> {
/**
* @description takes a normal js object and reruns HMap instance.
* @example const hmap = HMap.from({a:1}).
*/
static from<K extends string | symbol, V>(obj: Record<K, V>): HMap<K, V>;
/**
* @description similar to [].filter() it takes a callback with key value and reruns the keys and values the condition is applied to them.
* @example HMap.from({a:1}).filter((v, k) => v === 1).toObject() // {a:1}
*/
filter: (callback: (value: V, key: K, map: HMap<K, V>) => boolean) => HMap<K, V>;
/**
* @description will behave like .map in arrays where you can change the keys and the values, will return a new HMap object.
* @example HMap.from({a:1}).mapFields((v, k) => ([v + 1, "b"])).toObject() // {b:2}
*/
mapFields: <U extends string | symbol>(callback: (value: V, key: K, map: HMap<K, V>) => [V, U]) => HMap<U, V>;
/**
* @description will behave like .map in arrays, but you can't change the keys but you can change the values, will return a new HMap object.
* @example HMap.from({a:1}).mapFields((v, k) => ([v + 1, "b"])).toObject() // { a: [ 2, 'b' ] }
*/
map: <U>(callback: (value: V, key: K, map: HMap<K, V>) => U) => HMap<K, U>;
/**
* @description will behave like .map in arrays, but you can't change the keys but you can change the values, will return a new HMap object.
* @example HMap.from({a:1, b:2}).mapArray((val) => (val)) // [ 1, 2 ]
*/
mapArray: <U>(callback: (value: V, key: K, map: HMap<K, V>) => U) => U[];
/**
* @description will return the value based on a callback that returns a boolean
* @example HMap.from({a:1}).findValue((val, key) => val > 0) // 1;
*/
findValue: (callback: (value: V, key: K, map: HMap<K, V>) => boolean) => V | undefined;
/**
* @description acts like [].every(), where you provide a function that takes the key and value and return boolean if the condition is applied on every.
* @example HMap.from({a:1}).every((val, key) => val === 1) // true
*/
every: (callback: (value: V, key: K, map: HMap<K, V>) => boolean) => boolean;
/**
* @description will get all of the HMap object keys in an array.
* @example HMap.from({a:1}).getAllKeys() // ["a"]
*/
getAllKeys: () => K[];
/**
* @description acts like [].some(), where you provide a function that takes the key and value and return boolean if the condition is applied on some.
* @example HMap.from({a:1}).some((val, key) => val === 1) // true
*/
some: (callback: (value: V, key: K, map: HMap<K, V>) => boolean) => boolean;
/**
* @description will get all of the HMap object values in an array.
* @example HMap.from({a:1}).getAllVAlues() // [1]
*/
getAllValues: () => V[];
/**
* @description will convert the HMap to normal JS object.
* @example HMap.from({a:1}).toObject() // {a:1}
*/
toObject: () => Record<K, V>;
}