@typescript-package/map
Version:
A lightweight TypeScript library for enhanced `map` management.
162 lines (161 loc) • 5.99 kB
TypeScript
import { DataCore, Data, DataConstructorInput, MapTypeConstructor } from '@typescript-package/data';
import { CoreMap } from './core-map.abstract';
/**
* @description
* @export
* @class FactoryMap
* @template Key
* @template Value
* @template {Map<Key, Value>} [MapType=Map<Key, Value>]
* @template {DataCore<MapType>} [DataType=Data<MapType>]
* @extends {CoreMap<Key, Value, MapType, DataType>}
*/
export declare class FactoryMap<Key, Value, MapType extends Map<Key, Value> = Map<Key, Value>, DataType extends DataCore<MapType> = Data<MapType>> extends CoreMap<Key, Value, MapType, DataType> {
#private;
/**
* @description
* @public
* @static
* @template {PropertyKey} Key
* @template Value
* @template {Map<Key, Value>} [MapType=Map<Key, Value>]
* @template {DataCore<MapType>} [DataType=Data<MapType>]
* @param {Record<Key, Value>} obj
* @param {?MapTypeConstructor<Key, Value, MapType>} [map]
* @param {?DataConstructorInput<MapType, DataType>} [data]
* @param {{
* defaultValue?: () => Value;
* cloner?: (value: Value) => Value;
* }} [param0={}]
* @param {() => Value} param0.defaultValue
* @param {(value: Value) => Value} param0.cloner
* @returns {FactoryMap<Key, Value, MapType, DataType>}
*/
static fromObject<Key extends PropertyKey, Value, MapType extends Map<Key, Value> = Map<Key, Value>, DataType extends DataCore<MapType> = Data<MapType>>(obj: Record<Key, Value>, map?: MapTypeConstructor<Key, Value, MapType>, data?: DataConstructorInput<MapType, DataType>, { defaultValue, cloner }?: {
defaultValue?: () => Value;
cloner?: (value: Value) => Value;
}): FactoryMap<Key, Value, MapType, DataType>;
/**
* @description Returns the `string` tag representation of the `FactoryMap` class when used in `Object.prototype.toString.call(instance)`.
* @public
* @readonly
*/
get [Symbol.toStringTag](): string;
/**
* @description Returns the privately stored data class.
* @public
* @readonly
* @type {DataType}
*/
get data(): DataType;
/**
* @description Returns the default value.
* @public
* @readonly
* @type {Value | undefined}
*/
get defaultValue(): Value | undefined;
/**
* Creates an instance of `FactoryMap`.
* @constructor
* @param {?[Key, Value][]} [entries]
* @param {?MapTypeConstructor<Key, Value, MapType>} [map]
* @param {?DataConstructorInput<MapType, DataType>} [data]
* @param {{
* cloner?: (value: Value) => Value,
* comparator?: ((a: [Key, Value], b: [Key, Value]) => number),
* defaultValue?: () => Value,
* ordered?: boolean;
* }} [param0={}]
* @param {(value: Value) => Value} param0.cloner
* @param {(a: [Key, Value], b: [Key, Value]) => number} param0.comparator
* @param {() => Value} param0.defaultValue
* @param {boolean} param0.ordered
*/
constructor(entries?: [Key, Value][], map?: MapTypeConstructor<Key, Value, MapType>, data?: DataConstructorInput<MapType, DataType>, { cloner, comparator, defaultValue, ordered }?: {
cloner?: (value: Value) => Value;
comparator?: ((a: [Key, Value], b: [Key, Value]) => number);
defaultValue?: () => Value;
ordered?: boolean;
});
/**
* @inheritdoc
* @public
* @param {Key} key
* @returns {(Value | undefined)}
*/
get(key: Key): Value | undefined;
/**
* @description Gets the cloner function, to use for e.g. `structuredClone`.
* @public
* @returns {((value: Value) => Value) | undefined}
*/
getCloner(): ((value: Value) => Value) | undefined;
/**
* @description Returns the default value function.
* @public
* @returns {(() => Value) | undefined}
*/
getDefaultValue(): (() => Value) | undefined;
/**
* @inheritdoc
* @public
* @param {Key} key
* @param {Value} value
* @returns {this}
*/
set(key: Key, value: Value): this;
/**
* @description Sets the cloner function, to use for e.g. `structuredClone`.
* @public
* @param {(value: Value) => Value} clonerFn
* @returns {this} The current instance of `FactoryMap`.
* @example
* ```ts
* const map = new FactoryMap<string, number>();
* map.setCloner((value) => structuredClone(value));
* console.log(map.get('key')); // undefined
* map.set('key', { a: 1 });
* console.log(map.get('key')); // { a: 1 }
* map.set('key', { a: 2 });
* console.log(map.get('key')); // { a: 2 }
* ```
*/
setCloner(clonerFn: (value: Value) => Value): this;
/**
* @description Sets the compare function used in sorting.
* @public
* @param {(a: [Key, Value], b: [Key, Value]) => number} compareFn
* @returns {this}
*/
setComparator(compareFn: (a: [Key, Value], b: [Key, Value]) => number): this;
/**
* @description Sets the default value function.
* @public
* @param {() => Value} valueFn
* @returns {this} The current instance of `FactoryMap`.
* @example
* ```ts
* const map = new FactoryMap<string, number>();
* map.setDefaultValue(() => 0);
* console.log(map.get('key')); // 0
* map.set('key', 1);
* console.log(map.get('key')); // 1
* ```
*/
setDefaultValue(valueFn: () => Value): this;
/**
* @description Sets whether the map should sort automatically after each `set`.
* @public
* @param {boolean} ordered
* @returns {this}
*/
setOrdered(ordered: boolean): this;
/**
* @description Sorts the map with a stored or given comparator.
* @public
* @param {(a: [Key, Value], b: [Key, Value]) => number} [compareFn=this.#comparator]
* @returns {this}
*/
sort(compareFn?: (a: [Key, Value], b: [Key, Value]) => number): this;
}