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