mild-map 1.1.0 → 1.1.2

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 CHANGED
@@ -1,21 +1,15 @@
1
1
  /* IMPORT */
2
- import { isWeakReferrable } from './utils.js';
2
+ import { isWeakReferable } from './utils.js';
3
3
  /* MAIN */
4
4
  class MildMap {
5
5
  /* VARIABLES */
6
- #strong;
7
- #weak;
8
- #size;
9
- #finalizationRegistry;
10
- #finalizationTokens;
6
+ #strong = new Map();
7
+ #weak = new WeakMap();
8
+ #size = 0;
9
+ #finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
10
+ #finalizationTokens = new WeakMap();
11
11
  /* CONSTRUCTOR */
12
12
  constructor(entries) {
13
- /* VARIABLES */
14
- this.#strong = new Map();
15
- this.#weak = new WeakMap();
16
- this.#size = 0;
17
- this.#finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
18
- this.#finalizationTokens = new WeakMap();
19
13
  if (entries) {
20
14
  for (const [key, value] of entries) {
21
15
  this.set(key, value);
@@ -32,7 +26,7 @@ class MildMap {
32
26
  if (!hasKey)
33
27
  return false;
34
28
  this.#size -= 1;
35
- if (isWeakReferrable(key)) {
29
+ if (isWeakReferable(key)) {
36
30
  const token = this.#finalizationTokens.get(key);
37
31
  if (token) {
38
32
  this.#finalizationRegistry.unregister(token);
@@ -45,7 +39,7 @@ class MildMap {
45
39
  }
46
40
  }
47
41
  get(key) {
48
- if (isWeakReferrable(key)) {
42
+ if (isWeakReferable(key)) {
49
43
  return this.#weak.get(key);
50
44
  }
51
45
  else {
@@ -53,7 +47,7 @@ class MildMap {
53
47
  }
54
48
  }
55
49
  has(key) {
56
- if (isWeakReferrable(key)) {
50
+ if (isWeakReferable(key)) {
57
51
  return this.#weak.has(key);
58
52
  }
59
53
  else {
@@ -65,7 +59,7 @@ class MildMap {
65
59
  if (!hasKey) {
66
60
  this.#size += 1;
67
61
  }
68
- if (isWeakReferrable(key)) {
62
+ if (isWeakReferable(key)) {
69
63
  this.#weak.set(key, value);
70
64
  if (!hasKey) {
71
65
  const token = {};
package/dist/utils.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const isWeakReferrable: (value: unknown) => value is object;
2
- export { isWeakReferrable };
1
+ declare const isWeakReferable: (value: unknown) => value is WeakKey;
2
+ export { isWeakReferable };
package/dist/utils.js CHANGED
@@ -1,11 +1,19 @@
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
- };
1
+ /* MAIN */
2
+ const isWeakReferable = (() => {
3
+ const SYMBOLS_ARE_WEAK_REFERABLE = (() => {
4
+ try {
5
+ new WeakSet().add(Symbol());
6
+ return true;
7
+ }
8
+ catch {
9
+ return false;
10
+ }
11
+ })();
12
+ return (value) => {
13
+ if (value === null)
14
+ return false;
15
+ return typeof value === 'object' || typeof value === 'function' || (SYMBOLS_ARE_WEAK_REFERABLE && typeof value === 'symbol' && !Symbol.keyFor(value));
16
+ };
17
+ })();
10
18
  /* EXPORT */
11
- export { isWeakReferrable };
19
+ export { isWeakReferable };
package/package.json CHANGED
@@ -2,7 +2,8 @@
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.0",
5
+ "license": "MIT",
6
+ "version": "1.1.2",
6
7
  "type": "module",
7
8
  "main": "dist/index.js",
8
9
  "exports": "./dist/index.js",
@@ -20,8 +21,8 @@
20
21
  "map"
21
22
  ],
22
23
  "devDependencies": {
23
- "fava": "^0.1.2",
24
- "tsex": "^2.1.0",
25
- "typescript": "^4.9.5"
24
+ "fava": "^0.3.4",
25
+ "tsex": "^4.0.2",
26
+ "typescript": "^5.7.3"
26
27
  }
27
28
  }
package/readme.md CHANGED
@@ -5,7 +5,7 @@ A WeakMap that supports any value, it holds strong references to primitives, and
5
5
  ## Install
6
6
 
7
7
  ```sh
8
- npm install --save mild-map
8
+ npm install mild-map
9
9
  ```
10
10
 
11
11
  ## Usage
package/.editorconfig DELETED
@@ -1,10 +0,0 @@
1
-
2
- root = true
3
-
4
- [*]
5
- charset = utf-8
6
- end_of_line = lf
7
- indent_size = 2
8
- indent_style = space
9
- insert_final_newline = true
10
- trim_trailing_whitespace = true
@@ -1,2 +0,0 @@
1
- declare const SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS: boolean;
2
- export { SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS };
package/dist/constants.js DELETED
@@ -1,12 +0,0 @@
1
- /* MAIN */
2
- const SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS = (() => {
3
- try {
4
- new WeakMap().set(Symbol(), 0);
5
- return true;
6
- }
7
- catch {
8
- return false;
9
- }
10
- })();
11
- /* EXPORT */
12
- export { SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS };
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/index.ts DELETED
@@ -1,139 +0,0 @@
1
-
2
- /* IMPORT */
3
-
4
- import {isWeakReferrable} from './utils';
5
-
6
- /* MAIN */
7
-
8
- class MildMap<K, V> {
9
-
10
- /* VARIABLES */
11
-
12
- #strong = new Map<K, V> ();
13
- #weak = new WeakMap<any, V> ();
14
- #size = 0;
15
-
16
- #finalizationRegistry = new FinalizationRegistry ( () => this.#size -= 1 );
17
- #finalizationTokens = new WeakMap<object, object> ();
18
-
19
- /* CONSTRUCTOR */
20
-
21
- constructor ( entries?: readonly (readonly [K, V])[] | null ) {
22
-
23
- if ( entries ) {
24
-
25
- for ( const [key, value] of entries ) {
26
-
27
- this.set ( key, value );
28
-
29
- }
30
-
31
- }
32
-
33
- }
34
-
35
- /* GETTER API */
36
-
37
- get size () {
38
-
39
- return this.#size;
40
-
41
- }
42
-
43
- /* API */
44
-
45
- delete ( key: K ): boolean {
46
-
47
- const hasKey = this.has ( key );
48
-
49
- if ( !hasKey ) return false;
50
-
51
- this.#size -= 1;
52
-
53
- if ( isWeakReferrable ( key ) ) {
54
-
55
- const token = this.#finalizationTokens.get ( key );
56
-
57
- if ( token ) {
58
-
59
- this.#finalizationRegistry.unregister ( token );
60
- this.#finalizationTokens.delete ( key );
61
-
62
- }
63
-
64
- return this.#weak.delete ( key );
65
-
66
- } else {
67
-
68
- return this.#strong.delete ( key );
69
-
70
- }
71
-
72
- }
73
-
74
- get ( key: K ): V | undefined {
75
-
76
- if ( isWeakReferrable ( key ) ) {
77
-
78
- return this.#weak.get ( key );
79
-
80
- } else {
81
-
82
- return this.#strong.get ( key );
83
-
84
- }
85
-
86
- }
87
-
88
- has ( key: K ): boolean {
89
-
90
- if ( isWeakReferrable ( key ) ) {
91
-
92
- return this.#weak.has ( key );
93
-
94
- } else {
95
-
96
- return this.#strong.has ( key );
97
-
98
- }
99
-
100
- }
101
-
102
- set ( key: K, value: V ): this {
103
-
104
- const hasKey = this.has ( key );
105
-
106
- if ( !hasKey ) {
107
-
108
- this.#size += 1;
109
-
110
- }
111
-
112
- if ( isWeakReferrable ( key ) ) {
113
-
114
- this.#weak.set ( key, value );
115
-
116
- if ( !hasKey ) {
117
-
118
- const token = {};
119
-
120
- this.#finalizationRegistry.register ( key, token, token );
121
- this.#finalizationTokens.set ( key, token );
122
-
123
- }
124
-
125
- } else {
126
-
127
- this.#strong.set ( key, value );
128
-
129
- }
130
-
131
- return this;
132
-
133
- }
134
-
135
- }
136
-
137
- /* EXPORT */
138
-
139
- export default MildMap;
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};
package/test/index.js DELETED
@@ -1,70 +0,0 @@
1
-
2
- /* IMPORT */
3
-
4
- import {describe} from 'fava';
5
- import {setTimeout as delay} from 'node:timers/promises';
6
- import MildMap from '../dist/index.js';
7
-
8
- /* MAIN */
9
-
10
- describe ( 'MildMap', it => {
11
-
12
- it ( 'works', async t => {
13
-
14
- const map = new MildMap ();
15
-
16
- let primitive = 0;
17
- let object = {};
18
-
19
- t.is ( map.size, 0 );
20
-
21
- t.is ( map.get ( primitive ), undefined );
22
- t.is ( map.get ( object ), undefined );
23
-
24
- t.false ( map.has ( primitive ) );
25
- t.false ( map.has ( object ) );
26
-
27
- t.false ( map.delete ( primitive ) );
28
- t.false ( map.delete ( object ) );
29
-
30
- map.set ( primitive, 'primitive' );
31
- map.set ( object, 'object' );
32
-
33
- t.is ( map.size, 2 );
34
-
35
- t.is ( map.get ( primitive ), 'primitive' );
36
- t.is ( map.get ( object ), 'object' );
37
-
38
- t.true ( map.has ( primitive ) );
39
- t.true ( map.has ( object ) );
40
-
41
- t.true ( map.delete ( primitive ) );
42
- t.true ( map.delete ( object ) );
43
-
44
- t.is ( map.size, 0 );
45
-
46
- map.set ( primitive, 'primitive' );
47
- map.set ( object, 'object' );
48
-
49
- t.is ( map.size, 2 );
50
-
51
- /* CLEANUP */
52
-
53
- let deleted = 0;
54
-
55
- const registry = new FinalizationRegistry ( () => deleted++ );
56
-
57
- registry.register ( object );
58
-
59
- object = null;
60
-
61
- await delay ( 500 );
62
- global.gc ();
63
- await delay ( 500 );
64
-
65
- t.is ( deleted, 1 );
66
- t.is ( map.size, 1 );
67
-
68
- });
69
-
70
- });
package/tsconfig.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "extends": "tsex/tsconfig.json",
3
- "compilerOptions": {
4
- "target": "es2022"
5
- }
6
- }