@augment-vir/common 30.0.0 → 30.0.2

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 (174) hide show
  1. package/README.md +11 -0
  2. package/dist/augments/array/array-map.d.ts +29 -1
  3. package/dist/augments/array/array-map.js +29 -1
  4. package/dist/augments/array/array-to-object.d.ts +27 -9
  5. package/dist/augments/array/array-to-object.js +96 -12
  6. package/dist/augments/array/awaited/awaited-filter.d.ts +29 -0
  7. package/dist/augments/array/awaited/awaited-filter.js +29 -0
  8. package/dist/augments/array/awaited/awaited-for-each.d.ts +28 -3
  9. package/dist/augments/array/awaited/awaited-for-each.js +28 -3
  10. package/dist/augments/array/awaited/awaited-map.d.ts +32 -0
  11. package/dist/augments/array/awaited/awaited-map.js +32 -0
  12. package/dist/augments/array/filter.d.ts +29 -0
  13. package/dist/augments/array/filter.js +61 -0
  14. package/dist/augments/array/remove-duplicates.d.ts +60 -1
  15. package/dist/augments/array/remove-duplicates.js +61 -2
  16. package/dist/augments/array/repeat-array.d.ts +22 -0
  17. package/dist/augments/array/repeat-array.js +22 -0
  18. package/dist/augments/array/shuffle-array.d.ts +8 -1
  19. package/dist/augments/array/shuffle-array.js +8 -1
  20. package/dist/augments/array/string-array.d.ts +8 -0
  21. package/dist/augments/array/string-array.js +8 -0
  22. package/dist/augments/enum/enum-value-check.d.ts +30 -0
  23. package/dist/augments/enum/enum-value-check.js +30 -0
  24. package/dist/augments/error/combine-errors.d.ts +24 -3
  25. package/dist/augments/error/combine-errors.js +26 -4
  26. package/dist/augments/function/call-asynchronously.d.ts +19 -0
  27. package/dist/augments/function/call-asynchronously.js +19 -0
  28. package/dist/augments/function/call-with-retries.d.ts +23 -3
  29. package/dist/augments/function/call-with-retries.js +23 -0
  30. package/dist/augments/function/debounce.d.ts +71 -4
  31. package/dist/augments/function/debounce.js +66 -4
  32. package/dist/augments/function/execution-duration.d.ts +17 -5
  33. package/dist/augments/function/execution-duration.js +16 -3
  34. package/dist/augments/function/if-truthy.d.ts +28 -0
  35. package/dist/augments/function/if-truthy.js +35 -0
  36. package/dist/augments/function/wrap-in-try.d.ts +12 -4
  37. package/dist/augments/function/wrap-in-try.js +64 -0
  38. package/dist/augments/json/append-json.d.ts +3 -3
  39. package/dist/augments/json/append-json.js +33 -0
  40. package/dist/augments/json/copy-through-json.d.ts +32 -1
  41. package/dist/augments/json/copy-through-json.js +32 -1
  42. package/dist/augments/json/json-compatible.d.ts +37 -9
  43. package/dist/augments/json/jsonify.d.ts +30 -1
  44. package/dist/augments/json/jsonify.js +31 -2
  45. package/dist/augments/log/log-colors.d.ts +41 -0
  46. package/dist/augments/log/log-colors.js +43 -2
  47. package/dist/augments/log/log-string.d.ts +21 -0
  48. package/dist/augments/log/log-string.js +9 -2
  49. package/dist/augments/log/log-writer.d.ts +22 -0
  50. package/dist/augments/log/log.d.ts +44 -1
  51. package/dist/augments/log/log.js +46 -3
  52. package/dist/augments/log/logger.d.ts +49 -0
  53. package/dist/augments/log/logger.js +21 -0
  54. package/dist/augments/number/clamp.d.ts +13 -2
  55. package/dist/augments/number/clamp.js +12 -1
  56. package/dist/augments/number/coords.d.ts +6 -2
  57. package/dist/augments/number/digit.d.ts +7 -0
  58. package/dist/augments/number/dimensions.d.ts +6 -2
  59. package/dist/augments/number/min-max.d.ts +9 -2
  60. package/dist/augments/number/min-max.js +4 -2
  61. package/dist/augments/number/number-conversion.d.ts +9 -3
  62. package/dist/augments/number/number-conversion.js +10 -4
  63. package/dist/augments/number/round.d.ts +19 -0
  64. package/dist/augments/number/round.js +19 -0
  65. package/dist/augments/number/scientific.d.ts +18 -1
  66. package/dist/augments/number/scientific.js +18 -1
  67. package/dist/augments/number/truncate-number.d.ts +24 -3
  68. package/dist/augments/number/truncate-number.js +28 -8
  69. package/dist/augments/number/wrap-number.d.ts +9 -3
  70. package/dist/augments/number/wrap-number.js +8 -2
  71. package/dist/augments/object/diff.d.ts +27 -5
  72. package/dist/augments/object/diff.js +20 -4
  73. package/dist/augments/object/empty.d.ts +7 -1
  74. package/dist/augments/object/get-or-set.d.ts +6 -27
  75. package/dist/augments/object/get-or-set.js +66 -4
  76. package/dist/augments/object/key-count.d.ts +4 -0
  77. package/dist/augments/object/map-entries.js +23 -0
  78. package/dist/augments/object/map-enum.d.ts +7 -0
  79. package/dist/augments/object/map-enum.js +24 -0
  80. package/dist/augments/object/map-values.d.ts +25 -13
  81. package/dist/augments/object/map-values.js +29 -21
  82. package/dist/augments/object/merge-deep.d.ts +4 -0
  83. package/dist/augments/object/merge-deep.js +4 -0
  84. package/dist/augments/object/merge-defined-properties.d.ts +13 -0
  85. package/dist/augments/object/merge-defined-properties.js +13 -0
  86. package/dist/augments/object/merge-property-arrays.d.ts +28 -0
  87. package/dist/augments/object/merge-property-arrays.js +28 -0
  88. package/dist/augments/object/object-entries.d.ts +27 -0
  89. package/dist/augments/object/object-entries.js +27 -0
  90. package/dist/augments/object/object-filter.d.ts +20 -0
  91. package/dist/augments/object/object-filter.js +20 -0
  92. package/dist/augments/object/object-keys.d.ts +32 -0
  93. package/dist/augments/object/object-keys.js +32 -0
  94. package/dist/augments/object/object-values.d.ts +9 -0
  95. package/dist/augments/object/object-values.js +9 -0
  96. package/dist/augments/path/esm-path.d.ts +26 -0
  97. package/dist/augments/path/esm-path.js +31 -0
  98. package/dist/augments/prisma/base-prisma-types.d.ts +38 -0
  99. package/dist/augments/prisma/prisma-basic-model.d.ts +29 -0
  100. package/dist/augments/prisma/prisma-basic-model.js +1 -0
  101. package/dist/augments/prisma/prisma-full-model.d.ts +37 -0
  102. package/dist/augments/prisma/prisma-full-model.js +1 -0
  103. package/dist/augments/prisma/prisma-model-create.d.ts +159 -0
  104. package/dist/augments/prisma/prisma-model-create.js +58 -0
  105. package/dist/augments/prisma/prisma-model-name.d.ts +18 -0
  106. package/dist/augments/prisma/prisma-model-name.js +1 -0
  107. package/dist/augments/promise/timed-promise.d.ts +15 -0
  108. package/dist/augments/promise/timed-promise.js +15 -0
  109. package/dist/augments/random/random-boolean.d.ts +6 -0
  110. package/dist/augments/random/random-boolean.js +6 -0
  111. package/dist/augments/random/random-integer.d.ts +3 -1
  112. package/dist/augments/random/random-integer.js +3 -1
  113. package/dist/augments/random/random-string.d.ts +6 -2
  114. package/dist/augments/random/random-string.js +6 -2
  115. package/dist/augments/regexp/match.d.ts +10 -0
  116. package/dist/augments/regexp/match.js +13 -0
  117. package/dist/augments/regexp/regexp-flags.d.ts +56 -3
  118. package/dist/augments/regexp/regexp-flags.js +71 -14
  119. package/dist/augments/regexp/regexp-string.d.ts +4 -0
  120. package/dist/augments/regexp/regexp-string.js +5 -1
  121. package/dist/augments/selection-set/select-collapsed.d.ts +46 -3
  122. package/dist/augments/selection-set/select-collapsed.js +40 -2
  123. package/dist/augments/selection-set/select-from.d.ts +47 -2
  124. package/dist/augments/selection-set/select-from.js +48 -1
  125. package/dist/augments/selection-set/selection-set.d.ts +26 -7
  126. package/dist/augments/selection-set/selection-set.js +1 -4
  127. package/dist/augments/string/casing/capitalization.d.ts +15 -1
  128. package/dist/augments/string/casing/capitalization.js +14 -0
  129. package/dist/augments/string/casing/casing.d.ts +53 -7
  130. package/dist/augments/string/casing/casing.js +36 -11
  131. package/dist/augments/string/casing/kebab-and-camel.d.ts +14 -0
  132. package/dist/augments/string/casing/kebab-and-camel.js +20 -5
  133. package/dist/augments/string/comma.d.ts +26 -0
  134. package/dist/augments/string/{commas.js → comma.js} +21 -2
  135. package/dist/augments/string/join.d.ts +15 -4
  136. package/dist/augments/string/join.js +15 -4
  137. package/dist/augments/string/prefix.d.ts +6 -0
  138. package/dist/augments/string/prefix.js +4 -0
  139. package/dist/augments/string/remove-duplicate-characters.d.ts +17 -0
  140. package/dist/augments/string/remove-duplicate-characters.js +22 -0
  141. package/dist/augments/string/replace.d.ts +17 -0
  142. package/dist/augments/string/replace.js +17 -0
  143. package/dist/augments/string/split.d.ts +29 -2
  144. package/dist/augments/string/split.js +32 -7
  145. package/dist/augments/string/substring-index.d.ts +11 -5
  146. package/dist/augments/string/substring-index.js +12 -3
  147. package/dist/augments/string/suffix.d.ts +22 -0
  148. package/dist/augments/string/suffix.js +16 -0
  149. package/dist/augments/string/white-space.d.ts +8 -1
  150. package/dist/augments/string/white-space.js +8 -1
  151. package/dist/augments/string/wrap-string.d.ts +15 -0
  152. package/dist/augments/string/wrap-string.js +15 -0
  153. package/dist/augments/type/ensure-type.d.ts +4 -0
  154. package/dist/augments/type/ensure-type.js +4 -0
  155. package/dist/augments/type/readonly.d.ts +7 -0
  156. package/dist/augments/type/readonly.js +7 -0
  157. package/dist/augments/type/type-recursion.d.ts +93 -1
  158. package/dist/augments/type/union.d.ts +1 -0
  159. package/dist/augments/type/union.js +1 -0
  160. package/dist/augments/type/void-type.d.ts +6 -2
  161. package/dist/augments/type/writable.d.ts +7 -0
  162. package/dist/augments/type/writable.js +7 -0
  163. package/dist/index.d.ts +11 -5
  164. package/dist/index.js +11 -5
  165. package/package.json +19 -9
  166. package/dist/augments/boolean/if-truthy.d.ts +0 -2
  167. package/dist/augments/boolean/if-truthy.js +0 -9
  168. package/dist/augments/file/esm-path.d.ts +0 -4
  169. package/dist/augments/file/esm-path.js +0 -8
  170. package/dist/augments/prisma/prisma-models.d.ts +0 -106
  171. package/dist/augments/regexp/safe-match.d.ts +0 -1
  172. package/dist/augments/regexp/safe-match.js +0 -4
  173. package/dist/augments/string/commas.d.ts +0 -7
  174. /package/dist/augments/prisma/{prisma-models.js → base-prisma-types.js} +0 -0
