76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
|
// https://github.com/mbostock/internmap/ v2.0.3 Copyright 2021 Mike Bostock
|
||
|
(function (global, factory) {
|
||
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.internmap = {}));
|
||
|
}(this, (function (exports) { 'use strict';
|
||
|
|
||
|
class InternMap extends Map {
|
||
|
constructor(entries, key = keyof) {
|
||
|
super();
|
||
|
Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
|
||
|
if (entries != null) for (const [key, value] of entries) this.set(key, value);
|
||
|
}
|
||
|
get(key) {
|
||
|
return super.get(intern_get(this, key));
|
||
|
}
|
||
|
has(key) {
|
||
|
return super.has(intern_get(this, key));
|
||
|
}
|
||
|
set(key, value) {
|
||
|
return super.set(intern_set(this, key), value);
|
||
|
}
|
||
|
delete(key) {
|
||
|
return super.delete(intern_delete(this, key));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class InternSet extends Set {
|
||
|
constructor(values, key = keyof) {
|
||
|
super();
|
||
|
Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
|
||
|
if (values != null) for (const value of values) this.add(value);
|
||
|
}
|
||
|
has(value) {
|
||
|
return super.has(intern_get(this, value));
|
||
|
}
|
||
|
add(value) {
|
||
|
return super.add(intern_set(this, value));
|
||
|
}
|
||
|
delete(value) {
|
||
|
return super.delete(intern_delete(this, value));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function intern_get({_intern, _key}, value) {
|
||
|
const key = _key(value);
|
||
|
return _intern.has(key) ? _intern.get(key) : value;
|
||
|
}
|
||
|
|
||
|
function intern_set({_intern, _key}, value) {
|
||
|
const key = _key(value);
|
||
|
if (_intern.has(key)) return _intern.get(key);
|
||
|
_intern.set(key, value);
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
function intern_delete({_intern, _key}, value) {
|
||
|
const key = _key(value);
|
||
|
if (_intern.has(key)) {
|
||
|
value = _intern.get(key);
|
||
|
_intern.delete(key);
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
function keyof(value) {
|
||
|
return value !== null && typeof value === "object" ? value.valueOf() : value;
|
||
|
}
|
||
|
|
||
|
exports.InternMap = InternMap;
|
||
|
exports.InternSet = InternSet;
|
||
|
|
||
|
Object.defineProperty(exports, '__esModule', { value: true });
|
||
|
|
||
|
})));
|