mild-map 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -6,5 +6,10 @@ declare class MildMap<K, V> {
6
6
  get(key: K): V | undefined;
7
7
  has(key: K): boolean;
8
8
  set(key: K, value: V): this;
9
+ [Symbol.iterator](): IterableIterator<[K, V]>;
10
+ keys(): IterableIterator<K>;
11
+ values(): IterableIterator<V>;
12
+ entries(): IterableIterator<[K, V]>;
13
+ forEach(callback: (value: V, key: K, map: MildMap<K, V>) => void, thisArg?: any): undefined;
9
14
  }
10
15
  export default MildMap;
package/dist/index.js CHANGED
@@ -5,9 +5,14 @@ class MildMap {
5
5
  /* VARIABLES */
6
6
  #strong = new Map();
7
7
  #weak = new WeakMap();
8
+ #weakRefs = new Set();
9
+ #weakRefsMap = new WeakMap();
8
10
  #size = 0;
9
- #finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
10
11
  #finalizationTokens = new WeakMap();
12
+ #finalizationRegistry = new FinalizationRegistry(weakRef => {
13
+ this.#size -= 1;
14
+ this.#weakRefs.delete(weakRef);
15
+ });
11
16
  /* CONSTRUCTOR */
12
17
  constructor(entries) {
13
18
  if (entries) {
@@ -31,6 +36,11 @@ class MildMap {
31
36
  if (token) {
32
37
  this.#finalizationRegistry.unregister(token);
33
38
  this.#finalizationTokens.delete(key);
39
+ const weakRef = this.#weakRefsMap.get(key);
40
+ if (weakRef) {
41
+ this.#weakRefs.delete(weakRef);
42
+ this.#weakRefsMap.delete(key);
43
+ }
34
44
  }
35
45
  return this.#weak.delete(key);
36
46
  }
@@ -62,8 +72,11 @@ class MildMap {
62
72
  if (isWeakReferable(key)) {
63
73
  this.#weak.set(key, value);
64
74
  if (!hasKey) {
75
+ const weakRef = new WeakRef(key);
65
76
  const token = {};
66
- this.#finalizationRegistry.register(key, token, token);
77
+ this.#weakRefs.add(weakRef);
78
+ this.#weakRefsMap.set(key, weakRef);
79
+ this.#finalizationRegistry.register(key, weakRef, token);
67
80
  this.#finalizationTokens.set(key, token);
68
81
  }
69
82
  }
@@ -72,6 +85,44 @@ class MildMap {
72
85
  }
73
86
  return this;
74
87
  }
88
+ /* ITERATION API */
89
+ [Symbol.iterator]() {
90
+ return this.entries();
91
+ }
92
+ *keys() {
93
+ yield* this.#strong.keys();
94
+ for (const weakRef of this.#weakRefs) {
95
+ const key = weakRef.deref();
96
+ if (key === undefined)
97
+ continue;
98
+ yield key;
99
+ }
100
+ }
101
+ *values() {
102
+ yield* this.#strong.values();
103
+ for (const weakRef of this.#weakRefs) {
104
+ const key = weakRef.deref();
105
+ if (key === undefined || !this.#weak.has(key))
106
+ continue;
107
+ const value = this.#weak.get(key);
108
+ yield value;
109
+ }
110
+ }
111
+ *entries() {
112
+ yield* this.#strong.entries();
113
+ for (const weakRef of this.#weakRefs) {
114
+ const key = weakRef.deref();
115
+ if (key === undefined || !this.#weak.has(key))
116
+ continue;
117
+ const value = this.#weak.get(key);
118
+ yield [key, value];
119
+ }
120
+ }
121
+ forEach(callback, thisArg) {
122
+ for (const [key, value] of this.entries()) {
123
+ callback.call(thisArg, value, key, this);
124
+ }
125
+ }
75
126
  }
76
127
  /* EXPORT */
77
128
  export default MildMap;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": "github:fabiospampinato/mild-map",
4
4
  "description": "A WeakMap that supports any value, it holds strong references to primitives, and weak references to objects.",
5
5
  "license": "MIT",
6
- "version": "1.1.2",
6
+ "version": "1.2.0",
7
7
  "type": "module",
8
8
  "main": "dist/index.js",
9
9
  "exports": "./dist/index.js",
package/readme.md CHANGED
@@ -14,6 +14,8 @@ It has the same API of a WeakMap, except that it just works with primitives too.
14
14
 
15
15
  It also implements the `size` property, like a Map.
16
16
 
17
+ It also implements all the iteration methods, like a Map.
18
+
17
19
  ## License
18
20
 
19
21
  MIT © Fabio Spampinato