@@ -2,21 +2,39 @@ import { PartialDeep } from 'type-fest';
2
2
  /**
3
3
  * Extract all nested object keys and values that are different between the two given objects.
4
4
  *
5
- * @returns An empty tuple if the values are equal.
5
+ * @category Object
6
+ * @category Package : @augment-vir/common
7
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
8
+ * changes in the first value, second entry contains the changes in the second value.
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
6
10
  */
7
11
  export declare function diffObjects<T0 extends Readonly<Record<PropertyKey, unknown>>, T1 extends Readonly<Record<PropertyKey, unknown>>>(object0: T0, object1: T1): [PartialDeep<T0>, PartialDeep<T1>] | [];
8
12
  /**
9
13
  * Extract all entries in the given arrays that are not equal.
10
14
  *
11
- * @returns An empty tuple if the values are equal.
15
+ * @category Object
16
+ * @category Package : @augment-vir/common
17
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
18
+ * changes in the first value, second entry contains the changes in the second value.
19
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
12
20
  */
13
21
  export declare function diffArrays<T0, T1>(array0: ReadonlyArray<T0>, array1: ReadonlyArray<T1>): [Array<T0>, Array<T1>] | [];
14
- /** Callback for checking equality between two values that can be of different types. */
22
+ /**
23
+ * Callback for checking equality between two values that can be of different types.
24
+ *
25
+ * @category Object
26
+ * @category Package : @augment-vir/common
27
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
28
+ */
15
29
  export type AreEqualCallback<T0, T1> = (value0: T0, value1: T1) => boolean;
16
30
  /**
17
31
  * Simple diff check that is useful simply to return the same format as the other diff functions.
18
32
  *
19
- * @returns An empty tuple if the values are equal.
33
+ * @category Object
34
+ * @category Package : @augment-vir/common
35
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
36
+ * changes in the first value, second entry contains the changes in the second value.
37
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
20
38
  */
21
39
  export declare function diffBasic<T0, T1>(value0: T0, value1: T1,
22
40
  /** A custom equality checker. Defaults to a strict equality check (`===`). */
@@ -24,6 +42,10 @@ areEqual?: AreEqualCallback<T0, T1>): [T0, T1] | [];
24
42
  /**
25
43
  * Diff any values. For diffing objects, use `diffObjects` to get better types.
26
44
  *
27
- * @returns An empty tuple if the values are equal.
45
+ * @category Object
46
+ * @category Package : @augment-vir/common
47
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
48
+ * changes in the first value, second entry contains the changes in the second value.
49
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
28
50
  */
29
51
  export declare function diffValues<T0, T1>(value0: T0, value1: T1): [T0, T1] | [];
@@ -3,7 +3,11 @@ import { getObjectTypedKeys } from '@augment-vir/core';
3
3
  /**
4
4
  * Extract all nested object keys and values that are different between the two given objects.
5
5
  *
6
- * @returns An empty tuple if the values are equal.
6
+ * @category Object
7
+ * @category Package : @augment-vir/common
8
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
9
+ * changes in the first value, second entry contains the changes in the second value.
10
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
11
  */
8
12
  export function diffObjects(object0, object1) {
9
13
  const allObjectKeys = Array.from(new Set([
@@ -42,7 +46,11 @@ export function diffObjects(object0, object1) {
42
46
  /**
43
47
  * Extract all entries in the given arrays that are not equal.
44
48
  *
45
- * @returns An empty tuple if the values are equal.
49
+ * @category Object
50
+ * @category Package : @augment-vir/common
51
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
52
+ * changes in the first value, second entry contains the changes in the second value.
53
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
46
54
  */
47
55
  export function diffArrays(array0, array1) {
48
56
  const allArrayIndexes = Array.from(new Set([
@@ -78,7 +86,11 @@ export function diffArrays(array0, array1) {
78
86
  /**
79
87
  * Simple diff check that is useful simply to return the same format as the other diff functions.
80
88
  *
81
- * @returns An empty tuple if the values are equal.
89
+ * @category Object
90
+ * @category Package : @augment-vir/common
91
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
92
+ * changes in the first value, second entry contains the changes in the second value.
93
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
82
94
  */
83
95
  export function diffBasic(value0, value1,
84
96
  /** A custom equality checker. Defaults to a strict equality check (`===`). */
@@ -117,7 +129,11 @@ const orderedValueDiffs = [
117
129
  /**
118
130
  * Diff any values. For diffing objects, use `diffObjects` to get better types.
119
131
  *
120
- * @returns An empty tuple if the values are equal.
132
+ * @category Object
133
+ * @category Package : @augment-vir/common
134
+ * @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
135
+ * changes in the first value, second entry contains the changes in the second value.
136
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
121
137
  */
122
138
  export function diffValues(value0, value1) {
123
139
  let diffOutput = undefined;
@@ -1,3 +1,9 @@
1
1
  import { IsEmptyObject } from 'type-fest';
2
- /** Excludes empty objects from a union. */
2
+ /**
3
+ * Excludes empty objects from a union.
4
+ *
5
+ * @category Object
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
3
9
  export type ExcludeEmpty<T> = IsEmptyObject<T> extends true ? never : T;
@@ -1,31 +1,10 @@
1
1
  import { MaybePromise, type AnyObject } from '@augment-vir/core';
2
- /**
3
- * Get a value from a map or call the callback and return its result and store the result in the
4
- * map.
5
- *
6
- * @category Object:Common
7
- */
8
- export declare function getOrSetFromMap<MapKey extends object, MapValue>(map: WeakMap<MapKey, MapValue>, key: MapKey, createNewValueCallback: () => MapValue): MapValue;
9
- export declare function getOrSetFromMap<MapKey, MapValue>(map: Map<MapKey, MapValue>, key: MapKey, createNewValueCallback: () => MapValue): MapValue;
10
- /**
11
- * Given an object, tries to get the given key in that object. If the key is not in that object,
12
- * then the given `createCallback` is used to create a new value which is then stored in the given
13
- * object and returned. Automatically handles `createCallback` returning a promise, if it does.
14
- *
15
- * @category Object:Common
16
- * @example
17
- *
18
- * ```ts
19
- * // instead of doing this
20
- * if (!myObject[myKey]) {
21
- * myObject[myKey] = {};
22
- * }
23
- * myObject[myKey]![nextKey] = 'some value';
24
- *
25
- * // do this
26
- * getOrSetInObject(myObject, myKey, () => {});
27
- * ```
28
- */
2
+ export declare function getOrSetFromMap<MapKey extends object, MapValue>(map: WeakMap<MapKey, MapValue>, key: MapKey, createCallback: () => MapValue): MapValue;
3
+ export declare function getOrSetFromMap<MapKey, MapValue>(map: Map<MapKey, MapValue>, key: MapKey, createCallback: () => MapValue): MapValue;
4
+ export declare function getOrSetFromMap<MapKey extends object, MapValue>(map: WeakMap<MapKey, MapValue>, key: MapKey, createCallback: () => Promise<MapValue>): Promise<MapValue>;
5
+ export declare function getOrSetFromMap<MapKey, MapValue>(map: Map<MapKey, MapValue>, key: MapKey, createCallback: () => Promise<MapValue>): Promise<MapValue>;
6
+ export declare function getOrSetFromMap<MapKey extends object, MapValue>(map: WeakMap<MapKey, MapValue>, key: MapKey, createCallback: () => MaybePromise<MapValue>): MaybePromise<MapValue>;
7
+ export declare function getOrSetFromMap<MapKey, MapValue>(map: Map<MapKey, MapValue>, key: MapKey, createCallback: () => MaybePromise<MapValue>): MaybePromise<MapValue>;
29
8
  export declare function getOrSet<OriginalObject extends AnyObject, Key extends keyof OriginalObject>(originalObject: OriginalObject, key: Key, createCallback: () => OriginalObject[Key]): Required<OriginalObject>[Key];
30
9
  export declare function getOrSet<OriginalObject extends AnyObject, Key extends keyof OriginalObject>(originalObject: OriginalObject, key: Key, createCallback: () => Promise<OriginalObject[Key]>): Promise<Required<OriginalObject>[Key]>;
31
10
  export declare function getOrSet<OriginalObject extends AnyObject, Key extends keyof OriginalObject>(originalObject: OriginalObject, key: Key, createCallback: () => MaybePromise<OriginalObject[Key]>): MaybePromise<Required<OriginalObject>[Key]>;
@@ -1,17 +1,79 @@
1
1
  import { check } from '@augment-vir/assert';
2
2
  import { ensureError } from '@augment-vir/core';
3
- export function getOrSetFromMap(map, key, createNewValueCallback) {
3
+ /**
4
+ * Given an map, tries to get the given key in that map. If the key is not in that map, then the
5
+ * given `createCallback` is used to create a new value which is then stored in the given map and
6
+ * returned. Automatically handles an async `createCallback`.
7
+ *
8
+ * @category Object
9
+ * @category Package : @augment-vir/common
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * // instead of doing this
14
+ * if (!myMap.get(myKey)) {
15
+ * myMap.set(myKey, {});
16
+ * }
17
+ * myMap.get(myKey)![nextKey] = 'some value';
18
+ *
19
+ * // do this
20
+ * getOrSetInObject(myMap, myKey, () => {
21
+ * return {};
22
+ * });
23
+ * ```
24
+ *
25
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
26
+ */
27
+ export function getOrSetFromMap(map, key, createCallback) {
4
28
  const mapKey = key;
5
29
  if (map.has(mapKey)) {
6
30
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7
31
  return map.get(mapKey);
8
32
  }
9
33
  else {
10
- const newValue = createNewValueCallback();
11
- map.set(mapKey, newValue);
12
- return newValue;
34
+ const createdValue = createCallback();
35
+ if (check.isPromise(createdValue)) {
36
+ return new Promise(async (resolve, reject) => {
37
+ try {
38
+ const awaitedValue = await createdValue;
39
+ map.set(mapKey, awaitedValue);
40
+ resolve(awaitedValue);
41
+ }
42
+ catch (error) {
43
+ reject(ensureError(error));
44
+ }
45
+ });
46
+ }
47
+ else {
48
+ map.set(mapKey, createdValue);
49
+ return createdValue;
50
+ }
13
51
  }
14
52
  }
53
+ /**
54
+ * Given an object, tries to get the given key in that object. If the key is not in that object,
55
+ * then the given `createCallback` is used to create a new value which is then stored in the given
56
+ * object and returned. Automatically handles an async `createCallback`.
57
+ *
58
+ * @category Object
59
+ * @category Package : @augment-vir/common
60
+ * @example
61
+ *
62
+ * ```ts
63
+ * // instead of doing this
64
+ * if (!myObject[myKey]) {
65
+ * myObject[myKey] = {};
66
+ * }
67
+ * myObject[myKey]![nextKey] = 'some value';
68
+ *
69
+ * // do this
70
+ * getOrSetInObject(myObject, myKey, () => {
71
+ * return {};
72
+ * });
73
+ * ```
74
+ *
75
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
76
+ */
15
77
  export function getOrSet(originalObject, key, createCallback) {
16
78
  if (key in originalObject) {
17
79
  return originalObject[key];
@@ -2,6 +2,10 @@ import type { AnyObject } from '@augment-vir/core';
2
2
  /**
3
3
  * Counts the number of unique keys in an object type. Note that a key of just `string` will count
4
4
  * as 1.
5
+ *
6
+ * @category Object
7
+ * @category Package : @augment-vir/common
8
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
5
9
  */
6
10
  export type KeyCount<T extends AnyObject> = UnionToTuple<keyof T>['length'];
7
11
  /**
@@ -2,6 +2,29 @@ import { check } from '@augment-vir/assert';
2
2
  import { ensureError } from '@augment-vir/core';
3
3
  import { filterMap } from '../array/filter.js';
4
4
  import { getObjectTypedEntries, typedObjectFromEntries } from './object-entries.js';
5
+ /**
6
+ * Maps an object. The callback must return a key and value.
7
+ *
8
+ * @category Object
9
+ * @category Package : @augment-vir/common
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * import {mapObject} from '@augment-vir/common';
14
+ *
15
+ * mapObject({a: 1, b: 2}, (key, value) => {
16
+ * return {
17
+ * key: `key-${key}`,
18
+ * value: `value-${value}`,
19
+ * };
20
+ * });
21
+ * // output is `{'key-a': 'value-1', 'key-b': 'value-2'}`
22
+ * ```
23
+ *
24
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
25
+ * @see
26
+ * - {@link mapObjectValues}: the variant that only maps values.
27
+ */
5
28
  export function mapObject(originalObject, mapCallback) {
6
29
  try {
7
30
  let gotAPromise = false;
@@ -1,4 +1,11 @@
1
1
  import type { EnumBaseType, MaybePromise, Values } from '@augment-vir/core';
2
+ /**
3
+ * Creates an object that maps all values of an enum to the provided `Values` type.
4
+ *
5
+ * @category Object
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
2
9
  export type EnumMap<Enum extends EnumBaseType, Value> = Values<Enum> extends PropertyKey ? Record<Values<Enum>, Value> : 'ERROR: invalid enum';
3
10
  export declare function mapEnumToObject<const Enum extends EnumBaseType, const Value>(enumInput: Enum, callback: (enumValue: Values<Enum>) => Promise<Value>): Promise<EnumMap<Enum, Value>>;
4
11
  export declare function mapEnumToObject<const Enum extends EnumBaseType, const Value>(enumInput: Enum, callback: (enumValue: Values<Enum>, wholeEnum: Enum) => Value): Value extends Promise<any> ? Promise<any> extends Value ? Promise<EnumMap<Enum, Awaited<Value>>> : MaybePromise<EnumMap<Enum, Awaited<Value>>> : EnumMap<Enum, Value>;
@@ -1,4 +1,28 @@
1
1
  import { mapObject } from './map-entries.js';
2
+ /**
3
+ * Maps all values of an enum as keys in an object where each value is the callback's output for
4
+ * that key.
5
+ *
6
+ * @category Object
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {mapEnumToObject} from '@augment-vir/common';
12
+ *
13
+ * enum MyEnum {
14
+ * A = 'a',
15
+ * B = 'b',
16
+ * }
17
+ *
18
+ * mapEnumToObject(MyEnum, (enumValue) => {
19
+ * return `value-${enumValue}`;
20
+ * });
21
+ * // output is `{[MyEnum.A]: 'value-a', [MyEnum.B]: 'value-b'}`
22
+ * ```
23
+ *
24
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
25
+ */
2
26
  export function mapEnumToObject(enumInput, callback) {
3
27
  return mapObject(enumInput, (enumKey, enumValue) => {
4
28
  const key = enumValue;
@@ -1,23 +1,35 @@
1
- import { type Values } from '@augment-vir/core';
2
- export type InnerMappedValues<EntireInputGeneric extends object, MappedValueGeneric> = {
1
+ type InnerMappedValues<EntireInputGeneric extends object, MappedValueGeneric> = {
3
2
  [MappedProp in keyof EntireInputGeneric]: MappedValueGeneric;
4
3
  };
5
- export type MappedValues<EntireInputGeneric extends object, MappedValueGeneric> = MappedValueGeneric extends PromiseLike<unknown> ? Promise<InnerMappedValues<EntireInputGeneric, Awaited<MappedValueGeneric>>> : InnerMappedValues<EntireInputGeneric, Awaited<MappedValueGeneric>>;
4
+ type MappedValues<EntireInputGeneric extends object, MappedValueGeneric> = MappedValueGeneric extends PromiseLike<unknown> ? Promise<InnerMappedValues<EntireInputGeneric, Awaited<MappedValueGeneric>>> : InnerMappedValues<EntireInputGeneric, Awaited<MappedValueGeneric>>;
6
5
  /**
7
- * Map an object's keys to new values synchronously. This is different from plain mapObjectValues in
8
- * that this will not wrap the return value in a promise if any of the new object values are
9
- * promises. This function also requires currying in order to get the types correct. This allows you
10
- * to explicitly state the return type.
6
+ * Creates a new object with the same keys as the input object, but with values set to the result of
7
+ * `mapCallback` for each property. This is the same as {@link mapObjectValues} except that this
8
+ * preserves Promise values: it doesn't wrap them all in a single promise.
11
9
  *
10
+ * @category Object
11
+ * @category Package : @augment-vir/common
12
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
13
+ */
14
+ export declare function mapObjectValuesSync<EntireInputGeneric extends object, MappedValueGeneric>(inputObject: EntireInputGeneric, mapCallback: (inputKey: keyof EntireInputGeneric, keyValue: Required<EntireInputGeneric>[typeof inputKey], fullObject: EntireInputGeneric) => MappedValueGeneric): InnerMappedValues<EntireInputGeneric, MappedValueGeneric>;
15
+ /**
16
+ * Creates a new object with the same keys as the input object, but with values set to the result of
17
+ * `mapCallback` for each property. Automatically handles an async `mapCallback`.
18
+ *
19
+ * @category Object
20
+ * @category Package : @augment-vir/common
12
21
  * @example
13
22
  *
14
23
  * ```ts
15
- * mapObjectValuesSync({objectToIterateOver: 'initial value'})(callback);
24
+ * import {mapObjectValues} from '@augment-vir/common';
25
+ *
26
+ * mapObjectValues({a: 1, b: 2}, (key, value) => {
27
+ * return `key-${key} value-${value}`;
28
+ * });
29
+ * // output is `{a: 'key-a value-1', b: 'key-b value-2'}`
16
30
  * ```
17
- */
18
- export declare function mapObjectValuesSync<EntireInputGeneric extends object>(inputObject: EntireInputGeneric): <OutputObjectGeneric extends object>(mapCallback: (inputKey: keyof EntireInputGeneric, keyValue: Required<EntireInputGeneric>[typeof inputKey], fullObject: EntireInputGeneric) => never extends Values<OutputObjectGeneric> ? any : Values<OutputObjectGeneric>) => OutputObjectGeneric;
19
- /**
20
- * Creates a new object with the same properties as the input object, but with values set to the
21
- * result of mapCallback for each property.
31
+ *
32
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
22
33
  */
23
34
  export declare function mapObjectValues<EntireInputGeneric extends object, MappedValueGeneric>(inputObject: EntireInputGeneric, mapCallback: (inputKey: keyof EntireInputGeneric, keyValue: Required<EntireInputGeneric>[typeof inputKey], fullObject: EntireInputGeneric) => MappedValueGeneric): MappedValues<EntireInputGeneric, MappedValueGeneric>;
35
+ export {};
@@ -1,31 +1,39 @@
1
1
  import { ensureError, getObjectTypedKeys } from '@augment-vir/core';
2
2
  /**
3
- * Map an object's keys to new values synchronously. This is different from plain mapObjectValues in
4
- * that this will not wrap the return value in a promise if any of the new object values are
5
- * promises. This function also requires currying in order to get the types correct. This allows you
6
- * to explicitly state the return type.
3
+ * Creates a new object with the same keys as the input object, but with values set to the result of
4
+ * `mapCallback` for each property. This is the same as {@link mapObjectValues} except that this
5
+ * preserves Promise values: it doesn't wrap them all in a single promise.
7
6
  *
7
+ * @category Object
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
10
+ */
11
+ export function mapObjectValuesSync(inputObject, mapCallback) {
12
+ const mappedObject = getObjectTypedKeys(inputObject).reduce((accum, currentKey) => {
13
+ const mappedValue = mapCallback(currentKey, inputObject[currentKey], inputObject);
14
+ accum[currentKey] = mappedValue;
15
+ return accum;
16
+ }, {});
17
+ return mappedObject;
18
+ }
19
+ /**
20
+ * Creates a new object with the same keys as the input object, but with values set to the result of
21
+ * `mapCallback` for each property. Automatically handles an async `mapCallback`.
22
+ *
23
+ * @category Object
24
+ * @category Package : @augment-vir/common
8
25
  * @example
9
26
  *
10
27
  * ```ts
11
- * mapObjectValuesSync({objectToIterateOver: 'initial value'})(callback);
28
+ * import {mapObjectValues} from '@augment-vir/common';
29
+ *
30
+ * mapObjectValues({a: 1, b: 2}, (key, value) => {
31
+ * return `key-${key} value-${value}`;
32
+ * });
33
+ * // output is `{a: 'key-a value-1', b: 'key-b value-2'}`
12
34
  * ```
13
- */
14
- export function mapObjectValuesSync(inputObject) {
15
- function innerMap(mapCallback) {
16
- return getObjectTypedKeys(inputObject).reduce((accum, currentKey) => {
17
- const mappedValue = mapCallback(currentKey, inputObject[currentKey], inputObject);
18
- return {
19
- ...accum,
20
- [currentKey]: mappedValue,
21
- };
22
- }, {});
23
- }
24
- return innerMap;
25
- }
26
- /**
27
- * Creates a new object with the same properties as the input object, but with values set to the
28
- * result of mapCallback for each property.
35
+ *
36
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
29
37
  */
30
38
  export function mapObjectValues(inputObject, mapCallback) {
31
39
  let gotAPromise = false;
@@ -4,6 +4,10 @@ import { PartialDeep } from 'type-fest';
4
4
  * undefined will be removed.
5
5
  *
6
6
  * Note that order matters! Each input object will overwrite the properties of the previous objects.
7
+ *
8
+ * @category Object : Merge
9
+ * @category Package : @augment-vir/common
10
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
11
  */
8
12
  export declare function mergeDeep<const T extends object>(...inputs: (Readonly<T> | Readonly<PartialDeep<T, {
9
13
  recurseIntoArrays: true;
@@ -4,6 +4,10 @@ import { check } from '@augment-vir/assert';
4
4
  * undefined will be removed.
5
5
  *
6
6
  * Note that order matters! Each input object will overwrite the properties of the previous objects.
7
+ *
8
+ * @category Object : Merge
9
+ * @category Package : @augment-vir/common
10
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
11
  */
8
12
  export function mergeDeep(...inputs) {
9
13
  if (!check.isLengthAtLeast(inputs, 1)) {
@@ -2,5 +2,18 @@ import type { AnyObject, PartialWithNullable } from '@augment-vir/core';
2
2
  /**
3
3
  * Merge all objects together but ignore any override values that are `undefined` or `null` or
4
4
  * missing. This only merges objects at the top level, it is not a deep merge.
5
+ *
6
+ * @category Object : Merge
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {mergeDefinedProperties} from '@augment-vir/common';
12
+ *
13
+ * mergeDefinedProperties({a: 'default', b: 'default'}, {a: 'override', b: undefined});
14
+ * // output is `{a: 'override', b: 'default'}`
15
+ * ```
16
+ *
17
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
5
18
  */
6
19
  export declare function mergeDefinedProperties<const T extends AnyObject>(original: T, ...overrides: ReadonlyArray<PartialWithNullable<NoInfer<T>> | undefined>): T;
@@ -2,6 +2,19 @@ import { getObjectTypedEntries } from './object-entries.js';
2
2
  /**
3
3
  * Merge all objects together but ignore any override values that are `undefined` or `null` or
4
4
  * missing. This only merges objects at the top level, it is not a deep merge.
5
+ *
6
+ * @category Object : Merge
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {mergeDefinedProperties} from '@augment-vir/common';
12
+ *
13
+ * mergeDefinedProperties({a: 'default', b: 'default'}, {a: 'override', b: undefined});
14
+ * // output is `{a: 'override', b: 'default'}`
15
+ * ```
16
+ *
17
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
5
18
  */
6
19
  export function mergeDefinedProperties(original, ...overrides) {
7
20
  const finalObject = { ...original };
@@ -1 +1,29 @@
1
+ /**
2
+ * Merges all arrays by their property in the given objects.
3
+ *
4
+ * @category Object : Merge
5
+ * @category Package : @augment-vir/common
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {mergePropertyArrays} from '@augment-vir/common';
10
+ *
11
+ * mergePropertyArrays(
12
+ * {
13
+ * a: [
14
+ * 'a',
15
+ * 'b',
16
+ * ],
17
+ * },
18
+ * {
19
+ * a: [
20
+ * 'c',
21
+ * 'd',
22
+ * ],
23
+ * },
24
+ * ); // output is `{a: ['a', 'b', 'c', 'd']}`
25
+ * ```
26
+ *
27
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
28
+ */
1
29
  export declare function mergePropertyArrays<T extends Record<PropertyKey, unknown[]>>(...inputs: ReadonlyArray<Readonly<T>>): T;
@@ -1,4 +1,32 @@
1
1
  import { getOrSet } from './get-or-set.js';
2
+ /**
3
+ * Merges all arrays by their property in the given objects.
4
+ *
5
+ * @category Object : Merge
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {mergePropertyArrays} from '@augment-vir/common';
11
+ *
12
+ * mergePropertyArrays(
13
+ * {
14
+ * a: [
15
+ * 'a',
16
+ * 'b',
17
+ * ],
18
+ * },
19
+ * {
20
+ * a: [
21
+ * 'c',
22
+ * 'd',
23
+ * ],
24
+ * },
25
+ * ); // output is `{a: ['a', 'b', 'c', 'd']}`
26
+ * ```
27
+ *
28
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
29
+ */
2
30
  export function mergePropertyArrays(...inputs) {
3
31
  const combined = {};
4
32
  inputs.forEach((input) => {
@@ -1,4 +1,31 @@
1
1
  import { CompleteRequire } from '@augment-vir/core';
2
+ /**
3
+ * Gets an object's entries. This is the same as
4
+ * [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
5
+ * except that it has better TypeScript types.
6
+ *
7
+ * @category Object
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
10
+ */
2
11
  export declare function getObjectTypedEntries<const ObjectGeneric>(input: ObjectGeneric): [keyof ObjectGeneric, CompleteRequire<ObjectGeneric>[keyof CompleteRequire<ObjectGeneric>]][];
12
+ /**
13
+ * Create an object from an array of entries. This is the same as
14
+ * [`Object.fromEntries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
15
+ * except that it has better TypeScript types.
16
+ *
17
+ * @category Object
18
+ * @category Package : @augment-vir/common
19
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
20
+ */
3
21
  export declare function typedObjectFromEntries<const KeyType extends PropertyKey, const ValueType>(entries: ReadonlyArray<Readonly<[KeyType, ValueType]>>): Record<KeyType, ValueType>;
22
+ /**
23
+ * Gets an object's entries and sorts them by their key values. This is the same as
24
+ * [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
25
+ * except that it has better TypeScript types and sorts the entries.
26
+ *
27
+ * @category Object
28
+ * @category Package : @augment-vir/common
29
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
30
+ */
4
31
  export declare function getEntriesSortedByKey<const ObjectGeneric>(input: ObjectGeneric): [keyof ObjectGeneric, CompleteRequire<ObjectGeneric>[keyof CompleteRequire<ObjectGeneric>]][];
@@ -1,13 +1,40 @@
1
1
  import { getObjectTypedKeys } from '@augment-vir/core';
2
+ /**
3
+ * Gets an object's entries. This is the same as
4
+ * [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
5
+ * except that it has better TypeScript types.
6
+ *
7
+ * @category Object
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
10
+ */
2
11
  export function getObjectTypedEntries(input) {
3
12
  return getObjectTypedKeys(input).map((key) => [
4
13
  key,
5
14
  input[key],
6
15
  ]);
7
16
  }
17
+ /**
18
+ * Create an object from an array of entries. This is the same as
19
+ * [`Object.fromEntries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
20
+ * except that it has better TypeScript types.
21
+ *
22
+ * @category Object
23
+ * @category Package : @augment-vir/common
24
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
25
+ */
8
26
  export function typedObjectFromEntries(entries) {
9
27
  return Object.fromEntries(entries);
10
28
  }
29
+ /**
30
+ * Gets an object's entries and sorts them by their key values. This is the same as
31
+ * [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
32
+ * except that it has better TypeScript types and sorts the entries.
33
+ *
34
+ * @category Object
35
+ * @category Package : @augment-vir/common
36
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
37
+ */
11
38
  export function getEntriesSortedByKey(input) {
12
39
  return getObjectTypedEntries(input).sort((tupleA, tupleB) => String(tupleA[0]).localeCompare(String(tupleB[0])));
13
40
  }