UNPKG

@grandlinex/react-components

Version:
98 lines (97 loc) 2.19 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); /** * Map extension */ class CMap { constructor(arr) { this[_a] = 'CoreMap'; this.store = new Map(arr); this.size = this.store.size; } updateSize() { this.size = this.store.size; } clear() { this.store.clear(); this.updateSize(); } delete(key) { const x = this.store.delete(key); this.updateSize(); return x; } forEach(callbackfn, thisArg) { this.store.forEach(callbackfn, thisArg); } get(key) { return this.store.get(key); } has(key) { return this.store.has(key); } set(key, value) { this.store.set(key, value); this.updateSize(); return this; } entries() { return this.store.entries(); } keys() { return this.store.keys(); } values() { return this.store.values(); } [Symbol.iterator]() { return this.store[Symbol.iterator](); } /** * Return a array based on each element in storage * @param mapper mapping function */ map(mapper) { const out = []; this.forEach((v, k) => { out.push(mapper(v, k)); }); return out; } /** * Return an array with the values only */ toValueArray() { return this.map((v) => v); } /** * Return an array with the keys only */ toKeyArray() { return this.map((v, k) => k); } /** * Delete selected elements from the storage */ deleteSelected(selector) { this.forEach((v, k) => { if (selector(v, k)) { this.delete(k); } }); } /** * Merge another core-map in the existing, duplicated keys in the executing map will be overwritten */ merge(map, skipOverwrite = false) { map.forEach((value, key) => { if (!(skipOverwrite && this.has(key))) { this.set(key, value); } }); return this; } } _a = Symbol.toStringTag; exports.default = CMap;