@augment-vir/common 29.3.0 → 30.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.
Files changed (296) hide show
  1. package/LICENSE-CC0 +121 -0
  2. package/LICENSE-MIT +21 -0
  3. package/README.md +7 -3
  4. package/dist/augments/array/array-map.d.ts +34 -0
  5. package/dist/augments/array/array-map.js +32 -0
  6. package/dist/augments/array/array-to-object.d.ts +53 -0
  7. package/dist/augments/array/array-to-object.js +67 -0
  8. package/dist/augments/array/awaited/awaited-filter.d.ts +36 -0
  9. package/dist/augments/array/awaited/awaited-filter.js +36 -0
  10. package/dist/augments/array/awaited/awaited-for-each.d.ts +31 -0
  11. package/dist/augments/array/awaited/awaited-for-each.js +34 -0
  12. package/dist/augments/array/awaited/awaited-map.d.ts +33 -0
  13. package/dist/augments/array/awaited/awaited-map.js +40 -0
  14. package/dist/augments/array/filter.d.ts +32 -0
  15. package/dist/augments/array/filter.js +74 -0
  16. package/dist/augments/array/remove-duplicates.d.ts +60 -0
  17. package/dist/augments/array/remove-duplicates.js +72 -0
  18. package/dist/augments/array/repeat-array.d.ts +23 -0
  19. package/dist/augments/array/repeat-array.js +25 -0
  20. package/dist/augments/array/shuffle-array.d.ts +9 -0
  21. package/dist/augments/array/shuffle-array.js +17 -0
  22. package/dist/augments/array/string-array.d.ts +9 -0
  23. package/dist/augments/array/string-array.js +11 -0
  24. package/dist/augments/core-exports.d.ts +1 -0
  25. package/dist/augments/core-exports.js +1 -0
  26. package/dist/augments/enum/enum-value-check.d.ts +32 -0
  27. package/dist/augments/enum/enum-value-check.js +34 -0
  28. package/dist/augments/error/combine-errors.d.ts +24 -0
  29. package/dist/augments/error/combine-errors.js +34 -0
  30. package/dist/augments/function/call-asynchronously.d.ts +25 -0
  31. package/dist/augments/function/call-asynchronously.js +26 -0
  32. package/dist/augments/function/call-with-retries.d.ts +24 -0
  33. package/dist/augments/function/call-with-retries.js +50 -0
  34. package/dist/augments/function/debounce.d.ts +89 -0
  35. package/dist/augments/function/debounce.js +112 -0
  36. package/dist/augments/function/execution-duration.d.ts +20 -0
  37. package/dist/augments/function/execution-duration.js +39 -0
  38. package/dist/augments/function/if-truthy.d.ts +28 -0
  39. package/dist/augments/function/if-truthy.js +35 -0
  40. package/dist/{types/augments → augments/function}/wrap-in-try.d.ts +14 -7
  41. package/dist/augments/function/wrap-in-try.js +98 -0
  42. package/dist/augments/json/append-json.d.ts +5 -0
  43. package/dist/augments/json/append-json.js +59 -0
  44. package/dist/augments/json/copy-through-json.d.ts +34 -0
  45. package/dist/augments/json/copy-through-json.js +42 -0
  46. package/dist/augments/json/json-compatible.d.ts +50 -0
  47. package/dist/augments/json/jsonify.d.ts +31 -0
  48. package/dist/augments/json/jsonify.js +33 -0
  49. package/dist/augments/log/log-colors.d.ts +64 -0
  50. package/dist/augments/log/log-colors.js +157 -0
  51. package/dist/augments/log/log-string.d.ts +35 -0
  52. package/dist/augments/log/log-string.js +73 -0
  53. package/dist/augments/log/log-writer.d.ts +30 -0
  54. package/dist/augments/log/log.d.ts +56 -0
  55. package/dist/augments/log/log.js +85 -0
  56. package/dist/augments/log/logger.d.ts +60 -0
  57. package/dist/augments/log/logger.js +59 -0
  58. package/dist/augments/number/clamp.d.ts +18 -0
  59. package/dist/augments/number/clamp.js +19 -0
  60. package/dist/augments/number/coords.d.ts +23 -0
  61. package/dist/augments/number/digit.d.ts +8 -0
  62. package/dist/augments/number/dimensions.d.ts +23 -0
  63. package/dist/augments/number/min-max.d.ts +18 -0
  64. package/dist/augments/number/min-max.js +16 -0
  65. package/dist/augments/number/number-conversion.d.ts +29 -0
  66. package/dist/augments/number/number-conversion.js +56 -0
  67. package/dist/augments/number/round.d.ts +22 -0
  68. package/dist/augments/number/round.js +24 -0
  69. package/dist/augments/number/scientific.d.ts +18 -0
  70. package/dist/augments/number/scientific.js +20 -0
  71. package/dist/augments/number/truncate-number.d.ts +40 -0
  72. package/dist/{esm/augments → augments/number}/truncate-number.js +39 -17
  73. package/dist/augments/number/wrap-number.d.ts +19 -0
  74. package/dist/augments/number/wrap-number.js +28 -0
  75. package/dist/augments/object/diff.d.ts +51 -0
  76. package/dist/augments/object/diff.js +149 -0
  77. package/dist/augments/object/empty.d.ts +9 -0
  78. package/dist/augments/object/get-or-set.d.ts +10 -0
  79. package/dist/augments/object/get-or-set.js +100 -0
  80. package/dist/augments/object/key-count.d.ts +21 -0
  81. package/dist/augments/object/map-entries.d.ts +13 -0
  82. package/dist/augments/object/map-entries.js +80 -0
  83. package/dist/augments/object/map-enum.d.ts +11 -0
  84. package/dist/augments/object/map-enum.js +45 -0
  85. package/dist/augments/object/map-values.d.ts +35 -0
  86. package/dist/augments/object/map-values.js +65 -0
  87. package/dist/{types/augments → augments}/object/merge-deep.d.ts +4 -0
  88. package/dist/{esm/augments → augments}/object/merge-deep.js +9 -6
  89. package/dist/augments/object/merge-defined-properties.d.ts +19 -0
  90. package/dist/augments/object/merge-defined-properties.js +32 -0
  91. package/dist/augments/object/merge-property-arrays.d.ts +29 -0
  92. package/dist/augments/object/merge-property-arrays.js +39 -0
  93. package/dist/augments/object/object-entries.d.ts +31 -0
  94. package/dist/augments/object/object-entries.js +40 -0
  95. package/dist/augments/object/object-filter.d.ts +22 -0
  96. package/dist/augments/object/object-filter.js +27 -0
  97. package/dist/augments/object/object-keys.d.ts +34 -0
  98. package/dist/augments/object/object-keys.js +43 -0
  99. package/dist/augments/object/object-values.d.ts +11 -0
  100. package/dist/augments/object/object-values.js +13 -0
  101. package/dist/augments/path/esm-path.d.ts +26 -0
  102. package/dist/augments/path/esm-path.js +31 -0
  103. package/dist/augments/prisma/prisma-models.d.ts +143 -0
  104. package/dist/augments/prisma/prisma-models.js +1 -0
  105. package/dist/augments/promise/timed-promise.d.ts +22 -0
  106. package/dist/augments/promise/timed-promise.js +51 -0
  107. package/dist/augments/random/random-boolean.d.ts +24 -0
  108. package/dist/augments/random/random-boolean.js +32 -0
  109. package/dist/augments/random/random-integer.d.ts +14 -0
  110. package/dist/augments/random/random-integer.js +34 -0
  111. package/dist/augments/random/random-string.d.ts +18 -0
  112. package/dist/augments/random/random-string.js +67 -0
  113. package/dist/augments/regexp/match.d.ts +10 -0
  114. package/dist/augments/regexp/match.js +13 -0
  115. package/dist/augments/regexp/regexp-flags.d.ts +56 -0
  116. package/dist/augments/regexp/regexp-flags.js +75 -0
  117. package/dist/augments/regexp/regexp-string.d.ts +9 -0
  118. package/dist/augments/regexp/regexp-string.js +11 -0
  119. package/dist/augments/selection-set/select-collapsed.d.ts +54 -0
  120. package/dist/augments/selection-set/select-collapsed.js +63 -0
  121. package/dist/augments/selection-set/select-from.d.ts +48 -0
  122. package/dist/augments/selection-set/select-from.js +72 -0
  123. package/dist/augments/selection-set/selection-set.d.ts +37 -0
  124. package/dist/augments/selection-set/selection-set.js +1 -0
  125. package/dist/augments/string/casing/capitalization.d.ts +17 -0
  126. package/dist/augments/string/casing/capitalization.js +25 -0
  127. package/dist/augments/string/casing/casing.d.ts +73 -0
  128. package/dist/augments/string/casing/casing.js +65 -0
  129. package/dist/augments/string/casing/kebab-and-camel.d.ts +17 -0
  130. package/dist/augments/string/casing/kebab-and-camel.js +57 -0
  131. package/dist/augments/string/comma.d.ts +26 -0
  132. package/dist/augments/string/comma.js +48 -0
  133. package/dist/augments/string/join.d.ts +21 -0
  134. package/dist/augments/string/join.js +34 -0
  135. package/dist/augments/string/prefix.d.ts +30 -0
  136. package/dist/augments/string/prefix.js +30 -0
  137. package/dist/augments/string/remove-duplicate-characters.d.ts +17 -0
  138. package/dist/augments/string/remove-duplicate-characters.js +22 -0
  139. package/dist/augments/string/replace.d.ts +18 -0
  140. package/dist/augments/string/replace.js +22 -0
  141. package/dist/augments/string/split.d.ts +31 -0
  142. package/dist/augments/string/split.js +53 -0
  143. package/dist/augments/string/substring-index.d.ts +19 -0
  144. package/dist/augments/string/substring-index.js +47 -0
  145. package/dist/augments/string/suffix.d.ts +96 -0
  146. package/dist/augments/string/suffix.js +89 -0
  147. package/dist/augments/string/white-space.d.ts +12 -0
  148. package/dist/augments/string/white-space.js +19 -0
  149. package/dist/augments/string/wrap-string.d.ts +19 -0
  150. package/dist/augments/string/wrap-string.js +20 -0
  151. package/dist/augments/type/ensure-type.d.ts +12 -0
  152. package/dist/augments/type/ensure-type.js +14 -0
  153. package/dist/augments/type/readonly.d.ts +9 -0
  154. package/dist/augments/type/readonly.js +10 -0
  155. package/dist/augments/type/type-recursion.d.ts +253 -0
  156. package/dist/augments/type/type-recursion.js +1 -0
  157. package/dist/augments/type/union.d.ts +1 -0
  158. package/dist/augments/type/union.js +1 -0
  159. package/dist/augments/type/void-type.d.ts +9 -0
  160. package/dist/augments/type/void-type.js +1 -0
  161. package/dist/augments/type/writable.d.ts +10 -0
  162. package/dist/augments/type/writable.js +10 -0
  163. package/dist/index.d.ts +83 -0
  164. package/dist/index.js +83 -0
  165. package/package.json +43 -15
  166. package/dist/cjs/augments/ansi.js +0 -27
  167. package/dist/cjs/augments/array/array.js +0 -68
  168. package/dist/cjs/augments/array/remove-duplicates.js +0 -16
  169. package/dist/cjs/augments/async.js +0 -28
  170. package/dist/cjs/augments/boolean.js +0 -19
  171. package/dist/cjs/augments/common-number.js +0 -112
  172. package/dist/cjs/augments/common-string.js +0 -245
  173. package/dist/cjs/augments/debounce.js +0 -38
  174. package/dist/cjs/augments/environment.js +0 -6
  175. package/dist/cjs/augments/error.js +0 -52
  176. package/dist/cjs/augments/esm-path.js +0 -11
  177. package/dist/cjs/augments/function.js +0 -2
  178. package/dist/cjs/augments/json-compatible.js +0 -2
  179. package/dist/cjs/augments/json.js +0 -28
  180. package/dist/cjs/augments/object/any-object.js +0 -2
  181. package/dist/cjs/augments/object/enum.js +0 -48
  182. package/dist/cjs/augments/object/filter-object.js +0 -26
  183. package/dist/cjs/augments/object/get-or-set.js +0 -40
  184. package/dist/cjs/augments/object/has-key.js +0 -6
  185. package/dist/cjs/augments/object/jsonify.js +0 -6
  186. package/dist/cjs/augments/object/key-count.js +0 -2
  187. package/dist/cjs/augments/object/map-object.js +0 -61
  188. package/dist/cjs/augments/object/merge-deep.js +0 -52
  189. package/dist/cjs/augments/object/merge-property-arrays.js +0 -14
  190. package/dist/cjs/augments/object/object-entries.js +0 -40
  191. package/dist/cjs/augments/object/object.js +0 -18
  192. package/dist/cjs/augments/object/pick-deep.js +0 -2
  193. package/dist/cjs/augments/object/selection-set.js +0 -56
  194. package/dist/cjs/augments/object/typed-has-property.js +0 -29
  195. package/dist/cjs/augments/promise/deferred-promise.js +0 -31
  196. package/dist/cjs/augments/promise/promise.js +0 -79
  197. package/dist/cjs/augments/promise/wait.js +0 -66
  198. package/dist/cjs/augments/random.js +0 -118
  199. package/dist/cjs/augments/regexp.js +0 -19
  200. package/dist/cjs/augments/string/prefixes.js +0 -20
  201. package/dist/cjs/augments/string/suffixes.js +0 -40
  202. package/dist/cjs/augments/string/uuid.js +0 -13
  203. package/dist/cjs/augments/time.js +0 -35
  204. package/dist/cjs/augments/truncate-number.js +0 -153
  205. package/dist/cjs/augments/tuple.js +0 -20
  206. package/dist/cjs/augments/type-recursion.js +0 -2
  207. package/dist/cjs/augments/type.js +0 -36
  208. package/dist/cjs/augments/union.js +0 -2
  209. package/dist/cjs/augments/wrap-in-try.js +0 -37
  210. package/dist/cjs/index.js +0 -60
  211. package/dist/esm/augments/ansi.js +0 -24
  212. package/dist/esm/augments/array/array.js +0 -57
  213. package/dist/esm/augments/array/remove-duplicates.js +0 -13
  214. package/dist/esm/augments/async.js +0 -23
  215. package/dist/esm/augments/boolean.js +0 -14
  216. package/dist/esm/augments/common-number.js +0 -100
  217. package/dist/esm/augments/common-string.js +0 -227
  218. package/dist/esm/augments/debounce.js +0 -34
  219. package/dist/esm/augments/environment.js +0 -3
  220. package/dist/esm/augments/error.js +0 -45
  221. package/dist/esm/augments/esm-path.js +0 -8
  222. package/dist/esm/augments/json.js +0 -24
  223. package/dist/esm/augments/object/enum.js +0 -41
  224. package/dist/esm/augments/object/filter-object.js +0 -21
  225. package/dist/esm/augments/object/get-or-set.js +0 -36
  226. package/dist/esm/augments/object/has-key.js +0 -3
  227. package/dist/esm/augments/object/jsonify.js +0 -3
  228. package/dist/esm/augments/object/map-object.js +0 -57
  229. package/dist/esm/augments/object/merge-property-arrays.js +0 -11
  230. package/dist/esm/augments/object/object-entries.js +0 -32
  231. package/dist/esm/augments/object/object.js +0 -14
  232. package/dist/esm/augments/object/selection-set.js +0 -52
  233. package/dist/esm/augments/object/typed-has-property.js +0 -25
  234. package/dist/esm/augments/promise/deferred-promise.js +0 -28
  235. package/dist/esm/augments/promise/promise.js +0 -71
  236. package/dist/esm/augments/promise/wait.js +0 -60
  237. package/dist/esm/augments/random.js +0 -113
  238. package/dist/esm/augments/regexp.js +0 -14
  239. package/dist/esm/augments/string/prefixes.js +0 -16
  240. package/dist/esm/augments/string/suffixes.js +0 -31
  241. package/dist/esm/augments/string/uuid.js +0 -9
  242. package/dist/esm/augments/time.js +0 -31
  243. package/dist/esm/augments/tuple.js +0 -16
  244. package/dist/esm/augments/type.js +0 -30
  245. package/dist/esm/augments/wrap-in-try.js +0 -34
  246. package/dist/esm/index.js +0 -44
  247. package/dist/types/augments/ansi.d.ts +0 -1
  248. package/dist/types/augments/array/array.d.ts +0 -26
  249. package/dist/types/augments/array/remove-duplicates.d.ts +0 -1
  250. package/dist/types/augments/async.d.ts +0 -14
  251. package/dist/types/augments/boolean.d.ts +0 -6
  252. package/dist/types/augments/common-number.d.ts +0 -52
  253. package/dist/types/augments/common-string.d.ts +0 -74
  254. package/dist/types/augments/debounce.d.ts +0 -15
  255. package/dist/types/augments/environment.d.ts +0 -1
  256. package/dist/types/augments/error.d.ts +0 -9
  257. package/dist/types/augments/esm-path.d.ts +0 -4
  258. package/dist/types/augments/function.d.ts +0 -25
  259. package/dist/types/augments/json-compatible.d.ts +0 -22
  260. package/dist/types/augments/json.d.ts +0 -9
  261. package/dist/types/augments/object/any-object.d.ts +0 -2
  262. package/dist/types/augments/object/enum.d.ts +0 -8
  263. package/dist/types/augments/object/filter-object.d.ts +0 -4
  264. package/dist/types/augments/object/get-or-set.d.ts +0 -26
  265. package/dist/types/augments/object/has-key.d.ts +0 -1
  266. package/dist/types/augments/object/jsonify.d.ts +0 -2
  267. package/dist/types/augments/object/key-count.d.ts +0 -13
  268. package/dist/types/augments/object/map-object.d.ts +0 -20
  269. package/dist/types/augments/object/merge-property-arrays.d.ts +0 -1
  270. package/dist/types/augments/object/object-entries.d.ts +0 -8
  271. package/dist/types/augments/object/object.d.ts +0 -21
  272. package/dist/types/augments/object/pick-deep.d.ts +0 -14
  273. package/dist/types/augments/object/selection-set.d.ts +0 -27
  274. package/dist/types/augments/object/typed-has-property.d.ts +0 -7
  275. package/dist/types/augments/promise/deferred-promise.d.ts +0 -8
  276. package/dist/types/augments/promise/promise.d.ts +0 -15
  277. package/dist/types/augments/promise/wait.d.ts +0 -18
  278. package/dist/types/augments/random.d.ts +0 -37
  279. package/dist/types/augments/regexp.d.ts +0 -3
  280. package/dist/types/augments/string/prefixes.d.ts +0 -9
  281. package/dist/types/augments/string/suffixes.d.ts +0 -17
  282. package/dist/types/augments/string/uuid.d.ts +0 -5
  283. package/dist/types/augments/time.d.ts +0 -14
  284. package/dist/types/augments/truncate-number.d.ts +0 -19
  285. package/dist/types/augments/tuple.d.ts +0 -17
  286. package/dist/types/augments/type-recursion.d.ts +0 -161
  287. package/dist/types/augments/type.d.ts +0 -69
  288. package/dist/types/augments/union.d.ts +0 -5
  289. package/dist/types/index.d.ts +0 -44
  290. /package/dist/{esm/augments → augments/json}/json-compatible.js +0 -0
  291. /package/dist/{esm/augments/function.js → augments/log/log-writer.js} +0 -0
  292. /package/dist/{esm/augments/object/any-object.js → augments/number/coords.js} +0 -0
  293. /package/dist/{esm/augments/object/key-count.js → augments/number/digit.js} +0 -0
  294. /package/dist/{esm/augments/object/pick-deep.js → augments/number/dimensions.js} +0 -0
  295. /package/dist/{esm/augments/type-recursion.js → augments/object/empty.js} +0 -0
  296. /package/dist/{esm/augments/union.js → augments/object/key-count.js} +0 -0
