UNPKG

@yookue/ts-multi-map

Version:

Multiple key/value map & range map for typescript

365 lines (363 loc) 9.13 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/ReadonlyMultiKeyMap.ts var ReadonlyMultiKeyMap_exports = {}; __export(ReadonlyMultiKeyMap_exports, { ReadonlyMultiKeyMap: () => ReadonlyMultiKeyMap }); module.exports = __toCommonJS(ReadonlyMultiKeyMap_exports); var import__ = require(".."); var ReadonlyMultiKeyMap = class { /** * Construct a readonly multi key map instance * * @param entries the map entries that represented as [K[], V][] * * @example * ```ts * const map = new ReadonlyMultiKeyMap([ * [['row1', 'col1'], 'foo'] * ]); * ``` */ constructor(entries) { this.map = new import__.MultiKeyMap(); entries == null ? void 0 : entries.forEach((entry) => { const [ks, v] = entry; this.map.set(ks, v); }); } /** * Construct a readonly multi key map instance * * @param entries the map entries that represented as [K[], V][] * * @returns a readonly multi key map instance * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * ``` */ static of(entries) { return new ReadonlyMultiKeyMap(entries); } /** * Returns the value of the given keys * * @param keys the keys to retrieve * @param defaults the default value if not found * * @returns the value of the given keys * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.get(['row1', 'col1']); // 'foo' * map.get(['row2', 'col2'], 'bar'); // 'bar' * ``` */ get(keys, defaults) { return this.map.get(keys, defaults); } /** * Returns the keys array of the map * * @returns the keys array of the map */ keys() { return this.map.keys(); } /** * Returns the values of the map * * @returns the values of the map */ values() { return this.map.values(); } /** * Returns the keys/value entries of the map * * @returns the keys/value entries of the map */ entries() { return this.map.entries(); } /** * Processes each entry in the map * * @param callback a callback function that processes each entry * @param thisArg any instance to retrieve 'this' reference in the callback function * * @example * ```ts * map.forEach((value, keys) => { * console.log(value); * }); * ``` */ forEach(callback, thisArg) { this.map.forEach(callback, thisArg); } /** * Processes each entry in the map with index capability * * @param callback a callback function that processes each entry * @param thisArg any instance to retrieve 'this' reference in the callback function * * @example * ```ts * map.forEachIndexing((value, keys, index) => { * console.log(index); * }); * ``` */ forEachIndexing(callback, thisArg) { this.map.forEachIndexing(callback, thisArg); } /** * Processes each entry in the map with breakable capability * * @param callback a callback function that processes each entry. Returning false indicates to break the map iteration * @param thisArg any instance to retrieve 'this' reference in the callback function * * @example * ```ts * map.forEachBreakable((value, keys) => { * return true; * }); * ``` */ forEachBreakable(callback, thisArg) { this.map.forEachBreakable(callback, thisArg); } /** * Returns whether the map contains the given keys * * @param keys the keys to check * @param exact whether matching entry values exactly * * @returns whether the map contains the given key * * @example * ```ts * map.hasKey(['row1', 'col1']); * ``` */ hasKey(keys, exact = true) { return this.map.hasKey(keys, exact); } /** * Returns whether the map contains the given keys/value pair * * @param keys the keys to check * @param value the value to check * * @returns whether the map contains the given keys/value pair * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasKeyValue(['row1', 'col1'], 'foo'); // true * map.hasKeyValue(['row1', 'col1'], 'bar'); // false * ``` */ hasKeyValue(keys, value) { return this.map.hasKeyValue(keys, value); } /** * Returns whether the map contains any of the given keys * * @param keys the keys to check * @param exact whether matching entry values exactly * * @returns whether the map contains any of the given keys * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasAnyKeys([['row1', 'col1'], ['row2', 'col2']]); // true * ``` */ hasAnyKeys(keys, exact = true) { return this.map.hasAnyKeys(keys, exact); } /** * Returns whether the map contains all the given keys * * @param keys the keys to check * @param exact whether matching entry values exactly * * @returns whether the map contains all the given keys * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasAllKeys([['row1', 'col1'], ['row2', 'col2']]); // false * ``` */ hasAllKeys(keys, exact = true) { return this.map.hasAllKeys(keys, exact); } /** * Returns whether the map contains the given value * * @param value the value to check * * @returns whether the map contains the given value * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasValue('foo'); // true * map.hasValue('bar'); // false * ``` */ hasValue(value) { return this.map.hasValue(value); } /** * Returns whether the map contains any of the given values * * @param values the values to check * * @returns whether the map contains any of the given values * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasAnyValues(['foo', 'bar']); // true * ``` */ hasAnyValues(values) { return this.map.hasAnyValues(values); } /** * Returns whether the map contains all the given values * * @param values the values to check * * @returns whether the map contains all the given values * * @example * ```ts * map.hasAllValues(['foo', 'bar']); * ``` */ hasAllValues(values) { return this.map.hasAllValues(values); } /** * Returns whether the map is empty * * @returns whether the map is empty */ isEmpty() { return this.map.isEmpty(); } /** * Returns whether the map is not empty * * @returns whether the map is not empty */ isNotEmpty() { return this.map.isNotEmpty(); } /** * Returns the keys with the given value * * @param value the value to inspect * @param defaults the default keys if nothing matches the given value * * @returns the keys with the given value * * @example * ```ts * map.getKey('bar'); * ``` */ getKey(value, defaults) { return this.map.getKey(value, defaults); } /** * Response for returning the list of keys/value to iterate * * @example * ```ts * for (const [keys, value] of map) { * console.log(value); * } * ``` */ // @ts-ignore [Symbol.iterator]() { return this.map[Symbol.iterator](); } /** * Returns the size of map * * @returns the size of map */ get size() { return this.map.size; } /** * Returns the string representation of the map identifier ('ReadonlyMultiKeyMap') * * @returns the string representation of the map identifier */ get [Symbol.toStringTag]() { return "ReadonlyMultiKeyMap"; } /** * Returns the string representation of the map elements * * @returns the string representation of the map elements * * @example * ```ts * const map = ReadonlyMultiKeyMap.of([ * [['row1', 'col1'], 'foo'], * [['row2', 'col2'], 'bar'] * ]); * console.log(map.toString()); // '[row1,col1]:foo;[row2,col2]:bar' * ``` */ toString() { return this.map.toString(); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ReadonlyMultiKeyMap });