getorset-anything 1.0.2 → 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.
Files changed (3) hide show
  1. package/dist/index.d.ts +35 -20
  2. package/dist/index.js +38 -17
  3. package/package.json +13 -13
package/dist/index.d.ts CHANGED
@@ -1,31 +1,47 @@
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;
1
+ export type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
2
+ export type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
3
+ export type KeyOfWeakMap<M extends WeakMap<object, unknown>> = M extends WeakMap<infer K, unknown> ? K : never;
4
+ export type ValueOfWeakMap<M extends WeakMap<object, unknown>> = M extends WeakMap<object, infer V> ? V : never;
3
5
  /**
4
6
  * Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
5
7
  *
6
8
  * @example
7
- * ```js
8
- * const map = new Map<string, number[]>()
9
+ * ;```js
10
+ * const map = new Map<string, number[]>()
9
11
  *
10
- * const arr = mapGetOrSet(map, '123', () => [])
11
- * // ↳ []
12
+ * const arr = mapGetOrSet(map, '123', () => [])
13
+ * // ↳ []
12
14
  *
13
- * arr.push('xyz')
14
- * ```
15
+ * arr.push('xyz')
16
+ * ```
15
17
  */
16
18
  export declare function mapGetOrSet<M extends Map<unknown, unknown>, T extends () => ValueOfMap<M>>(map: M, key: KeyOfMap<M>, initialValue: T): ReturnType<T>;
19
+ /**
20
+ * Retrieve the value in a weak map, or if it wasn't found, set an initial value and return that.
21
+ *
22
+ * @example
23
+ * ;```js
24
+ * const map = new WeakMap<string, number[]>()
25
+ *
26
+ * const arr = weakMapGetOrSet(map, obj, () => [])
27
+ * // ↳ []
28
+ *
29
+ * arr.push('xyz')
30
+ * ```
31
+ */
32
+ export declare function weakMapGetOrSet<M extends WeakMap<object, unknown>, T extends () => ValueOfWeakMap<M>>(map: M, key: KeyOfWeakMap<M>, initialValue: T): ReturnType<T>;
17
33
  /**
18
34
  * Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
19
35
  *
20
36
  * @example
21
- * ```js
22
- * const obj: Record<string, number[]> = {}
37
+ * ;```js
38
+ * const obj: Record<string, number[]> = {}
23
39
  *
24
- * const arr = objGetOrSet(obj, '123', () => [])
25
- * // ↳ []
40
+ * const arr = objGetOrSet(obj, '123', () => [])
41
+ * // ↳ []
26
42
  *
27
- * arr.push('xyz')
28
- * ```
43
+ * arr.push('xyz')
44
+ * ```
29
45
  */
