mild-set 1.0.0

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/.editorconfig ADDED
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,2 @@
1
+ declare const SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS: boolean;
2
+ export { SUPPORTS_SYMBOLS_AS_WEAKSET_KEYS };
@@ -0,0 +1,12 @@
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 };
@@ -0,0 +1,8 @@
1
+ declare class MildSet<V> {
2
+ #private;
3
+ constructor(entries?: readonly V[] | null);
4
+ add(value: V): this;
5
+ delete(value: V): boolean;
6
+ has(value: V): boolean;
7
+ }
8
+ export default MildSet;
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ /* IMPORT */
2
+ import { isWeakReferrable } from './utils.js';
3
+ /* MAIN */
4
+ class MildSet {
5
+ /* VARIABLES */
6
+ #strong;
7
+ #weak;
8
+ /* CONSTRUCTOR */
9
+ constructor(entries) {
10
+ /* VARIABLES */
11
+ this.#strong = new Set();
12
+ this.#weak = new WeakSet();
13
+ if (entries) {
14
+ for (const value of entries) {
15
+ this.add(value);
16
+ }
17
+ }
18
+ }
19
+ /* API */
20
+ add(value) {
21
+ if (isWeakReferrable(value)) {
22
+ this.#weak.add(value);
23
+ }
24
+ else {
25
+ this.#strong.add(value);
26
+ }
27
+ return this;
28
+ }
29
+ delete(value) {
30
+ if (isWeakReferrable(value)) {
31
+ return this.#weak.delete(value);
32
+ }
33
+ else {
34
+ return this.#strong.delete(value);
35
+ }
36
+ }
37
+ has(value) {
38
+ if (isWeakReferrable(value)) {
39
+ return this.#weak.has(value);
40
+ }
41
+ else {
42
+ return this.#strong.has(value);
43
+ }
44
+ }
45
+ }
46
+ /* EXPORT */
47
+ export default MildSet;
@@ -0,0 +1,2 @@
1
+ declare const isWeakReferrable: (value: unknown) => value is symbol | object;
2
+ export { isWeakReferrable };
package/dist/utils.js ADDED
@@ -0,0 +1,11 @@
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/license ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present Fabio Spampinato
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a
6
+ copy of this software and associated documentation files (the "Software"),
7
+ to deal in the Software without restriction, including without limitation
8
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
+ and/or sell copies of the Software, and to permit persons to whom the
10
+ Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
+ DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "mild-set",
3
+ "repository": "github:fabiospampinato/mild-set",
4
+ "description": "A WeakSet that supports any value, it holds strong references to primitives, and weak references to objects.",
5
+ "version": "1.0.0",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "exports": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "scripts": {
11
+ "clean": "tsex clean",
12
+ "compile": "tsex compile",
13
+ "compile:watch": "tsex compile --watch",
14
+ "test": "node --expose-gc test/index.js",
15
+ "prepublishOnly": "npm run clean && npm run compile && npm run test"
16
+ },
17
+ "keywords": [
18
+ "weak",
19
+ "strong",
20
+ "map"
21
+ ],
22
+ "devDependencies": {
23
+ "fava": "^0.1.2",
24
+ "tsex": "^2.1.0",
25
+ "typescript": "^4.9.5"
26
+ }
27
+ }
package/readme.md ADDED
@@ -0,0 +1,17 @@
1
+ # MildSet
2
+
3
+ A WeakSet that supports any value, it holds strong references to primitives, and weak references to objects.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install --save mild-set
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ It has the same API of a WeakSet, except that it just works with primitives too.
14
+
15
+ ## License
16
+
17
+ MIT © Fabio Spampinato
@@ -0,0 +1,22 @@
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 ADDED
@@ -0,0 +1,81 @@
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
+
15
+ /* CONSTRUCTOR */
16
+
17
+ constructor ( entries?: readonly V[] | null ) {
18
+
19
+ if ( entries ) {
20
+
21
+ for ( const value of entries ) {
22
+
23
+ this.add ( value );
24
+
25
+ }
26
+
27
+ }
28
+
29
+ }
30
+
31
+ /* API */
32
+
33
+ add ( value: V ): this {
34
+
35
+ if ( isWeakReferrable ( value ) ) {
36
+
37
+ this.#weak.add ( value );
38
+
39
+ } else {
40
+
41
+ this.#strong.add ( value );
42
+
43
+ }
44
+
45
+ return this;
46
+
47
+ }
48
+
49
+ delete ( value: V ): boolean {
50
+
51
+ if ( isWeakReferrable ( value ) ) {
52
+
53
+ return this.#weak.delete ( value );
54
+
55
+ } else {
56
+
57
+ return this.#strong.delete ( value );
58
+
59
+ }
60
+
61
+ }
62
+
63
+ has ( value: V ): boolean {
64
+
65
+ if ( isWeakReferrable ( value ) ) {
66
+
67
+ return this.#weak.has ( value );
68
+
69
+ } else {
70
+
71
+ return this.#strong.has ( value );
72
+
73
+ }
74
+
75
+ }
76
+
77
+ }
78
+
79
+ /* EXPORT */
80
+
81
+ export default MildSet;
package/src/utils.ts ADDED
@@ -0,0 +1,20 @@
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};
package/test/index.js ADDED
@@ -0,0 +1,55 @@
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.false ( set.has ( primitive ) );
20
+ t.false ( set.has ( object ) );
21
+
22
+ t.false ( set.delete ( primitive ) );
23
+ t.false ( set.delete ( object ) );
24
+
25
+ set.add ( primitive );
26
+ set.add ( object );
27
+
28
+ t.true ( set.has ( primitive ) );
29
+ t.true ( set.has ( object ) );
30
+
31
+ t.true ( set.delete ( primitive ) );
32
+ t.true ( set.delete ( object ) );
33
+
34
+ set.add ( primitive );
35
+ set.add ( object );
36
+
37
+ /* CLEANUP */
38
+
39
+ let deleted = 0;
40
+
41
+ const registry = new FinalizationRegistry ( () => deleted++ );
42
+
43
+ registry.register ( object );
44
+
45
+ object = null;
46
+
47
+ await delay ( 500 );
48
+ global.gc ();
49
+ await delay ( 500 );
50
+
51
+ t.is ( deleted, 1 );
52
+
53
+ });
54
+
55
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "tsex/tsconfig.json",
3
+ "compilerOptions": {
4
+ "target": "es2022"
5
+ }
6
+ }