@thi.ng/associative
Version:
ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
32 lines (31 loc) • 989 B
JavaScript
import { mixin } from "@thi.ng/api/mixin";
import { isNode } from "@thi.ng/checks/is-node";
import { map } from "@thi.ng/transducers/map";
let inspect = null;
isNode() && import("util").then((m) => {
inspect = m.inspect;
});
const __inspectSet = (coll, opts) => [...map((x) => inspect(x, opts), coll)].join(", ");
const __inspectMap = (coll, opts) => [
...map(
([k, v]) => `${inspect(k, opts)} => ${inspect(v, opts)}`,
coll
)
].join(", ");
const __inspectable = mixin({
[Symbol.for("nodejs.util.inspect.custom")](depth, opts) {
const name = this[Symbol.toStringTag];
const childOpts = {
...opts,
depth: opts.depth === null ? null : opts.depth - 1
};
return depth >= 0 ? [
`${name}(${this.size || 0}) {`,
inspect ? this instanceof Set ? __inspectSet(this, childOpts) : this instanceof Map ? __inspectMap(this, childOpts) : "" : "",
"}"
].join(" ") : opts.stylize(`[${name}]`, "special");
}
});
export {
__inspectable
};