mild-map 1.0.0 → 1.1.1
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 +1 -0
- package/dist/index.js +37 -5
- package/package.json +6 -3
- package/readme.md +2 -0
- package/src/index.ts +55 -5
- package/test/index.js +9 -0
- package/dist/constants.d.ts +0 -2
- package/dist/constants.js +0 -12
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -11
- package/src/constants.ts +0 -22
- package/src/utils.ts +0 -20
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
1
|
/* IMPORT */
|
|
2
|
-
import {
|
|
2
|
+
import { isWeakReferable } from 'is';
|
|
3
|
+
/* HELPERS */
|
|
4
|
+
const isWeakabe = (value) => {
|
|
5
|
+
return isWeakReferable(value);
|
|
6
|
+
};
|
|
3
7
|
/* MAIN */
|
|
4
8
|
class MildMap {
|
|
5
9
|
/* VARIABLES */
|
|
6
10
|
#strong;
|
|
7
11
|
#weak;
|
|
12
|
+
#size;
|
|
13
|
+
#finalizationRegistry;
|
|
14
|
+
#finalizationTokens;
|
|
8
15
|
/* CONSTRUCTOR */
|
|
9
16
|
constructor(entries) {
|
|
10
17
|
/* VARIABLES */
|
|
11
18
|
this.#strong = new Map();
|
|
12
19
|
this.#weak = new WeakMap();
|
|
20
|
+
this.#size = 0;
|
|
21
|
+
this.#finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
|
|
22
|
+
this.#finalizationTokens = new WeakMap();
|
|
13
23
|
if (entries) {
|
|
14
24
|
for (const [key, value] of entries) {
|
|
15
25
|
this.set(key, value);
|
|
16
26
|
}
|
|
17
27
|
}
|
|
18
28
|
}
|
|
29
|
+
/* GETTER API */
|
|
30
|
+
get size() {
|
|
31
|
+
return this.#size;
|
|
32
|
+
}
|
|
19
33
|
/* API */
|
|
20
34
|
delete(key) {
|
|
21
|
-
|
|
35
|
+
const hasKey = this.has(key);
|
|
36
|
+
if (!hasKey)
|
|
37
|
+
return false;
|
|
38
|
+
this.#size -= 1;
|
|
39
|
+
if (isWeakabe(key)) {
|
|
40
|
+
const token = this.#finalizationTokens.get(key);
|
|
41
|
+
if (token) {
|
|
42
|
+
this.#finalizationRegistry.unregister(token);
|
|
43
|
+
this.#finalizationTokens.delete(key);
|
|
44
|
+
}
|
|
22
45
|
return this.#weak.delete(key);
|
|
23
46
|
}
|
|
24
47
|
else {
|
|
@@ -26,7 +49,7 @@ class MildMap {
|
|
|
26
49
|
}
|
|
27
50
|
}
|
|
28
51
|
get(key) {
|
|
29
|
-
if (
|
|
52
|
+
if (isWeakabe(key)) {
|
|
30
53
|
return this.#weak.get(key);
|
|
31
54
|
}
|
|
32
55
|
else {
|
|
@@ -34,7 +57,7 @@ class MildMap {
|
|
|
34
57
|
}
|
|
35
58
|
}
|
|
36
59
|
has(key) {
|
|
37
|
-
if (
|
|
60
|
+
if (isWeakabe(key)) {
|
|
38
61
|
return this.#weak.has(key);
|
|
39
62
|
}
|
|
40
63
|
else {
|
|
@@ -42,8 +65,17 @@ class MildMap {
|
|
|
42
65
|
}
|
|
43
66
|
}
|
|
44
67
|
set(key, value) {
|
|
45
|
-
|
|
68
|
+
const hasKey = this.has(key);
|
|
69
|
+
if (!hasKey) {
|
|
70
|
+
this.#size += 1;
|
|
71
|
+
}
|
|
72
|
+
if (isWeakabe(key)) {
|
|
46
73
|
this.#weak.set(key, value);
|
|
74
|
+
if (!hasKey) {
|
|
75
|
+
const token = {};
|
|
76
|
+
this.#finalizationRegistry.register(key, token, token);
|
|
77
|
+
this.#finalizationTokens.set(key, token);
|
|
78
|
+
}
|
|
47
79
|
}
|
|
48
80
|
else {
|
|
49
81
|
this.#strong.set(key, value);
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "mild-map",
|
|
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
|
-
"version": "1.
|
|
5
|
+
"version": "1.1.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"exports": "./dist/index.js",
|
|
@@ -19,9 +19,12 @@
|
|
|
19
19
|
"strong",
|
|
20
20
|
"map"
|
|
21
21
|
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"is": "npm:@fabiospampinato/is@^2.5.0"
|
|
24
|
+
},
|
|
22
25
|
"devDependencies": {
|
|
23
|
-
"fava": "^0.
|
|
24
|
-
"tsex": "^2.
|
|
26
|
+
"fava": "^0.2.0",
|
|
27
|
+
"tsex": "^2.2.0",
|
|
25
28
|
"typescript": "^4.9.5"
|
|
26
29
|
}
|
|
27
30
|
}
|
package/readme.md
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
|
|
2
2
|
/* IMPORT */
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {isWeakReferable} from 'is';
|
|
5
|
+
|
|
6
|
+
/* HELPERS */
|
|
7
|
+
|
|
8
|
+
const isWeakabe = ( value: unknown ): value is object => { //FIXME: Overridden type guard for convenience, delete this once TS updates its types
|
|
9
|
+
return isWeakReferable ( value );
|
|
10
|
+
};
|
|
5
11
|
|
|
6
12
|
/* MAIN */
|
|
7
13
|
|
|
@@ -11,6 +17,10 @@ class MildMap<K, V> {
|
|
|
11
17
|
|
|
12
18
|
#strong = new Map<K, V> ();
|
|
13
19
|
#weak = new WeakMap<any, V> ();
|
|
20
|
+
#size = 0;
|
|
21
|
+
|
|
22
|
+
#finalizationRegistry = new FinalizationRegistry ( () => this.#size -= 1 );
|
|
23
|
+
#finalizationTokens = new WeakMap<object, object> ();
|
|
14
24
|
|
|
15
25
|
/* CONSTRUCTOR */
|
|
16
26
|
|
|
@@ -28,11 +38,34 @@ class MildMap<K, V> {
|
|
|
28
38
|
|
|
29
39
|
}
|
|
30
40
|
|
|
41
|
+
/* GETTER API */
|
|
42
|
+
|
|
43
|
+
get size () {
|
|
44
|
+
|
|
45
|
+
return this.#size;
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
|
|
31
49
|
/* API */
|
|
32
50
|
|
|
33
51
|
delete ( key: K ): boolean {
|
|
34
52
|
|
|
35
|
-
|
|
53
|
+
const hasKey = this.has ( key );
|
|
54
|
+
|
|
55
|
+
if ( !hasKey ) return false;
|
|
56
|
+
|
|
57
|
+
this.#size -= 1;
|
|
58
|
+
|
|
59
|
+
if ( isWeakabe ( key ) ) {
|
|
60
|
+
|
|
61
|
+
const token = this.#finalizationTokens.get ( key );
|
|
62
|
+
|
|
63
|
+
if ( token ) {
|
|
64
|
+
|
|
65
|
+
this.#finalizationRegistry.unregister ( token );
|
|
66
|
+
this.#finalizationTokens.delete ( key );
|
|
67
|
+
|
|
68
|
+
}
|
|
36
69
|
|
|
37
70
|
return this.#weak.delete ( key );
|
|
38
71
|
|
|
@@ -46,7 +79,7 @@ class MildMap<K, V> {
|
|
|
46
79
|
|
|
47
80
|
get ( key: K ): V | undefined {
|
|
48
81
|
|
|
49
|
-
if (
|
|
82
|
+
if ( isWeakabe ( key ) ) {
|
|
50
83
|
|
|
51
84
|
return this.#weak.get ( key );
|
|
52
85
|
|
|
@@ -60,7 +93,7 @@ class MildMap<K, V> {
|
|
|
60
93
|
|
|
61
94
|
has ( key: K ): boolean {
|
|
62
95
|
|
|
63
|
-
if (
|
|
96
|
+
if ( isWeakabe ( key ) ) {
|
|
64
97
|
|
|
65
98
|
return this.#weak.has ( key );
|
|
66
99
|
|
|
@@ -74,10 +107,27 @@ class MildMap<K, V> {
|
|
|
74
107
|
|
|
75
108
|
set ( key: K, value: V ): this {
|
|
76
109
|
|
|
77
|
-
|
|
110
|
+
const hasKey = this.has ( key );
|
|
111
|
+
|
|
112
|
+
if ( !hasKey ) {
|
|
113
|
+
|
|
114
|
+
this.#size += 1;
|
|
115
|
+
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if ( isWeakabe ( key ) ) {
|
|
78
119
|
|
|
79
120
|
this.#weak.set ( key, value );
|
|
80
121
|
|
|
122
|
+
if ( !hasKey ) {
|
|
123
|
+
|
|
124
|
+
const token = {};
|
|
125
|
+
|
|
126
|
+
this.#finalizationRegistry.register ( key, token, token );
|
|
127
|
+
this.#finalizationTokens.set ( key, token );
|
|
128
|
+
|
|
129
|
+
}
|
|
130
|
+
|
|
81
131
|
} else {
|
|
82
132
|
|
|
83
133
|
this.#strong.set ( key, value );
|
package/test/index.js
CHANGED
|
@@ -16,6 +16,8 @@ describe ( 'MildMap', it => {
|
|
|
16
16
|
let primitive = 0;
|
|
17
17
|
let object = {};
|
|
18
18
|
|
|
19
|
+
t.is ( map.size, 0 );
|
|
20
|
+
|
|
19
21
|
t.is ( map.get ( primitive ), undefined );
|
|
20
22
|
t.is ( map.get ( object ), undefined );
|
|
21
23
|
|
|
@@ -28,6 +30,8 @@ describe ( 'MildMap', it => {
|
|
|
28
30
|
map.set ( primitive, 'primitive' );
|
|
29
31
|
map.set ( object, 'object' );
|
|
30
32
|
|
|
33
|
+
t.is ( map.size, 2 );
|
|
34
|
+
|
|
31
35
|
t.is ( map.get ( primitive ), 'primitive' );
|
|
32
36
|
t.is ( map.get ( object ), 'object' );
|
|
33
37
|
|
|
@@ -37,9 +41,13 @@ describe ( 'MildMap', it => {
|
|
|
37
41
|
t.true ( map.delete ( primitive ) );
|
|
38
42
|
t.true ( map.delete ( object ) );
|
|
39
43
|
|
|
44
|
+
t.is ( map.size, 0 );
|
|
45
|
+
|
|
40
46
|
map.set ( primitive, 'primitive' );
|
|
41
47
|
map.set ( object, 'object' );
|
|
42
48
|
|
|
49
|
+
t.is ( map.size, 2 );
|
|
50
|
+
|
|
43
51
|
/* CLEANUP */
|
|
44
52
|
|
|
45
53
|
let deleted = 0;
|
|
@@ -55,6 +63,7 @@ describe ( 'MildMap', it => {
|
|
|
55
63
|
await delay ( 500 );
|
|
56
64
|
|
|
57
65
|
t.is ( deleted, 1 );
|
|
66
|
+
t.is ( map.size, 1 );
|
|
58
67
|
|
|
59
68
|
});
|
|
60
69
|
|
package/dist/constants.d.ts
DELETED
package/dist/constants.js
DELETED
package/dist/utils.d.ts
DELETED
package/dist/utils.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/* IMPORT */
|
|
2
|
-
import { SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS } from './constants.js';
|
|
3
|
-
/* IMPORT */
|
|
4
|
-
const isWeakReferrable = (value) => {
|
|
5
|
-
if (value === null)
|
|
6
|
-
return false;
|
|
7
|
-
const type = typeof value;
|
|
8
|
-
return type === 'object' || type === 'function' || (SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS && type === 'symbol');
|
|
9
|
-
};
|
|
10
|
-
/* EXPORT */
|
|
11
|
-
export { isWeakReferrable };
|
package/src/constants.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/* MAIN */
|
|
3
|
-
|
|
4
|
-
const SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS = ((): boolean => {
|
|
5
|
-
|
|
6
|
-
try {
|
|
7
|
-
|
|
8
|
-
new WeakMap ().set ( Symbol (), 0 );
|
|
9
|
-
|
|
10
|
-
return true;
|
|
11
|
-
|
|
12
|
-
} catch {
|
|
13
|
-
|
|
14
|
-
return false;
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
})();
|
|
19
|
-
|
|
20
|
-
/* EXPORT */
|
|
21
|
-
|
|
22
|
-
export {SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS};
|
package/src/utils.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/* IMPORT */
|
|
3
|
-
|
|
4
|
-
import {SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS} from './constants';
|
|
5
|
-
|
|
6
|
-
/* IMPORT */
|
|
7
|
-
|
|
8
|
-
const isWeakReferrable = ( value: unknown ): value is object | symbol => {
|
|
9
|
-
|
|
10
|
-
if ( value === null ) return false;
|
|
11
|
-
|
|
12
|
-
const type = typeof value;
|
|
13
|
-
|
|
14
|
-
return type === 'object' || type === 'function' || ( SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS && type === 'symbol' );
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/* EXPORT */
|
|
19
|
-
|
|
20
|
-
export {isWeakReferrable};
|