@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
@@ -1,7 +1,66 @@
1
- export function removeDuplicates(originalArray, calculateUniqueIdCallback) {
1
+ /**
2
+ * Removes duplicates from an array. Optionally provide a callback for calculating a unique id for
3
+ * entries.
4
+ *
5
+ * @category Array
6
+ * @category Package : @augment-vir/common
7
+ * @example No callback
8
+ *
9
+ * ```ts
10
+ * import {removeDuplicates} from '@augment-vir/common';
11
+ *
12
+ * const result = removeDuplicates([
13
+ * 1,
14
+ * 1,
15
+ * 1,
16
+ * 1,
17
+ * 1,
18
+ * 2,
19
+ * 4,
20
+ * ]);
21
+ * // result is `[1, 2, 4]`
22
+ *
23
+ * const exampleEntry = {id: 5};
24
+ *
25
+ * const result2 = removeDuplicates([
26
+ * {id: 1},
27
+ * // this entry will not get filtered out because it's a new object reference
28
+ * {id: 1},
29
+ * exampleEntry,
30
+ * // this `exampleEntry` will get filtered out because it's the same reference as the one above
31
+ * exampleEntry,
32
+ * {id: 4},
33
+ * ]);
34
+ * // result2 is `[{id: 1}, {id: 1}, exampleEntry, {id: 4}]`
35
+ * ```
36
+ *
37
+ * @example With callback
38
+ *
39
+ * ```ts
40
+ * import {removeDuplicates} from '@augment-vir/common';
41
+ *
42
+ * const exampleEntry = {id: 5};
43
+ *
44
+ * const result2 = removeDuplicates(
45
+ * [
46
+ * {id: 1},
47
+ * {id: 1},
48
+ * exampleEntry,
49
+ * exampleEntry,
50
+ * {id: 4},
51
+ * ],
52
+ * (entry) => entry.id,
53
+ * );
54
+ * // result2 is `[{id: 1}, exampleEntry, {id: 4}]`
55
+ * ```
56
+ *
57
+ * @returns A new array (does not mutate).
58
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
59
+ */
60
+ export function removeDuplicates(originalArray, calculateUniqueId = (entry) => entry) {
2
61
  const grouped = new Map();
3
62
  return originalArray.filter((entry) => {
4
- const uniqueId = calculateUniqueIdCallback(entry);
63
+ const uniqueId = calculateUniqueId(entry);
5
64
  if (grouped.get(uniqueId)) {
6
65
  return false;
7
66
  }
@@ -1 +1,23 @@
1
+ /**
2
+ * Repeats an array. Constructs a new array with the entries from the original repeated the given
3
+ * number of times.
4
+ *
5
+ * @category Array
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {repeatArray} from '@augment-vir/common';
11
+ *
12
+ * const result = repeatArray(3, [
13
+ * 'a',
14
+ * 'b',
15
+ * 'c',
16
+ * ]);
17
+ * // result is `['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']`
18
+ * ```
19
+ *
20
+ * @returns A new array (does not mutate).
21
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
22
+ */
1
23
  export declare function repeatArray<T>(repeatCount: number, array: T[]): T[];
@@ -1,3 +1,25 @@
1
+ /**
2
+ * Repeats an array. Constructs a new array with the entries from the original repeated the given
3
+ * number of times.
4
+ *
5
+ * @category Array
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {repeatArray} from '@augment-vir/common';
11
+ *
12
+ * const result = repeatArray(3, [
13
+ * 'a',
14
+ * 'b',
15
+ * 'c',
16
+ * ]);
17
+ * // result is `['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']`
18
+ * ```
19
+ *
20
+ * @returns A new array (does not mutate).
21
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
22
+ */
1
23
  export function repeatArray(repeatCount, array) {
2
24
  return Array.from({ length: repeatCount }, () => [...array]).flat();
3
25
  }
@@ -1,2 +1,9 @@
1
- /** Creates a new array which is a shuffled version of the input array. */
1
+ /**
2
+ * Shuffles the positions of an array's entries (without mutating the array).
3
+ *
4
+ * @category Array
5
+ * @category Package : @augment-vir/common
6
+ * @returns A new array (does not mutate).
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
2
9
  export declare function shuffleArray<ArrayElementType>(input: ReadonlyArray<ArrayElementType>): Array<ArrayElementType>;
@@ -1,5 +1,12 @@
1
1
  import { randomString } from '../random/random-string.js';
2
- /** Creates a new array which is a shuffled version of the input array. */
2
+ /**
3
+ * Shuffles the positions of an array's entries (without mutating the array).
4
+ *
5
+ * @category Array
6
+ * @category Package : @augment-vir/common
7
+ * @returns A new array (does not mutate).
8
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
9
+ */
3
10
  export function shuffleArray(input) {
4
11
  return input
5
12
  .map((value) => {
@@ -1 +1,9 @@
1
+ /**
2
+ * Trims all entries in an array.
3
+ *
4
+ * @category Array
5
+ * @category Package : @augment-vir/common
6
+ * @returns A new array (does not mutate).
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
1
9
  export declare function trimArrayStrings(input: ReadonlyArray<string>): string[];
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Trims all entries in an array.
3
+ *
4
+ * @category Array
5
+ * @category Package : @augment-vir/common
6
+ * @returns A new array (does not mutate).
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
1
9
  export function trimArrayStrings(input) {
2
10
  return input.map((line) => line.trim()).filter((line) => line !== '');
3
11
  }
@@ -1,2 +1,32 @@
1
1
  import type { EnumBaseType } from '@augment-vir/core';
2
+ /**
3
+ * Filters the input array to all valid values from the given enum.
4
+ *
5
+ * @category Array
6
+ * @category Enum
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * enum MyEnum {
12
+ * A = 'a',
13
+ * B = 'b',
14
+ * }
15
+ *
16
+ * const result = filterToEnumValues(
17
+ * [
18
+ * 1,
19
+ * 2,
20
+ * 3,
21
+ * 'a',
22
+ * 'b',
23
+ * MyEnum.A,
24
+ * ],
25
+ * MyEnum,
26
+ * ); // result is `[MyEnum.A, MyEnum.B, MyEnum.A]`
27
+ * ```
28
+ *
29
+ * @returns A new array (does not mutate).
30
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
31
+ */
2
32
  export declare function filterToEnumValues<const T extends EnumBaseType>(inputs: ReadonlyArray<unknown>, checkEnum: T): T[keyof T][];
@@ -1,4 +1,34 @@
1
1
  import { check } from '@augment-vir/assert';
2
+ /**
3
+ * Filters the input array to all valid values from the given enum.
4
+ *
5
+ * @category Array
6
+ * @category Enum
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * enum MyEnum {
12
+ * A = 'a',
13
+ * B = 'b',
14
+ * }
15
+ *
16
+ * const result = filterToEnumValues(
17
+ * [
18
+ * 1,
19
+ * 2,
20
+ * 3,
21
+ * 'a',
22
+ * 'b',
23
+ * MyEnum.A,
24
+ * ],
25
+ * MyEnum,
26
+ * ); // result is `[MyEnum.A, MyEnum.B, MyEnum.A]`
27
+ * ```
28
+ *
29
+ * @returns A new array (does not mutate).
30
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
31
+ */
2
32
  export function filterToEnumValues(inputs, checkEnum) {
3
33
  return inputs.filter((input) => check.isEnumValue(input, checkEnum));
4
34
  }
@@ -1,3 +1,24 @@
1
- import { type AtLeastTuple } from '@augment-vir/core';
2
- export declare function combineErrors(errors: AtLeastTuple<Error, 1>): Error;
3
- export declare function combineErrors(errors: ReadonlyArray<Error | undefined>): Error | undefined;
1
+ /**
2
+ * Combines an array of errors into a single array.
3
+ *
4
+ * - If no errors are in the given array, a new Error with an empty message is returned.
5
+ * - If only one error is in the given array, it is directly returned without modification.
6
+ *
7
+ * @category Array
8
+ * @category Error
9
+ * @category Package : @augment-vir/common
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * import {combineErrors} from '@augment-vir/common';
14
+ *
15
+ * const result1 = combineErrors([
16
+ * new Error('message 1'),
17
+ * new Error('message 2'),
18
+ * ]); // result1 is a single error with the message 'message 1\nmessage 2'
19
+ * ```
20
+ *
21
+ * @returns A single error.
22
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
23
+ */
24
+ export declare function combineErrors(errors: ReadonlyArray<Error>): Error;
@@ -1,11 +1,33 @@
1
1
  import { check } from '@augment-vir/assert';
2
2
  import { extractErrorMessage } from '@augment-vir/core';
3
- export function combineErrors(rawErrors) {
4
- const errors = rawErrors.filter((error) => error);
3
+ /**
4
+ * Combines an array of errors into a single array.
5
+ *
6
+ * - If no errors are in the given array, a new Error with an empty message is returned.
7
+ * - If only one error is in the given array, it is directly returned without modification.
8
+ *
9
+ * @category Array
10
+ * @category Error
11
+ * @category Package : @augment-vir/common
12
+ * @example
13
+ *
14
+ * ```ts
15
+ * import {combineErrors} from '@augment-vir/common';
16
+ *
17
+ * const result1 = combineErrors([
18
+ * new Error('message 1'),
19
+ * new Error('message 2'),
20
+ * ]); // result1 is a single error with the message 'message 1\nmessage 2'
21
+ * ```
22
+ *
23
+ * @returns A single error.
24
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
25
+ */
26
+ export function combineErrors(errors) {
5
27
  if (!check.isLengthAtLeast(errors, 1)) {
6
- return undefined;
28
+ return new Error();
7
29
  }
8
- if (errors.length === 1) {
30
+ else if (errors.length === 1) {
9
31
  return errors[0];
10
32
  }
11
33
  return new Error(errors.map((error) => extractErrorMessage(error).trim()).join('\n'));
@@ -2,5 +2,24 @@ import { MaybePromise } from '@augment-vir/core';
2
2
  /**
3
3
  * Call a function asynchronously without interrupting current synchronous execution, even if the
4
4
  * function was originally synchronous.
5
+ *
6
+ * @category Function
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {callAsynchronously} from '@augment-vir/common';
12
+ *
13
+ * console.info('1');
14
+ * const later = callAsynchronously(() => {
15
+ * console.info('3');
16
+ * });
17
+ * console.info('2');
18
+ * await later;
19
+ *
20
+ * // logs 1,2,3 in numeric order
21
+ * ```
22
+ *
23
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
5
24
  */
6
25
  export declare function callAsynchronously<T>(callback: () => MaybePromise<T>): Promise<T>;
@@ -1,6 +1,25 @@
1
1
  /**
2
2
  * Call a function asynchronously without interrupting current synchronous execution, even if the
3
3
  * function was originally synchronous.
4
+ *
5
+ * @category Function
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {callAsynchronously} from '@augment-vir/common';
11
+ *
12
+ * console.info('1');
13
+ * const later = callAsynchronously(() => {
14
+ * console.info('3');
15
+ * });
16
+ * console.info('2');
17
+ * await later;
18
+ *
19
+ * // logs 1,2,3 in numeric order
20
+ * ```
21
+ *
22
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
4
23
  */
5
24
  export async function callAsynchronously(callback) {
6
25
  return await Promise.resolve().then(() => callback());
@@ -1,4 +1,24 @@
1
- import { MaybePromise } from '@augment-vir/core';
2
- export declare function callWithRetries<const T>(maxRetries: number, callback: () => Promise<T>): Promise<T>;
1
+ /**
2
+ * Calls `callback` until it doesn't throw an error or throws an error when `maxRetries` is reached.
3
+ * Similar to the `waitUntil` guard from '@augment-vir/assert' but doesn't check the callback's
4
+ * output.
5
+ *
6
+ * @category Function
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {callWithRetries} from '@augment-vir/common';
12
+ *
13
+ * const result = callWithRetries(5, () => {
14
+ * if (Math.random() < 0.5) {
15
+ * return 'done';
16
+ * } else {
17
+ * throw new Error();
18
+ * }
19
+ * });
20
+ * ```
21
+ *
22
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
23
+ */
3
24
  export declare function callWithRetries<const T>(maxRetries: number, callback: () => T): T;
4
- export declare function callWithRetries<const T>(maxRetries: number, callback: () => MaybePromise<T>): MaybePromise<T>;
@@ -1,4 +1,27 @@
1
1
  import { ensureErrorAndPrependMessage } from '@augment-vir/core';
2
+ /**
3
+ * Calls `callback` until it doesn't throw an error or throws an error when `maxRetries` is reached.
4
+ * Similar to the `waitUntil` guard from '@augment-vir/assert' but doesn't check the callback's
5
+ * output.
6
+ *
7
+ * @category Function
8
+ * @category Package : @augment-vir/common
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import {callWithRetries} from '@augment-vir/common';
13
+ *
14
+ * const result = callWithRetries(5, () => {
15
+ * if (Math.random() < 0.5) {
16
+ * return 'done';
17
+ * } else {
18
+ * throw new Error();
19
+ * }
20
+ * });
21
+ * ```
22
+ *
23
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
24
+ */
2
25
  export function callWithRetries(maxRetries, callback) {
3
26
  try {
4
27
  const result = callback();
@@ -1,22 +1,89 @@
1
1
  import { MaybePromise } from '@augment-vir/core';
2
2
  import { AnyDuration } from '@date-vir/duration';
3
+ /**
4
+ * Different types of debouncing for the {@link Debounce} class.
5
+ *
6
+ * @category Function
7
+ * @category Package : @augment-vir/common
8
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
9
+ */
3
10
  export declare enum DebounceStyle {
4
11
  /**
5
- * Fires on the first call, then waits the given amount of milliseconds until a subsequent call
6
- * can be made.
12
+ * Fires on the first call, then waits the given amount of milliseconds until another call is
13
+ * allowed through.
14
+ *
15
+ * `.execute()` calls with a 25ms debounce time looks like this:
16
+ *
17
+ * | 1st `.execute()` | 2nd `.execute()` | 3rd `.execute()` | 4th `.execute()` |
18
+ * | ---------------- | ---------------- | ---------------- | ---------------- |
19
+ * | 0ms | 10ms | 20ms | 30ms |
20
+ * | fired! | | | fired! |
7
21
  */
8
22
  FirstThenWait = "first-then-wait",
9
23
  /**
10
24
  * Waits the given amount of milliseconds after the first call and then fires the latest
11
25
  * assigned callback.
26
+ *
27
+ * `.execute()` calls with a 25ms debounce time looks like this:
28
+ *
29
+ * | 1st `.execute()` | 2nd `.execute()` | 3rd `.execute()` | - | 4th `.execute()` | ... |
30
+ * | ---------------- | ---------------- | ---------------- | ------ | ---------------- | ------ |
31
+ * | 0ms | 10ms | 20ms | 25ms | 30ms | 50ms |
32
+ * | | | | fired! | | fired! |
12
33
  */
13
34
  AfterWait = "after-wait"
14
35
  }
36
+ /**
37
+ * Enable debouncing of callbacks, with various styles of debounce supported in {@link DebounceStyle}
38
+ * (see its docs for debounce style details). A callback can be provided on construction or to the
39
+ * `.execute()` method.
40
+ *
41
+ * @category Function
42
+ * @category Package : @augment-vir/common
43
+ * @example
44
+ *
45
+ * ```ts
46
+ * import {Debounce} from '@augment-vir/common';
47
+ *
48
+ * const debounce = new Debounce(
49
+ * DebounceStyle.FirstThenWait,
50
+ * {
51
+ * milliseconds: 500,
52
+ * },
53
+ * // callback can optionally be provided on construction
54
+ * () => {
55
+ * console.log('called');
56
+ * },
57
+ * );
58
+ *
59
+ * debounce.execute();
60
+ * // providing a callback in `.execute()` permanently overrides the callback provided in construction.
61
+ * debounce.execute(() => {});
62
+ * ```
63
+ *
64
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
65
+ */
15
66
  export declare class Debounce {
67
+ /** Debounce style. See {@link DebounceStyle} for more details. */
16
68
  debounceStyle: DebounceStyle;
69
+ /** Duration between debounces. */
17
70
  debounceDuration: AnyDuration;
71
+ /**
72
+ * Set the callback to be triggered on `.execute()`. If this is not set, the callback to be
73
+ * called can be passed in `.execute()` instead.
74
+ */
75
+ callback?: (() => MaybePromise<void>) | undefined;
18
76
  nextCallTimestamp: number;
19
- callback: (() => MaybePromise<void>) | undefined;
20
- constructor(debounceStyle: DebounceStyle, debounceDuration: AnyDuration, callback?: typeof this.callback | undefined);
77
+ constructor(
78
+ /** Debounce style. See {@link DebounceStyle} for more details. */
79
+ debounceStyle: DebounceStyle,
80
+ /** Duration between debounces. */
81
+ debounceDuration: AnyDuration,
82
+ /**
83
+ * Set the callback to be triggered on `.execute()`. If this is not set, the callback to be
84
+ * called can be passed in `.execute()` instead.
85
+ */
86
+ callback?: (() => MaybePromise<void>) | undefined);
87
+ /** Call the callback, if one has been set yet, if the current debounce timer is up. */
21
88
  execute(callback?: typeof this.callback | undefined): void;
22
89
  }
@@ -1,29 +1,91 @@
1
1
  import { convertDuration, DurationUnit } from '@date-vir/duration';
2
+ /**
3
+ * Different types of debouncing for the {@link Debounce} class.
4
+ *
5
+ * @category Function
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
2
9
  export var DebounceStyle;
3
10
  (function (DebounceStyle) {
4
11
  /**
5
- * Fires on the first call, then waits the given amount of milliseconds until a subsequent call
6
- * can be made.
12
+ * Fires on the first call, then waits the given amount of milliseconds until another call is
13
+ * allowed through.
14
+ *
15
+ * `.execute()` calls with a 25ms debounce time looks like this:
16
+ *
17
+ * | 1st `.execute()` | 2nd `.execute()` | 3rd `.execute()` | 4th `.execute()` |
18
+ * | ---------------- | ---------------- | ---------------- | ---------------- |
19
+ * | 0ms | 10ms | 20ms | 30ms |
20
+ * | fired! | | | fired! |
7
21
  */
8
22
  DebounceStyle["FirstThenWait"] = "first-then-wait";
9
23
  /**
10
24
  * Waits the given amount of milliseconds after the first call and then fires the latest
11
25
  * assigned callback.
26
+ *
27
+ * `.execute()` calls with a 25ms debounce time looks like this:
28
+ *
29
+ * | 1st `.execute()` | 2nd `.execute()` | 3rd `.execute()` | - | 4th `.execute()` | ... |
30
+ * | ---------------- | ---------------- | ---------------- | ------ | ---------------- | ------ |
31
+ * | 0ms | 10ms | 20ms | 25ms | 30ms | 50ms |
32
+ * | | | | fired! | | fired! |
12
33
  */
13
34
  DebounceStyle["AfterWait"] = "after-wait";
14
35
  })(DebounceStyle || (DebounceStyle = {}));
36
+ /**
37
+ * Enable debouncing of callbacks, with various styles of debounce supported in {@link DebounceStyle}
38
+ * (see its docs for debounce style details). A callback can be provided on construction or to the
39
+ * `.execute()` method.
40
+ *
41
+ * @category Function
42
+ * @category Package : @augment-vir/common
43
+ * @example
44
+ *
45
+ * ```ts
46
+ * import {Debounce} from '@augment-vir/common';
47
+ *
48
+ * const debounce = new Debounce(
49
+ * DebounceStyle.FirstThenWait,
50
+ * {
51
+ * milliseconds: 500,
52
+ * },
53
+ * // callback can optionally be provided on construction
54
+ * () => {
55
+ * console.log('called');
56
+ * },
57
+ * );
58
+ *
59
+ * debounce.execute();
60
+ * // providing a callback in `.execute()` permanently overrides the callback provided in construction.
61
+ * debounce.execute(() => {});
62
+ * ```
63
+ *
64
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
65
+ */
15
66
  export class Debounce {
16
67
  debounceStyle;
17
68
  debounceDuration;
18
- nextCallTimestamp = 0;
19
69
  callback;
20
- constructor(debounceStyle, debounceDuration, callback) {
70
+ nextCallTimestamp = 0;
71
+ constructor(
72
+ /** Debounce style. See {@link DebounceStyle} for more details. */
73
+ debounceStyle,
74
+ /** Duration between debounces. */
75
+ debounceDuration,
76
+ /**
77
+ * Set the callback to be triggered on `.execute()`. If this is not set, the callback to be
78
+ * called can be passed in `.execute()` instead.
79
+ */
80
+ callback) {
21
81
  this.debounceStyle = debounceStyle;
22
82
  this.debounceDuration = debounceDuration;
83
+ this.callback = callback;
23
84
  if (callback) {
24
85
  this.callback = callback;
25
86
  }
26
87
  }
88
+ /** Call the callback, if one has been set yet, if the current debounce timer is up. */
27
89
  execute(callback) {
28
90
  if (callback) {
29
91
  this.callback = callback;
@@ -1,8 +1,20 @@
1
1
  import { Duration, DurationUnit } from '@date-vir/duration';
2
- export type ExecutionDuration = Duration<DurationUnit.Milliseconds>;
3
2
  /**
4
- * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
5
- * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
6
- * purely synchronous).
3
+ * Measures how long (in milliseconds) the given callback takes to run to completion. By default
4
+ * this is synchronous, but it will automatically switch to async and await the callback if it
5
+ * returns a promise.
6
+ *
7
+ * @category Function
8
+ * @category Package : @augment-vir/common
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import {measureExecutionDuration} from '@augment-vir/common';
13
+ *
14
+ * const duration1 = measureExecutionDuration(() => {});
15
+ * const duration2 = await measureExecutionDuration(async () => {});
16
+ * ```
17
+ *
18
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
19
  */
8
- export declare function measureExecutionDuration<T>(callback: () => T): T extends Promise<any> ? Promise<ExecutionDuration> : ExecutionDuration;
20
+ export declare function measureExecutionDuration<T>(callback: () => T): T extends Promise<any> ? Promise<Duration<DurationUnit.Milliseconds>> : Duration<DurationUnit.Milliseconds>;
@@ -1,8 +1,21 @@
1
1
  import { ensureError } from '@augment-vir/core';
2
2
  /**
3
- * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
4
- * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
5
- * purely synchronous).
3
+ * Measures how long (in milliseconds) the given callback takes to run to completion. By default
4
+ * this is synchronous, but it will automatically switch to async and await the callback if it
5
+ * returns a promise.
6
+ *
7
+ * @category Function
8
+ * @category Package : @augment-vir/common
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import {measureExecutionDuration} from '@augment-vir/common';
13
+ *
14
+ * const duration1 = measureExecutionDuration(() => {});
15
+ * const duration2 = await measureExecutionDuration(async () => {});
16
+ * ```
17
+ *
18
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
6
19
  */
7
20
  export function measureExecutionDuration(callback) {
8
21
  const startTime = Date.now();