mild-map 1.1.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.js +9 -5
- package/package.json +6 -3
- package/src/index.ts +11 -5
- 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.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
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 */
|
|
@@ -32,7 +36,7 @@ class MildMap {
|
|
|
32
36
|
if (!hasKey)
|
|
33
37
|
return false;
|
|
34
38
|
this.#size -= 1;
|
|
35
|
-
if (
|
|
39
|
+
if (isWeakabe(key)) {
|
|
36
40
|
const token = this.#finalizationTokens.get(key);
|
|
37
41
|
if (token) {
|
|
38
42
|
this.#finalizationRegistry.unregister(token);
|
|
@@ -45,7 +49,7 @@ class MildMap {
|
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
get(key) {
|
|
48
|
-
if (
|
|
52
|
+
if (isWeakabe(key)) {
|
|
49
53
|
return this.#weak.get(key);
|
|
50
54
|
}
|
|
51
55
|
else {
|
|
@@ -53,7 +57,7 @@ class MildMap {
|
|
|
53
57
|
}
|
|
54
58
|
}
|
|
55
59
|
has(key) {
|
|
56
|
-
if (
|
|
60
|
+
if (isWeakabe(key)) {
|
|
57
61
|
return this.#weak.has(key);
|
|
58
62
|
}
|
|
59
63
|
else {
|
|
@@ -65,7 +69,7 @@ class MildMap {
|
|
|
65
69
|
if (!hasKey) {
|
|
66
70
|
this.#size += 1;
|
|
67
71
|
}
|
|
68
|
-
if (
|
|
72
|
+
if (isWeakabe(key)) {
|
|
69
73
|
this.#weak.set(key, value);
|
|
70
74
|
if (!hasKey) {
|
|
71
75
|
const token = {};
|
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.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/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
|
|
|
@@ -50,7 +56,7 @@ class MildMap<K, V> {
|
|
|
50
56
|
|
|
51
57
|
this.#size -= 1;
|
|
52
58
|
|
|
53
|
-
if (
|
|
59
|
+
if ( isWeakabe ( key ) ) {
|
|
54
60
|
|
|
55
61
|
const token = this.#finalizationTokens.get ( key );
|
|
56
62
|
|
|
@@ -73,7 +79,7 @@ class MildMap<K, V> {
|
|
|
73
79
|
|
|
74
80
|
get ( key: K ): V | undefined {
|
|
75
81
|
|
|
76
|
-
if (
|
|
82
|
+
if ( isWeakabe ( key ) ) {
|
|
77
83
|
|
|
78
84
|
return this.#weak.get ( key );
|
|
79
85
|
|
|
@@ -87,7 +93,7 @@ class MildMap<K, V> {
|
|
|
87
93
|
|
|
88
94
|
has ( key: K ): boolean {
|
|
89
95
|
|
|
90
|
-
if (
|
|
96
|
+
if ( isWeakabe ( key ) ) {
|
|
91
97
|
|
|
92
98
|
return this.#weak.has ( key );
|
|
93
99
|
|
|
@@ -109,7 +115,7 @@ class MildMap<K, V> {
|
|
|
109
115
|
|
|
110
116
|
}
|
|
111
117
|
|
|
112
|
-
if (
|
|
118
|
+
if ( isWeakabe ( key ) ) {
|
|
113
119
|
|
|
114
120
|
this.#weak.set ( key, value );
|
|
115
121
|
|
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 => {
|
|
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};
|