UNPKG

1.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.InternMap = void 0;
4function internGet({ map, initKey }, value) {
5 const key = initKey(value);
6 return map.has(key) ? map.get(key) : value;
7}
8function internSet({ map, initKey }, value) {
9 const key = initKey(value);
10 if (map.has(key))
11 return map.get(key);
12 map.set(key, value);
13 return value;
14}
15function internDelete({ map, initKey }, value) {
16 const key = initKey(value);
17 if (map.has(key)) {
18 value = map.get(key);
19 map.delete(key);
20 }
21 return value;
22}
23function keyof(value) {
24 return typeof value === 'object' ? value.valueOf() : value;
25}
26/**
27 * @see 参考 https://github.com/mbostock/internmap/blob/main/src/index.js
28 */
29class InternMap extends Map {
30 constructor(entries) {
31 super();
32 this.map = new Map();
33 this.initKey = keyof;
34 if (entries !== null) {
35 for (const [key, value] of entries) {
36 this.set(key, value);
37 }
38 }
39 }
40 get(key) {
41 return super.get(internGet({ map: this.map, initKey: this.initKey }, key));
42 }
43 has(key) {
44 return super.has(internGet({ map: this.map, initKey: this.initKey }, key));
45 }
46 set(key, value) {
47 return super.set(internSet({ map: this.map, initKey: this.initKey }, key), value);
48 }
49 delete(key) {
50 return super.delete(internDelete({ map: this.map, initKey: this.initKey }, key));
51 }
52}
53exports.InternMap = InternMap;
54//# sourceMappingURL=internMap.js.map
\No newline at end of file