UNPKG

@yookue/ts-multi-map

Version:

Multiple key/value map & range map for typescript

531 lines (529 loc) 13.5 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/MultiKeyMap.ts var MultiKeyMap_exports = {}; __export(MultiKeyMap_exports, { MultiKeyMap: () => MultiKeyMap }); module.exports = __toCommonJS(MultiKeyMap_exports); var import_object_hash = __toESM(require("object-hash")); var import__ = require(".."); var MultiKeyMap = class { /** * Construct a multi key map instance * * @param entries the map entries that represented as [K[], V][] * * @example * ```ts * const map = new MultiKeyMap([ * [['row1', 'col1'], 'foo'] * ]); * ``` */ constructor(entries) { this.keyMap = new import__.MultiValueMap(); this.valueMap = /* @__PURE__ */ new Map(); entries == null ? void 0 : entries.forEach((entry) => { const [ks, v] = entry; this.set(ks, v); }); } /** * Construct a multi key map instance * * @param entries the map entries that represented as [K[], V][] * * @returns a multi key map instance * * @example * ```ts * const map = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * ``` */ static of(entries) { return new MultiKeyMap(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 = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.get(['row1', 'col1']); // 'foo' * map.get(['row2', 'col2'], 'bar'); // 'bar' * ``` */ get(keys, defaults) { if (keys.length == 0 || this.isEmpty()) { return defaults; } const hash = (0, import_object_hash.default)(keys); return this.valueMap.get(hash) ?? defaults; } /** * Sets the value of the given keys * * @param keys the keys to set * @param value the value to set * * @example * ```ts * map.set(['row1', 'col1'], 'bar'); * ``` */ set(keys, value) { if (keys.length == 0) { return; } const hash = (0, import_object_hash.default)(keys); this.keyMap.set(hash, [...keys]); this.valueMap.set(hash, value); } /** * Clears the map */ clear() { this.keyMap.clear(); this.valueMap.clear(); } /** * Returns the keys array of the map * * @returns the keys array of the map */ keys() { return [...this.keyMap.values()]; } /** * Returns the values of the map * * @returns the values of the map */ values() { return [...this.valueMap.values()]; } /** * Returns the keys/value entries of the map * * @returns the keys/value entries of the map */ entries() { if (this.isEmpty()) { return []; } if (this.keyMap.size !== this.valueMap.size) { throw EvalError(`Internal maps size mismatch!`); } const result = []; for (const [k, v] of this.valueMap) { result.push([this.keyMap.get(k) ?? [], v]); } return result; } /** * Deletes the entry with the given key * * @param key the key to delete * * @returns whether the entry has been deleted * * @example * ```ts * map.deleteByKey(['row1', 'col1']); * ``` */ deleteByKey(key) { if (!key.length || this.isEmpty()) { return false; } const hash = (0, import_object_hash.default)(key); return this.keyMap.deleteByKey(hash) && this.valueMap.delete(hash); } /** * Deletes all the entries with the given keys * * @param keys the keys to delete * * @returns whether any of the entries has been deleted * * @example * ```ts * map.deleteByKey([['row1', 'col1'], ['row2', 'col2']]); * ``` */ deleteByKeys(keys) { if (!keys.length || this.isEmpty()) { return false; } let result = false; for (const key of keys) { if (this.deleteByKey(key)) { result = true; } } return result; } /** * Deletes the entry/entries with the given value * * @param value the value to delete * * @returns whether the entry/entries has been deleted * * @example * ```ts * map.deleteByValue('foo'); * ``` */ deleteByValue(value) { if (this.isEmpty()) { return false; } let result = false; for (const [k, v] of this.valueMap.entries()) { if (v === value && this.keyMap.deleteByKey(k) && this.valueMap.delete(k)) { result = true; } } return result; } /** * Deletes all the entries with any of the given values * * @param values the values to delete * * @returns whether any of the entries has been deleted * * @example * ```ts * map.deleteByValues(['foo', 'bar']); * ``` */ deleteByValues(values) { if (!values.length || this.isEmpty()) { return false; } let result = false; for (const value of values) { if (this.deleteByValue(value)) { result = true; } } return result; } /** * 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.entries().forEach((entry) => { const [ks, v] = entry; callback(v, ks); }, 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) { let index = 0; this.entries().forEach((entry) => { const [ks, v] = entry; callback(v, ks, index++); }, 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.entries().forEach((entry) => { const [ks, v] = entry; if (!callback(v, ks)) { return; } }, 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.isNotEmpty() && keys.length > 0 && this.keyMap.hasValue(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 = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasKeyValue(['row1', 'col1'], 'foo'); // true * map.hasKeyValue(['row1', 'col1'], 'bar'); // false * ``` */ hasKeyValue(keys, value) { return this.isNotEmpty() && keys.length > 0 && this.get(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 = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasAnyKeys([['row1', 'col1'], ['row2', 'col2']]); // true * ``` */ hasAnyKeys(keys, exact = true) { return this.isNotEmpty() && keys.length > 0 && keys.some((item) => this.hasKey(item, 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 = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasAllKeys([['row1', 'col1'], ['row2', 'col2']]); // false * ``` */ hasAllKeys(keys, exact = true) { return this.isNotEmpty() && keys.length > 0 && keys.every((item) => this.hasKey(item, 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 = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasValue('foo'); // true * map.hasValue('bar'); // false * ``` */ hasValue(value) { return this.values().includes(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 = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'] * ]); * map.hasAnyValues(['foo', 'bar']); // true * ``` */ hasAnyValues(values) { return this.isNotEmpty() && values.length > 0 && values.some((item) => this.hasValue(item)); } /** * 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']); * ``` */ hasAllValues(values) { return this.isNotEmpty() && values.length > 0 && values.every((item) => this.hasValue(item)); } /** * Returns whether the map is empty * * @returns whether the map is empty */ isEmpty() { return !this.valueMap.size; } /** * Returns whether the map is not empty * * @returns whether the map is not empty */ isNotEmpty() { return this.valueMap.size > 0; } /** * 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) { if (this.isEmpty()) { return defaults; } for (const [k, v] of this.valueMap.entries()) { if (v === value) { return this.keyMap.get(k, defaults); } } return 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.entries()[Symbol.iterator](); } /** * Returns the size of map * * @returns the size of map */ get size() { return this.valueMap.size; } /** * Returns the string representation of the map identifier ('MultiKeyMap') * * @returns the string representation of the map identifier */ get [Symbol.toStringTag]() { return "MultiKeyMap"; } /** * Returns the string representation of the map elements * * @returns the string representation of the map elements * * @example * ```ts * const map = MultiKeyMap.of([ * [['row1', 'col1'], 'foo'], * [['row2', 'col2'], 'bar'] * ]); * console.log(map.toString()); // '[row1,col1]:foo;[row2,col2]:bar' * ``` */ toString() { return [...this].map((entry) => { const [ks, v] = entry.length <= 1 ? [[], entry[0]] : [entry.slice(0, -1), entry[entry.length - 1]]; return `[${ks.join()}]:${v}`; }).join(";"); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MultiKeyMap });