mild-map 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_WEAKMAP_KEYS: boolean;
2
+ export { SUPPORTS_SYMBOLS_AS_WEAKMAP_KEYS };
@@ -0,0 +1,12 @@
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 };
@@ -0,0 +1,9 @@
1
+ declare class MildMap<K, V> {
2
+ #private;
3
+ constructor(entries?: readonly (readonly [K, V])[] | null);
4
+ delete(key: K): boolean;
5
+ get(key: K): V | undefined;
6
+ has(key: K): boolean;
7
+ set(key: K, value: V): this;
8
+ }
9
+ export default MildMap;
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ /* IMPORT */
2
+ import { isWeakReferrable } from './utils.js';
3
+ /* MAIN */
4
+ class MildMap {
5
+ /* VARIABLES */
6
+ #strong;
7
+ #weak;
8
+ /* CONSTRUCTOR */
9
+ constructor(entries) {
10
+ /* VARIABLES */
11
+ this.#strong = new Map();
12
+ this.#weak = new WeakMap();
13
+ if (entries) {
14
+ for (const [key, value] of entries) {
15
+ this.set(key, value);
16
+ }
17
+ }
18
+ }
19
+ /* API */
20
+ delete(key) {
21
+ if (isWeakReferrable(key)) {
22
+ return this.#weak.delete(key);
23
+ }
24
+ else {
25
+ return this.#strong.delete(key);
26
+ }
27
+ }
28
+ get(key) {
29
+ if (isWeakReferrable(key)) {
30
+ return this.#weak.get(key);
31
+ }
32
+ else {
33
+ return this.#strong.get(key);
34
+ }
35
+ }
36
+ has(key) {
37
+ if (isWeakReferrable(key)) {
38
+ return this.#weak.has(key);
39
+ }
40
+ else {
41
+ return this.#strong.has(key);
42
+ }
43
+ }
44
+ set(key, value) {
45
+ if (isWeakReferrable(key)) {
46
+ this.#weak.set(key, value);
47
+ }
48
+ else {
49
+ this.#strong.set(key, value);
50
+ }
51
+ return this;
52
+ }
53
+ }
54
+ /* EXPORT */
55
+ export default MildMap;
@@ -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_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/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-map",
3
+ "repository": "github:fabiospampinato/mild-map",
4
+ "description": "A WeakMap 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
+ # MildMap
2
+
3
+ A WeakMap 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-map
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ It has the same API of a WeakMap, 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_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 ADDED
@@ -0,0 +1,95 @@
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
+
15
+ /* CONSTRUCTOR */
16
+
17
+ constructor ( entries?: readonly (readonly [K, V])[] | null ) {
18
+
19
+ if ( entries ) {
20
+
21
+ for ( const [key, value] of entries ) {
22
+
23
+ this.set ( key, value );
24
+
25
+ }
26
+
27
+ }
28
+
29
+ }
30
+
31
+ /* API */
32
+
33
+ delete ( key: K ): boolean {
34
+
35
+ if ( isWeakReferrable ( key ) ) {
36
+
37
+ return this.#weak.delete ( key );
38
+
39
+ } else {
40
+
41
+ return this.#strong.delete ( key );
42
+
43
+ }
44
+
45
+ }
46
+
47
+ get ( key: K ): V | undefined {
48
+
49
+ if ( isWeakReferrable ( key ) ) {
50
+
51
+ return this.#weak.get ( key );
52
+
53
+ } else {
54
+
55
+ return this.#strong.get ( key );
56
+
57
+ }
58
+
59
+ }
60
+
61
+ has ( key: K ): boolean {
62
+
63
+ if ( isWeakReferrable ( key ) ) {
64
+
65
+ return this.#weak.has ( key );
66
+
67
+ } else {
68
+
69
+ return this.#strong.has ( key );
70
+
71
+ }
72
+
73
+ }
74
+
75
+ set ( key: K, value: V ): this {
76
+
77
+ if ( isWeakReferrable ( key ) ) {
78
+
79
+ this.#weak.set ( key, value );
80
+
81
+ } else {
82
+
83
+ this.#strong.set ( key, value );
84
+
85
+ }
86
+
87
+ return this;
88
+
89
+ }
90
+
91
+ }
92
+
93
+ /* EXPORT */
94
+
95
+ export default MildMap;
package/src/utils.ts ADDED
@@ -0,0 +1,20 @@
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 | symbol => {
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 ADDED
@@ -0,0 +1,61 @@
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.get ( primitive ), undefined );
20
+ t.is ( map.get ( object ), undefined );
21
+
22
+ t.false ( map.has ( primitive ) );
23
+ t.false ( map.has ( object ) );
24
+
25
+ t.false ( map.delete ( primitive ) );
26
+ t.false ( map.delete ( object ) );
27
+
28
+ map.set ( primitive, 'primitive' );
29
+ map.set ( object, 'object' );
30
+
31
+ t.is ( map.get ( primitive ), 'primitive' );
32
+ t.is ( map.get ( object ), 'object' );
33
+
34
+ t.true ( map.has ( primitive ) );
35
+ t.true ( map.has ( object ) );
36
+
37
+ t.true ( map.delete ( primitive ) );
38
+ t.true ( map.delete ( object ) );
39
+
40
+ map.set ( primitive, 'primitive' );
41
+ map.set ( object, 'object' );
42
+
43
+ /* CLEANUP */
44
+
45
+ let deleted = 0;
46
+
47
+ const registry = new FinalizationRegistry ( () => deleted++ );
48
+
49
+ registry.register ( object );
50
+
51
+ object = null;
52
+
53
+ await delay ( 500 );
54
+ global.gc ();
55
+ await delay ( 500 );
56
+
57
+ t.is ( deleted, 1 );
58
+
59
+ });
60
+
61
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "tsex/tsconfig.json",
3
+ "compilerOptions": {
4
+ "target": "es2022"
5
+ }
6
+ }