mild-set 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 MildSet {
5
5
  /* VARIABLES */
6
- #strong;
7
- #weak;
8
- #size;
9
- #finalizationRegistry;
10
- #finalizationTokens;
6
+ #strong = new Set();
7
+ #weak = new WeakSet();
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 Set();
15
- this.#weak = new WeakSet();
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 value of entries) {
21
15
  this.add(value);
@@ -32,7 +26,7 @@ class MildSet {
32
26
  if (!hasValue) {
33
27
  this.#size += 1;
34
28
  }
35
- if (isWeakReferrable(value)) {
29
+ if (isWeakReferable(value)) {
36
30
  this.#weak.add(value);
37
31
  if (!hasValue) {
38
32
  const token = {};
@@ -50,7 +44,7 @@ class MildSet {
50
44
  if (!hasValue)
51
45
  return false;
52
46
  this.#size -= 1;
53
- if (isWeakReferrable(value)) {
47
+ if (isWeakReferable(value)) {
54
48
  const token = this.#finalizationTokens.get(value);
55
49
  if (token) {
56
50
  this.#finalizationRegistry.unregister(token);
@@ -63,7 +57,7 @@ class MildSet {
63
57
  }
64
58
  }
65
59
  has(value) {
66
- if (isWeakReferrable(value)) {
60
+ if (isWeakReferable(value)) {
67
61
  return this.#weak.has(value);
68
62
  }
69
63
  else {
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_WEAKSET_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_WEAKSET_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-set",
3
3
  "repository": "github:fabiospampinato/mild-set",
4
4
  "description": "A WeakSet 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
  "set"
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 WeakSet that supports any value, it holds strong references to primitives, and
5
5
  ## Install
6
6
 
7
7
  ```sh
8
- npm install --save mild-set
8
+ npm install mild-set
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_WEAKSET_KEYS: boolean;
2
- export { SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS };
package/dist/constants.js DELETED
@@ -1,12 +0,0 @@
1
- /* MAIN */
2
- const SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS = (() => {
3
- try {
4
- new WeakSet().add(Symbol());
5
- return true;
6
- }
7
- catch {
8
- return false;
9
- }
10
- })();
11
- /* EXPORT */
12
- export { SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS };
package/src/constants.ts DELETED
@@ -1,22 +0,0 @@
1
-
2
- /* MAIN */
3
-
4
- const SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS = ((): boolean => {
5
-
6
- try {
7
-
8
- new WeakSet ().add ( Symbol () );
9
-
10
- return true;
11
-
12
- } catch {
13
-
14
- return false;
15
-
16
- }
17
-
18
- })();
19
-
20
- /* EXPORT */
21
-
22
- export {SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS};
package/src/index.ts DELETED
@@ -1,125 +0,0 @@
1
-
2
- /* IMPORT */
3
-
4
- import {isWeakReferrable} from './utils';
5
-
6
- /* MAIN */
7
-
8
- class MildSet<V> {
9
-
10
- /* VARIABLES */
11
-
12
- #strong = new Set<V> ();
13
- #weak = new WeakSet<any> ();
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 V[] | null ) {
22
-
23
- if ( entries ) {
24
-
25
- for ( const value of entries ) {
26
-
27
- this.add ( 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
- add ( value: V ): this {
46
-
47
- const hasValue = this.has ( value );
48
-
49
- if ( !hasValue ) {
50
-
51
- this.#size += 1;
52
-
53
- }
54
-
55
- if ( isWeakReferrable ( value ) ) {
56
-
57
- this.#weak.add ( value );
58
-
59
- if ( !hasValue ) {
60
-
61
- const token = {};
62
-
63
- this.#finalizationRegistry.register ( value, token, token );
64
- this.#finalizationTokens.set ( value, token );
65
-
66
- }
67
-
68
- } else {
69
-
70
- this.#strong.add ( value );
71
-
72
- }
73
-
74
- return this;
75
-
76
- }
77
-
78
- delete ( value: V ): boolean {
79
-
80
- const hasValue = this.has ( value );
81
-
82
- if ( !hasValue ) return false;
83
-
84
- this.#size -= 1;
85
-
86
- if ( isWeakReferrable ( value ) ) {
87
-
88
- const token = this.#finalizationTokens.get ( value );
89
-
90
- if ( token ) {
91
-
92
- this.#finalizationRegistry.unregister ( token );
93
- this.#finalizationTokens.delete ( value );
94
-
95
- }
96
-
97
- return this.#weak.delete ( value );
98
-
99
- } else {
100
-
101
- return this.#strong.delete ( value );
102
-
103
- }
104
-
105
- }
106
-
107
- has ( value: V ): boolean {
108
-
109
- if ( isWeakReferrable ( value ) ) {
110
-
111
- return this.#weak.has ( value );
112
-
113
- } else {
114
-
115
- return this.#strong.has ( value );
116
-
117
- }
118
-
119
- }
120
-
121
- }
122
-
123
- /* EXPORT */
124
-
125
- export default MildSet;
package/src/utils.ts DELETED
@@ -1,20 +0,0 @@
1
-
2
- /* IMPORT */
3
-
4
- import {SUPPORTS_SYMBOLS_AS_WEAKSET_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_WEAKSET_KEYS && type === 'symbol' );
15
-
16
- };
17
-
18
- /* EXPORT */
19
-
20
- export {isWeakReferrable};
package/test/index.js DELETED
@@ -1,64 +0,0 @@
1
-
2
- /* IMPORT */
3
-
4
- import {describe} from 'fava';
5
- import {setTimeout as delay} from 'node:timers/promises';
6
- import MildSet from '../dist/index.js';
7
-
8
- /* MAIN */
9
-
10
- describe ( 'MildSet', it => {
11
-
12
- it ( 'works', async t => {
13
-
14
- const set = new MildSet ();
15
-
16
- let primitive = 0;
17
- let object = {};
18
-
19
- t.is ( set.size, 0 );
20
-
21
- t.false ( set.has ( primitive ) );
22
- t.false ( set.has ( object ) );
23
-
24
- t.false ( set.delete ( primitive ) );
25
- t.false ( set.delete ( object ) );
26
-
27
- set.add ( primitive );
28
- set.add ( object );
29
-
30
- t.is ( set.size, 2 );
31
-
32
- t.true ( set.has ( primitive ) );
33
- t.true ( set.has ( object ) );
34
-
35
- t.true ( set.delete ( primitive ) );
36
- t.true ( set.delete ( object ) );
37
-
38
- t.is ( set.size, 0 );
39
-
40
- set.add ( primitive );
41
- set.add ( object );
42
-
43
- t.is ( set.size, 2 );
44
-
45
- /* CLEANUP */
46
-
47
- let deleted = 0;
48
-
49
- const registry = new FinalizationRegistry ( () => deleted++ );
50
-
51
- registry.register ( object );
52
-
53
- object = null;
54
-
55
- await delay ( 500 );
56
- global.gc ();
57
- await delay ( 500 );
58
-
59
- t.is ( deleted, 1 );
60
- t.is ( set.size, 1 );
61
-
62
- });
63
-
64
- });
package/tsconfig.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "extends": "tsex/tsconfig.json",
3
- "compilerOptions": {
4
- "target": "es2022"
5
- }
6
- }