@yookue/ts-multi-map
Version:
Multiple key/value map & range map for typescript
366 lines (364 loc) • 9.58 kB
JavaScript
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/ReadonlyMultiValueMap.ts
var ReadonlyMultiValueMap_exports = {};
__export(ReadonlyMultiValueMap_exports, {
ReadonlyMultiValueMap: () => ReadonlyMultiValueMap
});
module.exports = __toCommonJS(ReadonlyMultiValueMap_exports);
var import__ = require("..");
var ReadonlyMultiValueMap = class {
/**
* Construct a readonly multi value map instance
*
* @param entries the map entries that represented as [K, V[]][]
*
* @example
* ```ts
* const map = new ReadonlyMultiValueMap([
* ['color', ['red', 'green', 'blue']]
* ]);
* ```
*/
constructor(entries) {
this.map = new import__.MultiValueMap();
entries == null ? void 0 : entries.forEach((entry) => {
const [k, vs] = entry;
this.map.set(k, vs);
});
}
/**
* Construct a readonly multi value map instance
*
* @param entries the map entries that represented as [K, V[]][]
*
* @returns a readonly multi value map instance
*
* @example
* ```ts
* const map = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']]
* ]);
* ```
*/
static of(entries) {
return new ReadonlyMultiValueMap(entries);
}
/**
* Returns the values of the given key
*
* @param key the key to retrieve
* @param defaults the default values if not found
*
* @returns the values of the given key
*
* @example
* ```ts
* const map = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']]
* ]);
* map.get('color'); // ['red', 'green', 'blue']
* map.get('foobar', ['foo', 'bar']); // ['foo', 'bar']
* ```
*/
get(key, defaults) {
return this.map.get(key, defaults);
}
/**
* Returns the keys of the map
*
* @returns the keys of the map
*/
keys() {
return this.map.keys();
}
/**
* Returns the values array of the map
*
* @returns the values array of the map
*/
values() {
return this.map.values();
}
/**
* Returns the key/values entries of the map
*
* @returns the key/values 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((values, key) => {
* console.log(key);
* });
* ```
*/
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((values, 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((values, 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('color');
* ```
*/
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 = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']]
* ]);
* map.hasKeyValue('color', 'red'); // true
* map.hasKeyValue('color', '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 = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']]
* ]);
* map.hasAnyKeys(['color', 'position']); // 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(['color', 'position']);
* ```
*/
hasAllKeys(keys) {
return this.map.hasAllKeys(keys);
}
/**
* Returns whether any entries of the map that contains the given values
*
* @param values the values to check
* @param exact whether matching entry values exactly
*
* @returns whether any entries of the map that contains the given values
*
* @example
* ```ts
* const map = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']],
* ['position', ['top', 'right', 'bottom', 'left']]
* ]);
* map.hasValue(['red', 'black'], true); // false
* map.hasValue(['top', 'right'], true); // false
* map.hasValue(['top', 'right'], false); // true
* ```
*/
hasValue(values, exact = true) {
return this.map.hasValue(values, exact);
}
/**
* Returns whether the map contains any of the given values
*
* @param values the values to check
* @param exact whether matching entry values exactly
*
* @returns whether the map contains any of the given values
*
* @example
* ```ts
* const map = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']],
* ['position', ['top', 'right', 'bottom', 'left']]
* ]);
* map.hasAnyValues([['red', 'black'], ['green', 'blue']]); // false
* map.hasAnyValues([['top', 'right'], ['top', 'right', 'bottom', 'left']]); // true
* ```
*/
hasAnyValues(values, exact = true) {
return this.map.hasAnyValues(values, exact);
}
/**
* Returns whether the map contains all the given values
*
* @param values the values to check
* @param exact whether matching entry values exactly
*
* @returns whether the map contains all the given values
*
* @example
* ```ts
* map.hasAllValues([['red', 'green', 'blue'], ['top', 'right', 'bottom', 'left']]);
* ```
*/
hasAllValues(values, exact = true) {
return this.map.hasAllValues(values, exact);
}
/**
* 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 key with the given values
*
* @param values the values to inspect
* @param defaults the default key if nothing matches the given value
*
* @returns the key with the given value
*
* @example
* ```ts
* map.getKey(['foo', 'bar']);
* ```
*/
getKey(values, defaults) {
return this.map.getKey(values, defaults);
}
/**
* Response for returning the list of key/values to iterate
*
* @example
* ```ts
* for (const [key, values] of map) {
* console.log(key);
* }
* ```
*/
// @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 ('ReadonlyMultiValueMap')
*
* @returns the string representation of the map identifier
*/
get [Symbol.toStringTag]() {
return "ReadonlyMultiValueMap";
}
/**
* Returns the string representation of the map elements
*
* @returns the string representation of the map elements
*
* @example
* ```ts
* const map = ReadonlyMultiValueMap.of([
* ['color', ['red', 'green', 'blue']],
* ['position', ['top', 'right', 'bottom', 'left']]
* ]);
* console.log(map.toString()); // 'color:[red,green,blue];position:[top,right,bottom,left]'
* ```
*/
toString() {
return this.map.toString();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ReadonlyMultiValueMap
});