UNPKG

@yookue/ts-multi-map

Version:

Multiple key/value map & range map for typescript

377 lines (375 loc) 9.83 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/ReadonlyRangeMap.ts var ReadonlyRangeMap_exports = {}; __export(ReadonlyRangeMap_exports, { ReadonlyRangeMap: () => ReadonlyRangeMap }); module.exports = __toCommonJS(ReadonlyRangeMap_exports); var import__ = require(".."); var ReadonlyRangeMap = class { /** * Construct a readonly range map instance * * @param entries the map entries * @param validation whether to compare the ranges to all the ranges previously, to determine if there are any conflicts * * @example * ```ts * const map = new ReadonlyRangeMap([ * [[1, 30], 'green'], * [[30, 60], 'blue'], * [[60, 90], 'orange'], * [[90, 100, true, true], 'red'] * ]); * ``` */ constructor(entries, validation = true) { this.map = new import__.RangeMap(); entries == null ? void 0 : entries.forEach((entry) => { const [k, v] = entry; this.map.set(k, v, validation); }); } /** * Construct a readonly range map instance * * @param entries the map entries * @param validation whether to compare the ranges to all the ranges previously, to determine if there are any conflicts * * @returns a readonly multi value map instance * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 30], 'green'], * [[30, 60], 'blue'], * [[60, 90], 'orange'], * [[90, 100, true, true], 'red'] * ]); * ``` */ static of(entries, validation = true) { return new ReadonlyRangeMap(entries, validation); } /** * Returns the value of the given key * * @param key the key to retrieve * @param defaults the default value if not found * * @returns the value of the given key * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 50, true, true], 'white'], * [[51, 100, false, true], 'black'] * ]); * map.get([1, 50, true, true]); // 'white' * map.get([51, 100, false, true]); // 'black' * ``` */ get(key, defaults) { return this.map.get(key, defaults); } /** * Returns the value that associated to the number, by determining which bound contains the given number * * @param digit the number to retrieve * @param defaults the default value if not found * * @returns the value that associated to the number, by determining which bound contains the given number * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 50, true, true], 'white'], * [[51, 100, false, true], 'black'] * ]); * map.get(25); // 'white' * map.get(200); // undefined * ``` */ getByDigit(digit, defaults) { return this.map.getByDigit(digit, defaults); } /** * Returns the keys of the map * * @returns the keys of the map */ keys() { return this.map.keys(); } /** * Returns the value array of the map * * @returns the value array of the map */ values() { return this.map.values(); } /** * Returns the key/value entries of the map * * @returns the key/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, key) => { * 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, key, 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, key) => { * return true; * }); * ``` */ forEachBreakable(callback, thisArg) { this.map.forEachBreakable(callback, thisArg); } /** * Returns whether the map contains the given key * * @param key the key to check * * @returns whether the map contains the given key * * @example * ```ts * map.hasKey([1, 30]); * ``` */ hasKey(key) { return this.map.hasKey(key); } /** * Returns whether the map contains the given key/value pair * * @param key the key to check * @param value the value to check * * @returns whether the map contains the given key/value pair * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 50], 'white'] * ]); * map.hasKeyValue([1, 50], 'white'); // true * map.hasKeyValue([1, 50], 'black'); // false * ``` */ hasKeyValue(key, value) { return this.map.hasKeyValue(key, value); } /** * Returns whether the map contains any of the given keys * * @param keys the keys to check * * @returns whether the map contains any of the given keys * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 50, true, true], 'white'], * [[51, 100, false, true], 'black'] * ]); * map.hasAnyKeys([[1, 50, true, true], [20, 60]]); // true * ``` */ hasAnyKeys(keys) { return this.map.hasAnyKeys(keys); } /** * Returns whether the map contains all the given keys * * @param keys the keys to check * * @returns whether the map contains all the given keys * * @example * ```ts * map.hasAllKeys([[1, 50], [51, 100]]); * ``` */ hasAllKeys(keys) { return this.map.hasAllKeys(keys); } /** * Returns whether any entries of the map that contains the given values * * @param value the value to check * * @returns whether any entries of the map that contains the given values * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 50, true, true], 'white'], * [[51, 100, false, true], 'black'] * ]); * map.hasValue('white'); // true * map.hasValue('blue'); // 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 = ReadonlyRangeMap.of([ * [[1, 50, true, true], 'white'], * [[51, 100, false, true], 'black'] * ]); * map.hasAnyValues(['red', 'black']); // true * map.hasAnyValues(['top', 'right']); // false * ``` */ hasAnyValues(values) { return this.map.hasAnyValues(values); } /** * Returns whether the map contains all the given values, matching exactly * * @param values the values to check * * @returns whether the map contains all the given values, matching exactly * * @example * ```ts * map.hasAllValues(['red', 'green', 'blue']); * ``` */ 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(); } /** * Response for returning the list of key/value to iterate * * @example * ```ts * for (const [key, 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 ('ReadonlyRangeMap') * * @returns the string representation of the map identifier */ get [Symbol.toStringTag]() { return "ReadonlyRangeMap"; } /** * Returns the string representation of the map elements * * @returns the string representation of the map elements * * @example * ```ts * const map = ReadonlyRangeMap.of([ * [[1, 50, true, true], 'white'], * [[51, 100, false, true], 'black'] * ]); * console.log(map.toString()); // '[start:1,end:50,startInclusive:true,endInclusive:true]:white;[start:51,end:100,startInclusive:false,endInclusive:true]:black' * ``` */ toString() { return this.map.toString(); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ReadonlyRangeMap });