getorset-anything 0.1.0 → 1.0.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 +6 -5
- package/dist/index.js +58 -23
- package/package.json +21 -71
- package/dist/cjs/index.cjs +0 -30
- package/dist/cjs/index.d.cts +0 -44
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer
|
|
|
13
13
|
* arr.push('xyz')
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
|
-
declare function mapGetOrSet<M extends Map<unknown, unknown>, T extends () => ValueOfMap<M>>(map: M, key: KeyOfMap<M>, initialValue: T): ReturnType<T>;
|
|
16
|
+
export declare function mapGetOrSet<M extends Map<unknown, unknown>, T extends () => ValueOfMap<M>>(map: M, key: KeyOfMap<M>, initialValue: T): ReturnType<T>;
|
|
17
17
|
/**
|
|
18
18
|
* Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
|
|
19
19
|
*
|
|
@@ -27,7 +27,9 @@ declare function mapGetOrSet<M extends Map<unknown, unknown>, T extends () => Va
|
|
|
27
27
|
* arr.push('xyz')
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
declare function objGetOrSet<O extends
|
|
30
|
+
export declare function objGetOrSet<O extends {
|
|
31
|
+
[key in string | number | symbol]: unknown;
|
|
32
|
+
}, T extends () => O[keyof O]>(obj: O, key: keyof O, initialValue: T): ReturnType<T>;
|
|
31
33
|
/**
|
|
32
34
|
* Retrieve the value in an array, or if it wasn't found, set an initial value and return that.
|
|
33
35
|
*
|
|
@@ -39,6 +41,5 @@ declare function objGetOrSet<O extends Record<string | number | symbol, unknown>
|
|
|
39
41
|
* // ↳ 123
|
|
40
42
|
* ```
|
|
41
43
|
*/
|
|
42
|
-
declare function arrGetOrSet<A extends unknown[], T extends () => A[number]>(arr: A, index: number, initialValue: T): ReturnType<T>;
|
|
43
|
-
|
|
44
|
-
export { arrGetOrSet, mapGetOrSet, objGetOrSet };
|
|
44
|
+
export declare function arrGetOrSet<A extends unknown[], T extends () => A[number]>(arr: A, index: number, initialValue: T): ReturnType<T>;
|
|
45
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```js
|
|
6
|
+
* const map = new Map<string, number[]>()
|
|
7
|
+
*
|
|
8
|
+
* const arr = mapGetOrSet(map, '123', () => [])
|
|
9
|
+
* // ↳ []
|
|
10
|
+
*
|
|
11
|
+
* arr.push('xyz')
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function mapGetOrSet(map, key, initialValue) {
|
|
15
|
+
let val = map.get(key);
|
|
16
|
+
if (val === undefined) {
|
|
17
|
+
val = initialValue();
|
|
18
|
+
map.set(key, val);
|
|
19
|
+
}
|
|
20
|
+
return val;
|
|
8
21
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```js
|
|
27
|
+
* const obj: Record<string, number[]> = {}
|
|
28
|
+
*
|
|
29
|
+
* const arr = objGetOrSet(obj, '123', () => [])
|
|
30
|
+
* // ↳ []
|
|
31
|
+
*
|
|
32
|
+
* arr.push('xyz')
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function objGetOrSet(obj, key, initialValue) {
|
|
36
|
+
let val = obj[key];
|
|
37
|
+
if (val === undefined) {
|
|
38
|
+
val = initialValue();
|
|
39
|
+
obj[key] = val;
|
|
40
|
+
}
|
|
41
|
+
return val;
|
|
16
42
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Retrieve the value in an array, or if it wasn't found, set an initial value and return that.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```js
|
|
48
|
+
* const arr: number[] = []
|
|
49
|
+
*
|
|
50
|
+
* const val = arrGetOrSet(arr, 0, () => 123)
|
|
51
|
+
* // ↳ 123
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function arrGetOrSet(arr, index, initialValue) {
|
|
55
|
+
let val = arr[index];
|
|
56
|
+
if (val === undefined) {
|
|
57
|
+
val = initialValue();
|
|
58
|
+
arr[index] = val;
|
|
59
|
+
}
|
|
60
|
+
return val;
|
|
24
61
|
}
|
|
25
|
-
|
|
26
|
-
export { arrGetOrSet, mapGetOrSet, objGetOrSet };
|
package/package.json
CHANGED
|
@@ -1,40 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "getorset-anything",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Gets a value from a Map/Obj or sets an initial value when not found and returns that. TypeScript supported.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
|
-
"module": "./dist/index.js",
|
|
9
|
-
"main": "./dist/index.js",
|
|
10
7
|
"exports": {
|
|
11
|
-
".":
|
|
12
|
-
"require": {
|
|
13
|
-
"types": "./dist/cjs/index.d.cts",
|
|
14
|
-
"default": "./dist/cjs/index.cjs"
|
|
15
|
-
},
|
|
16
|
-
"import": {
|
|
17
|
-
"types": "./dist/index.d.ts",
|
|
18
|
-
"default": "./dist/index.js"
|
|
19
|
-
}
|
|
20
|
-
}
|
|
8
|
+
".": "./dist/index.js"
|
|
21
9
|
},
|
|
22
|
-
"files": [
|
|
23
|
-
"dist"
|
|
24
|
-
],
|
|
25
10
|
"engines": {
|
|
26
|
-
"node": ">=
|
|
11
|
+
"node": ">=18"
|
|
27
12
|
},
|
|
28
13
|
"scripts": {
|
|
29
|
-
"lint": "tsc --noEmit && eslint ./src
|
|
14
|
+
"lint": "tsc --noEmit && eslint ./src",
|
|
30
15
|
"test": "vitest run",
|
|
31
|
-
"build": "
|
|
32
|
-
"release": "npm run lint &&
|
|
16
|
+
"build": "del-cli dist && tsc",
|
|
17
|
+
"release": "npm run lint && npm run build && np"
|
|
33
18
|
},
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"del-cli": "^5.1.0",
|
|
21
|
+
"np": "^10.0.5",
|
|
22
|
+
"vitest": "^1.6.0",
|
|
23
|
+
"@cycraft/eslint": "^0.3.0",
|
|
24
|
+
"@cycraft/tsconfig": "^0.1.2"
|
|
37
25
|
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
38
29
|
"keywords": [
|
|
39
30
|
"javascript",
|
|
40
31
|
"map",
|
|
@@ -55,54 +46,13 @@
|
|
|
55
46
|
"getter-setter",
|
|
56
47
|
"setter-getter"
|
|
57
48
|
],
|
|
58
|
-
"author": "Luca Ban - Mesqueeb",
|
|
49
|
+
"author": "Luca Ban - Mesqueeb (https://cycraft.co)",
|
|
59
50
|
"funding": "https://github.com/sponsors/mesqueeb",
|
|
60
51
|
"license": "MIT",
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
"
|
|
64
|
-
"@typescript-eslint/parser": "^5.59.2",
|
|
65
|
-
"del-cli": "^5.0.0",
|
|
66
|
-
"eslint": "^8.40.0",
|
|
67
|
-
"eslint-config-prettier": "^8.8.0",
|
|
68
|
-
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
69
|
-
"np": "^7.7.0",
|
|
70
|
-
"prettier": "^2.8.8",
|
|
71
|
-
"rollup": "^3.23.0",
|
|
72
|
-
"rollup-plugin-dts": "^5.3.0",
|
|
73
|
-
"rollup-plugin-esbuild": "^5.0.0",
|
|
74
|
-
"typescript": "^4.9.5",
|
|
75
|
-
"vitest": "^0.31.0"
|
|
76
|
-
},
|
|
77
|
-
"np": {
|
|
78
|
-
"yarn": false,
|
|
79
|
-
"branch": "production"
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "https://github.com/mesqueeb/getorset-anything.git"
|
|
80
55
|
},
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
"node_modules",
|
|
84
|
-
"dist",
|
|
85
|
-
"scripts",
|
|
86
|
-
"test"
|
|
87
|
-
],
|
|
88
|
-
"root": true,
|
|
89
|
-
"parser": "@typescript-eslint/parser",
|
|
90
|
-
"plugins": [
|
|
91
|
-
"@typescript-eslint",
|
|
92
|
-
"tree-shaking"
|
|
93
|
-
],
|
|
94
|
-
"extends": [
|
|
95
|
-
"eslint:recommended",
|
|
96
|
-
"plugin:@typescript-eslint/eslint-recommended",
|
|
97
|
-
"plugin:@typescript-eslint/recommended",
|
|
98
|
-
"prettier"
|
|
99
|
-
],
|
|
100
|
-
"rules": {
|
|
101
|
-
"@typescript-eslint/no-empty-function": "off",
|
|
102
|
-
"@typescript-eslint/no-explicit-any": "off",
|
|
103
|
-
"@typescript-eslint/ban-ts-ignore": "off",
|
|
104
|
-
"tree-shaking/no-side-effects-in-initialization": "error",
|
|
105
|
-
"@typescript-eslint/ban-ts-comment": "off"
|
|
106
|
-
}
|
|
107
|
-
}
|
|
56
|
+
"homepage": "https://github.com/mesqueeb/getorset-anything#readme",
|
|
57
|
+
"bugs": "https://github.com/mesqueeb/getorset-anything/issues"
|
|
108
58
|
}
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function mapGetOrSet(map, key, initialValue) {
|
|
4
|
-
let val = map.get(key);
|
|
5
|
-
if (val === void 0) {
|
|
6
|
-
val = initialValue();
|
|
7
|
-
map.set(key, val);
|
|
8
|
-
}
|
|
9
|
-
return val;
|
|
10
|
-
}
|
|
11
|
-
function objGetOrSet(obj, key, initialValue) {
|
|
12
|
-
let val = obj[key];
|
|
13
|
-
if (val === void 0) {
|
|
14
|
-
val = initialValue();
|
|
15
|
-
obj[key] = val;
|
|
16
|
-
}
|
|
17
|
-
return val;
|
|
18
|
-
}
|
|
19
|
-
function arrGetOrSet(arr, index, initialValue) {
|
|
20
|
-
let val = arr[index];
|
|
21
|
-
if (val === void 0) {
|
|
22
|
-
val = initialValue();
|
|
23
|
-
arr[index] = val;
|
|
24
|
-
}
|
|
25
|
-
return val;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
exports.arrGetOrSet = arrGetOrSet;
|
|
29
|
-
exports.mapGetOrSet = mapGetOrSet;
|
|
30
|
-
exports.objGetOrSet = objGetOrSet;
|
package/dist/cjs/index.d.cts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
|
|
2
|
-
type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
|
|
3
|
-
/**
|
|
4
|
-
* Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```js
|
|
8
|
-
* const map = new Map<string, number[]>()
|
|
9
|
-
*
|
|
10
|
-
* const arr = mapGetOrSet(map, '123', () => [])
|
|
11
|
-
* // ↳ []
|
|
12
|
-
*
|
|
13
|
-
* arr.push('xyz')
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
declare function mapGetOrSet<M extends Map<unknown, unknown>, T extends () => ValueOfMap<M>>(map: M, key: KeyOfMap<M>, initialValue: T): ReturnType<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```js
|
|
22
|
-
* const obj: Record<string, number[]> = {}
|
|
23
|
-
*
|
|
24
|
-
* const arr = objGetOrSet(obj, '123', () => [])
|
|
25
|
-
* // ↳ []
|
|
26
|
-
*
|
|
27
|
-
* arr.push('xyz')
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
declare function objGetOrSet<O extends Record<string | number | symbol, unknown>, T extends () => O[keyof O]>(obj: O, key: keyof O, initialValue: T): ReturnType<T>;
|
|
31
|
-
/**
|
|
32
|
-
* Retrieve the value in an array, or if it wasn't found, set an initial value and return that.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```js
|
|
36
|
-
* const arr: number[] = []
|
|
37
|
-
*
|
|
38
|
-
* const val = arrGetOrSet(arr, 0, () => 123)
|
|
39
|
-
* // ↳ 123
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
declare function arrGetOrSet<A extends unknown[], T extends () => A[number]>(arr: A, index: number, initialValue: T): ReturnType<T>;
|
|
43
|
-
|
|
44
|
-
export { arrGetOrSet, mapGetOrSet, objGetOrSet };
|