@@ -0,0 +1,112 @@
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
+ */
9
+ export var DebounceStyle;
10
+ (function (DebounceStyle) {
11
+ /**
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! |
21
+ */
22
+ DebounceStyle["FirstThenWait"] = "first-then-wait";
23
+ /**
24
+ * Waits the given amount of milliseconds after the first call and then fires the latest
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! |
33
+ */
34
+ DebounceStyle["AfterWait"] = "after-wait";
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
+ */
66
+ export class Debounce {
67
+ debounceStyle;
68
+ debounceDuration;
69
+ 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) {
81
+ this.debounceStyle = debounceStyle;
82
+ this.debounceDuration = debounceDuration;
83
+ this.callback = callback;
84
+ if (callback) {
85
+ this.callback = callback;
86
+ }
87
+ }
88
+ /** Call the callback, if one has been set yet, if the current debounce timer is up. */
89
+ execute(callback) {
90
+ if (callback) {
91
+ this.callback = callback;
92
+ }
93
+ else if (!this.callback) {
94
+ return;
95
+ }
96
+ const now = Date.now();
97
+ if (this.nextCallTimestamp > now) {
98
+ return;
99
+ }
100
+ if (this.debounceStyle === DebounceStyle.FirstThenWait) {
101
+ void this.callback();
102
+ }
103
+ else {
104
+ setTimeout(() => {
105
+ /** Use whatever the latest latestCallback is. */
106
+ void this.callback?.();
107
+ }, this.debounceDuration.milliseconds);
108
+ }
109
+ this.nextCallTimestamp =
110
+ now + convertDuration(this.debounceDuration, DurationUnit.Milliseconds).milliseconds;
111
+ }
112
+ }
@@ -0,0 +1,20 @@
1
+ import { Duration, DurationUnit } from '@date-vir/duration';
2
+ /**
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)
19
+ */
20
+ export declare function measureExecutionDuration<T>(callback: () => T): T extends Promise<any> ? Promise<Duration<DurationUnit.Milliseconds>> : Duration<DurationUnit.Milliseconds>;
@@ -0,0 +1,39 @@
1
+ import { ensureError } from '@augment-vir/core';
2
+ /**
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)
19
+ */
20
+ export function measureExecutionDuration(callback) {
21
+ const startTime = Date.now();
22
+ const result = callback();
23
+ if (result instanceof Promise) {
24
+ return new Promise(async (resolve, reject) => {
25
+ try {
26
+ await result;
27
+ const endTime = Date.now();
28
+ resolve({ milliseconds: endTime - startTime });
29
+ }
30
+ catch (caught) {
31
+ reject(ensureError(caught));
32
+ }
33
+ });
34
+ }
35
+ const endTime = Date.now();
36
+ return {
37
+ milliseconds: endTime - startTime,
38
+ };
39
+ }
@@ -0,0 +1,28 @@
1
+ import { type Falsy, type Truthy } from '@augment-vir/assert';
2
+ /**
3
+ * Checks an input for truthiness then calls the respective callback, returning the callback's
4
+ * output.
5
+ *
6
+ * @category Function
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {ifTruthy} from '@augment-vir/common';
12
+ *
13
+ * const result1 = ifTruthy(
14
+ * true,
15
+ * () => 1,
16
+ * () => 2,
17
+ * ); // result1 is `1`
18
+ * const result2 = ifTruthy(
19
+ * false,
20
+ * () => 1,
21
+ * () => 2,
22
+ * ); // result2 is `2`
23
+ * ```
24
+ *
25
+ * @returns The called callback's output.
26
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
27
+ */
28
+ export declare function ifTruthy<const InputType, IfTruthyType, IfFalsyType>(checkThis: InputType, ifTruthyCallback: (truthyInput: Truthy<InputType>) => IfTruthyType, ifFalsyCallback: (truthyInput: Falsy<InputType>) => IfFalsyType): IfTruthyType | IfFalsyType;
@@ -0,0 +1,35 @@
1
+ import { check } from '@augment-vir/assert';
2
+ /**
3
+ * Checks an input for truthiness then calls the respective callback, returning the callback's
4
+ * output.
5
+ *
6
+ * @category Function
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {ifTruthy} from '@augment-vir/common';
12
+ *
13
+ * const result1 = ifTruthy(
14
+ * true,
15
+ * () => 1,
16
+ * () => 2,
17
+ * ); // result1 is `1`
18
+ * const result2 = ifTruthy(
19
+ * false,
20
+ * () => 1,
21
+ * () => 2,
22
+ * ); // result2 is `2`
23
+ * ```
24
+ *
25
+ * @returns The called callback's output.
26
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
27
+ */
28
+ export function ifTruthy(checkThis, ifTruthyCallback, ifFalsyCallback) {
29
+ if (check.isTruthy(checkThis)) {
30
+ return ifTruthyCallback(checkThis);
31
+ }
32
+ else {
33
+ return ifFalsyCallback(checkThis);
34
+ }
35
+ }
@@ -1,14 +1,21 @@
1
- import { NoInputsFunction } from './function';
2
- import { PartialAndUndefined } from './object/object';
3
- export type WrapInTryOptions<FallbackValue> = PartialAndUndefined<{
1
+ import { type NoInputsFunction, type PartialWithUndefined } from '@augment-vir/core';
2
+ /**
3
+ * Options for {@link wrapInTry}.
4
+ *
5
+ * @category Function
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
9
+ export type WrapInTryOptions<FallbackValue> = PartialWithUndefined<{
4
10
  /**
5
- * A callback that is passed the error. The output of this callback is returned by `wrapInTry`.
6
- * This takes precedence over the other two options.
11
+ * Call this function if the callback passed to {@link wrapInTry} throws an error. The thrown
12
+ * error is passed to this function. If a `fallbackValue` option is also provided, it will be
13
+ * ignored.
7
14
  */
8
15
  handleError: (error: unknown) => FallbackValue;
9
16
  /**
10
- * Fallback to this value if the callback passed to `wrapInTry` throws an error. Takes
11
- * precedence over `returnError`.
17
+ * Fallback to this value if the callback passed to {@link wrapInTry} throws an error. This will
18
+ * be ignored if a `handleError` option is also set.
12
19
  */
13
20
  fallbackValue: FallbackValue;
14
21
  }>;
@@ -0,0 +1,98 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { ensureError, } from '@augment-vir/core';
3
+ /**
4
+ * Calls the callback and returns its output. If the callback throws an error, it is handled in the
5
+ * following ways:
6
+ *
7
+ * - If a `handleError` function is provided in `options`, it is passed the thrown error. The output
8
+ * of `handleError` is returned by `wrapInTry`.
9
+ * - If a `fallbackValue` is provided, it is returned by `wrapInTry`. The thrown error is ignored.
10
+ * - If no options are provided, the thrown error is returned by `wrapInTry`.
11
+ *
12
+ * @category Function
13
+ * @category Package : @augment-vir/common
14
+ * @example
15
+ *
16
+ * ```ts
17
+ * import {wrapInTry} from '@augment-vir/common';
18
+ *
19
+ * // `result1` will be `'success'`.
20
+ * const result1 = wrapInTry(
21
+ * () => {
22
+ * return 'success';
23
+ * },
24
+ * {
25
+ * fallbackValue: 'failure',
26
+ * },
27
+ * );
28
+ * // `result2` will be `'failure'`.
29
+ * const result2 = wrapInTry(
30
+ * () => {
31
+ * throw new Error();
32
+ * return 'success';
33
+ * },
34
+ * {
35
+ * fallbackValue: 'failure',
36
+ * },
37
+ * );
38
+ * // `result3` will be `'failure also'`.
39
+ * const result3 = wrapInTry(
40
+ * () => {
41
+ * throw new Error();
42
+ * return 'success';
43
+ * },
44
+ * {
45
+ * handleError() {
46
+ * return 'failure also';
47
+ * },
48
+ * },
49
+ * );
50
+ * // `result4` will be `'failure also'`.
51
+ * const result4 = wrapInTry(
52
+ * () => {
53
+ * throw new Error();
54
+ * return 'success';
55
+ * },
56
+ * {
57
+ * handleError() {
58
+ * return 'failure also';
59
+ * },
60
+ * fallbackValue: 'ignored',
61
+ * },
62
+ * );
63
+ * ```
64
+ *
65
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
66
+ */
67
+ export function wrapInTry(callback, options = {}) {
68
+ try {
69
+ const value = callback();
70
+ if (value instanceof Promise) {
71
+ return value.catch((error) => {
72
+ if (options.handleError) {
73
+ return options.handleError(error);
74
+ }
75
+ else if (check.hasKey(options, 'fallbackValue')) {
76
+ return options.fallbackValue;
77
+ }
78
+ else {
79
+ return ensureError(error);
80
+ }
81
+ });
82
+ }
83
+ else {
84
+ return value;
85
+ }
86
+ }
87
+ catch (error) {
88
+ if (options.handleError) {
89
+ return options.handleError(error);
90
+ }
91
+ else if (check.hasKey(options, 'fallbackValue')) {
92
+ return options.fallbackValue;
93
+ }
94
+ else {
95
+ return ensureError(error);
96
+ }
97
+ }
98
+ }
@@ -0,0 +1,5 @@
1
+ import type { JsonCompatibleArray, JsonCompatibleObject, JsonCompatibleValue } from './json-compatible.js';
2
+ import { JsonCompatiblePrimitive } from './json-compatible.js';
3
+ export declare function appendJson(entry: JsonCompatibleArray | JsonCompatiblePrimitive, ...entries: ReadonlyArray<JsonCompatibleValue | undefined>): JsonCompatibleArray;
4
+ export declare function appendJson(entry: JsonCompatibleObject, ...entries: ReadonlyArray<JsonCompatibleObject | JsonCompatibleArray | undefined>): JsonCompatibleObject;
5
+ export declare function appendJson(...entries: ReadonlyArray<JsonCompatibleValue | undefined>): JsonCompatibleObject | JsonCompatibleArray;
@@ -0,0 +1,59 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { copyThroughJson } from './copy-through-json.js';
3
+ /**
4
+ * Appends all provided JSON values together. `undefined` values will be ignored. The first value
5
+ * determines whether the output will be an object or an array. Any value appended to an array will
6
+ * work just fine, but primitives append to an object will likely behave unexpectedly. Arrays
7
+ * appended to arrays will be flattened (but only by one level).
8
+ *
9
+ * @category JSON : Common
10
+ * @category Package : @augment-vir/common
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * import {appendJson} from '@augment-vir/common';
15
+ *
16
+ * // `result1` will be `{a: 'q', b: 'b'}`
17
+ * const result1 = appendJson({a: 'a'}, {b: 'b'}, {a: 'q'});
18
+ * // `result2` will be `[{a: 'a'}, {b: 'b'}, {a: 'q'}, 'r']`
19
+ * const result2 = appendJson([{a: 'a'}], {b: 'b'}, {a: 'q'}, 'r');
20
+ * // `result3` will be `['a', ['b', 'c'], 'd', 'e']`
21
+ * const result3 = appendJson(
22
+ * ['a'],
23
+ * [
24
+ * [
25
+ * 'b',
26
+ * 'c',
27
+ * ],
28
+ * ],
29
+ * ['d'],
30
+ * 'e',
31
+ * );
32
+ * ```
33
+ *
34
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
35
+ */
36
+ export function appendJson(...rawEntries) {
37
+ const entries = rawEntries.filter(check.isTruthy);
38
+ if (!check.isLengthAtLeast(entries, 1)) {
39
+ return {};
40
+ }
41
+ const firstEntry = copyThroughJson(entries[0]);
42
+ const combinedData = typeof firstEntry === 'object'
43
+ ? firstEntry
44
+ : [firstEntry];
45
+ entries.slice(1).forEach((entry) => {
46
+ if (check.isArray(combinedData)) {
47
+ if (check.isArray(entry)) {
48
+ combinedData.push(...entry);
49
+ }
50
+ else {
51
+ combinedData.push(entry);
52
+ }
53
+ }
54
+ else {
55
+ Object.assign(combinedData, entry);
56
+ }
57
+ });
58
+ return combinedData;
59
+ }
@@ -0,0 +1,34 @@
1
+ import { Jsonify, Writable } from 'type-fest';
2
+ /**
3
+ * Deeply copy an object through JSON. This is the fastest deep copy, but the input must already be
4
+ * JSON serializable otherwise the copy will not match the original.
5
+ *
6
+ * @category JSON : Common
7
+ * @category Copy
8
+ * @category Package : @augment-vir/common
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import {copyThroughJson} from '@augment-vir/common';
13
+ *
14
+ * // `copy1` will be `{a: 'a', b: 'b'}`
15
+ * const copy1 = copyThroughJson({a: 'a', b: 'b'});
16
+ * // `copy2` will be `{map: {}, b: 'b'}`
17
+ * const copy2 = copyThroughJson({
18
+ * map: new Map([
19
+ * [
20
+ * 'q',
21
+ * 'r',
22
+ * ],
23
+ * [
24
+ * 's',
25
+ * 't',
26
+ * ],
27
+ * ]),
28
+ * b: 'b',
29
+ * });
30
+ * ```
31
+ *
32
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
33
+ */
34
+ export declare function copyThroughJson<const T>(input: T): Writable<Jsonify<T>>;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Deeply copy an object through JSON. This is the fastest deep copy, but the input must already be
3
+ * JSON serializable otherwise the copy will not match the original.
4
+ *
5
+ * @category JSON : Common
6
+ * @category Copy
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {copyThroughJson} from '@augment-vir/common';
12
+ *
13
+ * // `copy1` will be `{a: 'a', b: 'b'}`
14
+ * const copy1 = copyThroughJson({a: 'a', b: 'b'});
15
+ * // `copy2` will be `{map: {}, b: 'b'}`
16
+ * const copy2 = copyThroughJson({
17
+ * map: new Map([
18
+ * [
19
+ * 'q',
20
+ * 'r',
21
+ * ],
22
+ * [
23
+ * 's',
24
+ * 't',
25
+ * ],
26
+ * ]),
27
+ * b: 'b',
28
+ * });
29
+ * ```
30
+ *
31
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
32
+ */
33
+ export function copyThroughJson(input) {
34
+ try {
35
+ return JSON.parse(JSON.stringify(input));
36
+ /* node:coverage ignore next 4 */
37
+ }
38
+ catch (error) {
39
+ console.error(`Failed to JSON copy for`, input);
40
+ throw error;
41
+ }
42
+ }
@@ -0,0 +1,50 @@
1
+ import { Jsonify, Primitive } from 'type-fest';
2
+ /**
3
+ * These are similar in purpose, name, and structure to type-fest's JsonValue types but these are
4
+ * more permissive. The goal here is to allow any types that do not get serialized into just empty
5
+ * objects. (For example, JSON.stringify(new Map()) returns "{}", so we don't want to allow that
6
+ * type.)
7
+ */
8
+ /**
9
+ * All primitives that are allowed in JSON.
10
+ *
11
+ * Note that while `undefined` is allowed here, it will be behave slightly differently than the
12
+ * others.
13
+ *
14
+ * - `JSON.stringify(undefined)` will output `undefined`, not a string.
15
+ * - `JSON.stringify({a: null, b: undefined})` will output `'{"a": null}'`, omitting the `b` key
16
+ * entirely.
17
+ *
18
+ * @category JSON : Common
19
+ * @category Package : @augment-vir/common
20
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
21
+ */
22
+ export type JsonCompatiblePrimitive = Jsonify<Primitive> | undefined;
23
+ /**
24
+ * An object that only contains JSON compatible values.
25
+ *
26
+ * @category JSON : Common
27
+ * @category Package : @augment-vir/common
28
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
29
+ */
30
+ export type JsonCompatibleObject = Partial<{
31
+ readonly [key: string | number]: JsonCompatibleValue | Readonly<JsonCompatibleValue>;
32
+ }> | Partial<{
33
+ [key: string | number]: JsonCompatibleValue | Readonly<JsonCompatibleValue>;
34
+ }>;
35
+ /**
36
+ * An array that only contains JSON compatible values.
37
+ *
38
+ * @category JSON : Common
39
+ * @category Package : @augment-vir/common
40
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
41
+ */
42
+ export type JsonCompatibleArray = JsonCompatibleValue[] | ReadonlyArray<JsonCompatibleValue>;
43
+ /**
44
+ * Any JSON compatible value.
45
+ *
46
+ * @category JSON : Common
47
+ * @category Package : @augment-vir/common
48
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
49
+ */
50
+ export type JsonCompatibleValue = JsonCompatiblePrimitive | JsonCompatibleObject | JsonCompatibleArray;
@@ -0,0 +1,31 @@
1
+ import { Jsonify } from 'type-fest';
2
+ /**
3
+ * Creates a JSON compatible version of the value given. Under the hood this is actually the same as
4
+ * {@link copyThroughJson}.
5
+ *
6
+ * @category JSON : Common
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {jsonify} from '@augment-vir/common';
12
+ *
13
+ * // `result` is `{b: 'b'}`
14
+ * const result = jsonify({
15
+ * map: new Map([
16
+ * [
17
+ * 'q',
18
+ * 'r',
19
+ * ],
20
+ * [
21
+ * 's',
22
+ * 't',
23
+ * ],
24
+ * ]),
25
+ * b: 'b',
26
+ * });
27
+ * ```
28
+ *
29
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
30
+ */
31
+ export declare function jsonify<T>(value: T): Jsonify<T>;
@@ -0,0 +1,33 @@
1
+ import JSON5 from 'json5';
2
+ /**
3
+ * Creates a JSON compatible version of the value given. Under the hood this is actually the same as
4
+ * {@link copyThroughJson}.
5
+ *
6
+ * @category JSON : Common
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {jsonify} from '@augment-vir/common';
12
+ *
13
+ * // `result` is `{b: 'b'}`
14
+ * const result = jsonify({
15
+ * map: new Map([
16
+ * [
17
+ * 'q',
18
+ * 'r',
19
+ * ],
20
+ * [
21
+ * 's',
22
+ * 't',
23
+ * ],
24
+ * ]),
25
+ * b: 'b',
26
+ * });
27
+ * ```
28
+ *
29
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
30
+ */
31
+ export function jsonify(value) {
32
+ return JSON5.parse(JSON5.stringify(value));
33
+ }