UNPKG

@thi.ng/associative

Version:

ES Map/Set-compatible implementations with customizable equality semantics & supporting operations

160 lines (159 loc) 4.99 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __typeError = (msg) => { throw TypeError(msg); }; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); var _keys, _map, _opts; import { SEMAPHORE } from "@thi.ng/api/api"; import { isPlainObject } from "@thi.ng/checks/is-plain-object"; import { equiv } from "@thi.ng/equiv"; import { pairs } from "@thi.ng/transducers/pairs"; import { ArraySet } from "./array-set.js"; import { dissoc } from "./dissoc.js"; import { __disposableEntries } from "./internal/dispose.js"; import { __equivMap } from "./internal/equiv.js"; import { __tostringMixin } from "./internal/tostring.js"; import { into } from "./into.js"; let EquivMap = class extends Map { /** * Creates a new instance with optional initial key-value pairs and provided * options. If no `opts` are given, uses `ArraySet` for storing canonical * keys and * [`equiv`](https://docs.thi.ng/umbrella/equiv/functions/equiv.html) for * checking key equivalence. * * @param pairs - key-value pairs * @param opts - config options */ constructor(pairs2, opts) { super(); __privateAdd(this, _keys); __privateAdd(this, _map); __privateAdd(this, _opts); const _opts2 = { equiv, keys: ArraySet, ...opts }; __privateSet(this, _keys, new _opts2.keys(null, { equiv: _opts2.equiv })); __privateSet(this, _map, /* @__PURE__ */ new Map()); __privateSet(this, _opts, _opts2); if (pairs2) { this.into(pairs2); } } [Symbol.iterator]() { return this.entries(); } // mixin [Symbol.dispose]() { } get [Symbol.species]() { return EquivMap; } get [Symbol.toStringTag]() { return "EquivMap"; } get size() { return __privateGet(this, _keys).size; } clear() { __privateGet(this, _keys).clear(); __privateGet(this, _map).clear(); } empty() { return new EquivMap(null, __privateGet(this, _opts)); } copy() { const m = new EquivMap(); __privateSet(m, _keys, __privateGet(this, _keys).copy()); __privateSet(m, _map, new Map(__privateGet(this, _map))); __privateSet(m, _opts, __privateGet(this, _opts)); return m; } equiv(o) { return __equivMap(this, o); } delete(key) { const $key = __privateGet(this, _keys).get(key, SEMAPHORE); if ($key !== SEMAPHORE) { __privateGet(this, _map).delete($key); __privateGet(this, _keys).delete($key); return true; } return false; } dissoc(keys) { return dissoc(this, keys); } /** * The key & value args given the callback `fn` MUST be treated as * readonly/immutable. This could be enforced via TS, but would * break ES6 Map interface contract. * * @param fn - * @param thisArg - */ forEach(fn, thisArg) { for (let pair of __privateGet(this, _map)) { fn.call(thisArg, pair[1], pair[0], this); } } get(key, notFound) { const $key = __privateGet(this, _keys).get(key, SEMAPHORE); return $key !== SEMAPHORE ? __privateGet(this, _map).get($key) : notFound; } has(key) { return __privateGet(this, _keys).has(key); } set(key, value) { const $key = __privateGet(this, _keys).get(key, SEMAPHORE); if ($key !== SEMAPHORE) { __privateGet(this, _map).set($key, value); } else { __privateGet(this, _keys).add(key); __privateGet(this, _map).set(key, value); } return this; } into(pairs2) { return into(this, pairs2); } entries() { return __privateGet(this, _map).entries(); } keys() { return __privateGet(this, _map).keys(); } values() { return __privateGet(this, _map).values(); } opts() { return __privateGet(this, _opts); } }; _keys = new WeakMap(); _map = new WeakMap(); _opts = new WeakMap(); EquivMap = __decorateClass([ __disposableEntries, __tostringMixin ], EquivMap); function defEquivMap(src, opts) { return new EquivMap( isPlainObject(src) ? pairs(src) : src, opts ); } export { EquivMap, defEquivMap };