30
46
  export declare function objGetOrSet<O extends {
31
47
  [key in string | number | symbol]: unknown;
@@ -34,12 +50,11 @@ export declare function objGetOrSet<O extends {
34
50
  * Retrieve the value in an array, or if it wasn't found, set an initial value and return that.
35
51
  *
36
52
  * @example
37
- * ```js
38
- * const arr: number[] = []
53
+ * ;```js
54
+ * const arr: number[] = []
39
55
  *
40
- * const val = arrGetOrSet(arr, 0, () => 123)
41
- * // ↳ 123
42
- * ```
56
+ * const val = arrGetOrSet(arr, 0, () => 123)
57
+ * // ↳ 123
58
+ * ```
43
59
  */
44
60
  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
@@ -2,14 +2,14 @@
2
2
  * Retrieve the value in a map, or if it wasn't found, set an initial value and return that.
3
3
  *
4
4
  * @example
5
- * ```js
6
- * const map = new Map<string, number[]>()
5
+ * ;```js
6
+ * const map = new Map<string, number[]>()
7
7
  *
8
- * const arr = mapGetOrSet(map, '123', () => [])
9
- * // ↳ []
8
+ * const arr = mapGetOrSet(map, '123', () => [])
9
+ * // ↳ []
10
10
  *
11
- * arr.push('xyz')
12
- * ```
11
+ * arr.push('xyz')
12
+ * ```
13
13
  */
14
14
  export function mapGetOrSet(map, key, initialValue) {
15
15
  let val = map.get(key);
@@ -19,18 +19,39 @@ export function mapGetOrSet(map, key, initialValue) {
19
19
  }
20
20
  return val;
21
21
  }
22
+ /**
23
+ * Retrieve the value in a weak map, or if it wasn't found, set an initial value and return that.
24
+ *
25
+ * @example
26
+ * ;```js
27
+ * const map = new WeakMap<string, number[]>()
28
+ *
29
+ * const arr = weakMapGetOrSet(map, obj, () => [])
30
+ * // ↳ []
31
+ *
32
+ * arr.push('xyz')
33
+ * ```
34
+ */
35
+ export function weakMapGetOrSet(map, key, initialValue) {
36
+ let val = map.get(key);
37
+ if (val === undefined) {
38
+ val = initialValue();
39
+ map.set(key, val);
40
+ }
41
+ return val;
42
+ }
22
43
  /**
23
44
  * Retrieve the value in an object, or if it wasn't found, set an initial value and return that.
24
45
  *
25
46
  * @example
26
- * ```js
27
- * const obj: Record<string, number[]> = {}
47
+ * ;```js
48
+ * const obj: Record<string, number[]> = {}
28
49
  *
29
- * const arr = objGetOrSet(obj, '123', () => [])
30
- * // ↳ []
50
+ * const arr = objGetOrSet(obj, '123', () => [])
51
+ * // ↳ []
31
52
  *
32
- * arr.push('xyz')
33
- * ```
53
+ * arr.push('xyz')
54
+ * ```
34
55
  */
35
56
  export function objGetOrSet(obj, key, initialValue) {
36
57
  let val = obj[key];
@@ -44,12 +65,12 @@ export function objGetOrSet(obj, key, initialValue) {
44
65
  * Retrieve the value in an array, or if it wasn't found, set an initial value and return that.
45
66
  *
46
67
  * @example
47
- * ```js
48
- * const arr: number[] = []
68
+ * ;```js
69
+ * const arr: number[] = []
49
70
  *
50
- * const val = arrGetOrSet(arr, 0, () => 123)
51
- * // ↳ 123
52
- * ```
71
+ * const val = arrGetOrSet(arr, 0, () => 123)
72
+ * // ↳ 123
73
+ * ```
53
74
  */
54
75
  export function arrGetOrSet(arr, index, initialValue) {
55
76
  let val = arr[index];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "getorset-anything",
3
- "version": "1.0.2",
3
+ "version": "1.1.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,
@@ -10,18 +10,12 @@
10
10
  "engines": {
11
11
  "node": ">=18"
12
12
  },
13
- "scripts": {
14
- "lint": "tsc --noEmit && eslint ./src",
15
- "test": "vitest run",
16
- "build": "del-cli dist && tsc",
17
- "release": "npm run lint && npm run build && np"
18
- },
19
13
  "devDependencies": {
20
- "@cycraft/eslint": "^0.4.3",
14
+ "@cycraft/eslint": "^0.4.4",
21
15
  "@cycraft/tsconfig": "^0.1.2",
22
- "del-cli": "^6.0.0",
23
- "np": "^10.2.0",
24
- "vitest": "^3.0.6"
16
+ "del-cli": "^7.0.0",
17
+ "np": "^12.1.1",
18
+ "vitest": "^5.0.1"
25
19
  },
26
20
  "files": [
27
21
  "dist"
@@ -54,5 +48,11 @@
54
48
  "url": "https://github.com/mesqueeb/getorset-anything.git"
55
49
  },
56
50
  "homepage": "https://github.com/mesqueeb/getorset-anything#readme",
57
- "bugs": "https://github.com/mesqueeb/getorset-anything/issues"
58
- }
51
+ "bugs": "https://github.com/mesqueeb/getorset-anything/issues",
52
+ "scripts": {
53
+ "lint": "tsc --noEmit && eslint ./src",
54
+ "test": "vitest run",
55
+ "build": "del-cli dist && tsc",
56
+ "release": "pnpm run lint && pnpm run build && np"
57
+ }
58
+ }