mild-set 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 CHANGED
@@ -1,6 +1,7 @@
1
1
  declare class MildSet<V> {
2
2
  #private;
3
3
  constructor(entries?: readonly V[] | null);
4
+ get size(): number;
4
5
  add(value: V): this;
5
6
  delete(value: V): boolean;
6
7
  has(value: V): boolean;
package/dist/index.js CHANGED
@@ -1,25 +1,48 @@
1
1
  /* IMPORT */
2
- import { isWeakReferrable } from './utils.js';
2
+ import { isWeakReferable } from 'is';
3
+ /* HELPERS */
4
+ const isWeakable = (value) => {
5
+ return isWeakReferable(value);
6
+ };
3
7
  /* MAIN */
4
8
  class MildSet {
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 Set();
12
19
  this.#weak = new WeakSet();
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 value of entries) {
15
25
  this.add(value);
16
26
  }
17
27
  }
18
28
  }
29
+ /* GETTER API */
30
+ get size() {
31
+ return this.#size;
32
+ }
19
33
  /* API */
20
34
  add(value) {
21
- if (isWeakReferrable(value)) {
35
+ const hasValue = this.has(value);
36
+ if (!hasValue) {
37
+ this.#size += 1;
38
+ }
39
+ if (isWeakable(value)) {
22
40
  this.#weak.add(value);
41
+ if (!hasValue) {
42
+ const token = {};
43
+ this.#finalizationRegistry.register(value, token, token);
44
+ this.#finalizationTokens.set(value, token);
45
+ }
23
46
  }
24
47
  else {
25
48
  this.#strong.add(value);
@@ -27,7 +50,16 @@ class MildSet {
27
50
  return this;
28
51
  }
29
52
  delete(value) {
30
- if (isWeakReferrable(value)) {
53
+ const hasValue = this.has(value);
54
+ if (!hasValue)
55
+ return false;
56
+ this.#size -= 1;
57
+ if (isWeakable(value)) {
58
+ const token = this.#finalizationTokens.get(value);
59
+ if (token) {
60
+ this.#finalizationRegistry.unregister(token);
61
+ this.#finalizationTokens.delete(value);
62
+ }
31
63
  return this.#weak.delete(value);
32
64
  }
33
65
  else {
@@ -35,7 +67,7 @@ class MildSet {
35
67
  }
36
68
  }
37
69
  has(value) {
38
- if (isWeakReferrable(value)) {
70
+ if (isWeakable(value)) {
39
71
  return this.#weak.has(value);
40
72
  }
41
73
  else {
package/package.json CHANGED
@@ -2,7 +2,7 @@
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.0.0",
5
+ "version": "1.1.1",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "exports": "./dist/index.js",
@@ -17,11 +17,14 @@
17
17
  "keywords": [
18
18
  "weak",
19
19
  "strong",
20
- "map"
20
+ "set"
21
21
  ],
22
+ "dependencies": {
23
+ "is": "npm:@fabiospampinato/is@^2.5.0"
24
+ },
22
25
  "devDependencies": {
23
- "fava": "^0.1.2",
24
- "tsex": "^2.1.0",
26
+ "fava": "^0.2.0",
27
+ "tsex": "^2.2.0",
25
28
  "typescript": "^4.9.5"
26
29
  }
27
30
  }
package/readme.md CHANGED
@@ -12,6 +12,8 @@ npm install --save mild-set
12
12
 
13
13
  It has the same API of a WeakSet, except that it just works with primitives too.
14
14
 
15
+ It also implements the `size` property, like a Set.
16
+
15
17
  ## License
16
18
 
17
19
  MIT © Fabio Spampinato
package/src/index.ts CHANGED
@@ -1,7 +1,13 @@
1
1
 
2
2
  /* IMPORT */
3
3
 
4
- import {isWeakReferrable} from './utils';
4
+ import {isWeakReferable} from 'is';
5
+
6
+ /* HELPERS */
7
+
8
+ const isWeakable = ( 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 MildSet<V> {
11
17
 
12
18
  #strong = new Set<V> ();
13
19
  #weak = new WeakSet<any> ();
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,14 +38,39 @@ class MildSet<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
  add ( value: V ): this {
34
52
 
35
- if ( isWeakReferrable ( value ) ) {
53
+ const hasValue = this.has ( value );
54
+
55
+ if ( !hasValue ) {
56
+
57
+ this.#size += 1;
58
+
59
+ }
60
+
61
+ if ( isWeakable ( value ) ) {
36
62
 
37
63
  this.#weak.add ( value );
38
64
 
65
+ if ( !hasValue ) {
66
+
67
+ const token = {};
68
+
69
+ this.#finalizationRegistry.register ( value, token, token );
70
+ this.#finalizationTokens.set ( value, token );
71
+
72
+ }
73
+
39
74
  } else {
40
75
 
41
76
  this.#strong.add ( value );
@@ -48,7 +83,22 @@ class MildSet<V> {
48
83
 
49
84
  delete ( value: V ): boolean {
50
85
 
51
- if ( isWeakReferrable ( value ) ) {
86
+ const hasValue = this.has ( value );
87
+
88
+ if ( !hasValue ) return false;
89
+
90
+ this.#size -= 1;
91
+
92
+ if ( isWeakable ( value ) ) {
93
+
94
+ const token = this.#finalizationTokens.get ( value );
95
+
96
+ if ( token ) {
97
+
98
+ this.#finalizationRegistry.unregister ( token );
99
+ this.#finalizationTokens.delete ( value );
100
+
101
+ }
52
102
 
53
103
  return this.#weak.delete ( value );
54
104
 
@@ -62,7 +112,7 @@ class MildSet<V> {
62
112
 
63
113
  has ( value: V ): boolean {
64
114
 
65
- if ( isWeakReferrable ( value ) ) {
115
+ if ( isWeakable ( value ) ) {
66
116
 
67
117
  return this.#weak.has ( value );
68
118
 
package/test/index.js CHANGED
@@ -16,6 +16,8 @@ describe ( 'MildSet', it => {
16
16
  let primitive = 0;
17
17
  let object = {};
18
18
 
19
+ t.is ( set.size, 0 );
20
+
19
21
  t.false ( set.has ( primitive ) );
20
22
  t.false ( set.has ( object ) );
21
23
 
@@ -25,15 +27,21 @@ describe ( 'MildSet', it => {
25
27
  set.add ( primitive );
26
28
  set.add ( object );
27
29
 
30
+ t.is ( set.size, 2 );
31
+
28
32
  t.true ( set.has ( primitive ) );
29
33
  t.true ( set.has ( object ) );
30
34
 
31
35
  t.true ( set.delete ( primitive ) );
32
36
  t.true ( set.delete ( object ) );
33
37
 
38
+ t.is ( set.size, 0 );
39
+
34
40
  set.add ( primitive );
35
41
  set.add ( object );
36
42
 
43
+ t.is ( set.size, 2 );
44
+
37
45
  /* CLEANUP */
38
46
 
39
47
  let deleted = 0;
@@ -49,6 +57,7 @@ describe ( 'MildSet', it => {
49
57
  await delay ( 500 );
50
58
 
51
59
  t.is ( deleted, 1 );
60
+ t.is ( set.size, 1 );
52
61
 
53
62
  });
54
63
 
@@ -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/dist/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare const isWeakReferrable: (value: unknown) => value is symbol | object;
2
- export { isWeakReferrable };
package/dist/utils.js DELETED
@@ -1,11 +0,0 @@
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
- };
10
- /* EXPORT */
11
- export { isWeakReferrable };
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/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 | symbol => {
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};