@augment-vir/common 29.2.0 → 30.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/LICENSE-CC0 +121 -0
  2. package/LICENSE-MIT +21 -0
  3. package/dist/augments/array/array-map.d.ts +6 -0
  4. package/dist/augments/array/array-map.js +4 -0
  5. package/dist/augments/array/array-to-object.d.ts +17 -0
  6. package/dist/augments/array/array-to-object.js +31 -0
  7. package/dist/augments/array/awaited/awaited-filter.d.ts +7 -0
  8. package/dist/augments/array/awaited/awaited-filter.js +7 -0
  9. package/dist/augments/array/awaited/awaited-for-each.d.ts +6 -0
  10. package/dist/augments/array/awaited/awaited-for-each.js +9 -0
  11. package/dist/augments/array/awaited/awaited-map.d.ts +1 -0
  12. package/dist/augments/array/awaited/awaited-map.js +8 -0
  13. package/dist/augments/array/filter.d.ts +3 -0
  14. package/dist/augments/array/filter.js +13 -0
  15. package/dist/augments/array/repeat-array.d.ts +1 -0
  16. package/dist/augments/array/repeat-array.js +3 -0
  17. package/dist/augments/array/shuffle-array.d.ts +2 -0
  18. package/dist/augments/array/shuffle-array.js +10 -0
  19. package/dist/augments/array/string-array.d.ts +1 -0
  20. package/dist/augments/array/string-array.js +3 -0
  21. package/dist/augments/boolean/if-truthy.d.ts +2 -0
  22. package/dist/{esm/augments/boolean.js → augments/boolean/if-truthy.js} +2 -7
  23. package/dist/augments/core-exports.d.ts +1 -0
  24. package/dist/augments/core-exports.js +1 -0
  25. package/dist/augments/enum/enum-value-check.d.ts +2 -0
  26. package/dist/augments/enum/enum-value-check.js +4 -0
  27. package/dist/augments/error/combine-errors.d.ts +3 -0
  28. package/dist/augments/error/combine-errors.js +12 -0
  29. package/dist/augments/function/call-asynchronously.d.ts +6 -0
  30. package/dist/augments/function/call-asynchronously.js +7 -0
  31. package/dist/augments/function/call-with-retries.d.ts +4 -0
  32. package/dist/augments/function/call-with-retries.js +27 -0
  33. package/dist/augments/function/debounce.d.ts +22 -0
  34. package/dist/augments/function/debounce.js +50 -0
  35. package/dist/augments/function/execution-duration.d.ts +8 -0
  36. package/dist/{esm/augments/time.js → augments/function/execution-duration.js} +7 -12
  37. package/dist/{types/augments → augments/function}/wrap-in-try.d.ts +2 -3
  38. package/dist/{esm/augments → augments/function}/wrap-in-try.js +4 -4
  39. package/dist/augments/json/append-json.d.ts +5 -0
  40. package/dist/augments/json/append-json.js +26 -0
  41. package/dist/augments/json/copy-through-json.d.ts +3 -0
  42. package/dist/augments/json/copy-through-json.js +11 -0
  43. package/dist/augments/json/jsonify.d.ts +2 -0
  44. package/dist/augments/json/jsonify.js +4 -0
  45. package/dist/augments/log/log-colors.d.ts +23 -0
  46. package/dist/augments/log/log-colors.js +116 -0
  47. package/dist/augments/log/log-string.d.ts +14 -0
  48. package/dist/augments/log/log-string.js +66 -0
  49. package/dist/augments/log/log-writer.d.ts +8 -0
  50. package/dist/augments/log/log.d.ts +13 -0
  51. package/dist/augments/log/log.js +42 -0
  52. package/dist/augments/log/logger.d.ts +11 -0
  53. package/dist/augments/log/logger.js +38 -0
  54. package/dist/augments/number/clamp.d.ts +7 -0
  55. package/dist/augments/number/clamp.js +8 -0
  56. package/dist/augments/number/coords.d.ts +19 -0
  57. package/dist/augments/number/digit.d.ts +1 -0
  58. package/dist/augments/number/dimensions.d.ts +19 -0
  59. package/dist/augments/number/min-max.d.ts +11 -0
  60. package/dist/augments/number/min-max.js +14 -0
  61. package/dist/augments/number/number-conversion.d.ts +23 -0
  62. package/dist/augments/number/number-conversion.js +50 -0
  63. package/dist/augments/number/round.d.ts +3 -0
  64. package/dist/augments/number/round.js +5 -0
  65. package/dist/augments/number/scientific.d.ts +1 -0
  66. package/dist/augments/number/scientific.js +3 -0
  67. package/dist/{esm/augments → augments/number}/truncate-number.js +14 -12
  68. package/dist/augments/number/wrap-number.d.ts +13 -0
  69. package/dist/augments/number/wrap-number.js +22 -0
  70. package/dist/augments/object/diff.d.ts +29 -0
  71. package/dist/augments/object/diff.js +133 -0
  72. package/dist/augments/object/empty.d.ts +3 -0
  73. package/dist/augments/object/get-or-set.d.ts +31 -0
  74. package/dist/{esm/augments → augments}/object/get-or-set.js +5 -3
  75. package/dist/{types/augments → augments}/object/key-count.d.ts +9 -5
  76. package/dist/augments/object/map-entries.d.ts +13 -0
  77. package/dist/augments/object/map-entries.js +57 -0
  78. package/dist/augments/object/map-enum.d.ts +4 -0
  79. package/dist/augments/object/map-enum.js +21 -0
  80. package/dist/{types/augments/object/map-object.d.ts → augments/object/map-values.d.ts} +6 -3
  81. package/dist/{esm/augments/object/map-object.js → augments/object/map-values.js} +9 -9
  82. package/dist/{esm/augments → augments}/object/merge-deep.js +5 -6
  83. package/dist/augments/object/merge-defined-properties.d.ts +6 -0
  84. package/dist/augments/object/merge-defined-properties.js +19 -0
  85. package/dist/{esm/augments → augments}/object/merge-property-arrays.js +1 -1
  86. package/dist/augments/object/object-entries.d.ts +4 -0
  87. package/dist/augments/object/object-entries.js +13 -0
  88. package/dist/augments/object/object-filter.d.ts +2 -0
  89. package/dist/augments/object/object-filter.js +7 -0
  90. package/dist/augments/object/object-keys.d.ts +2 -0
  91. package/dist/augments/object/object-keys.js +11 -0
  92. package/dist/augments/object/object-values.d.ts +2 -0
  93. package/dist/augments/object/object-values.js +4 -0
  94. package/dist/augments/prisma/prisma-models.d.ts +106 -0
  95. package/dist/augments/prisma/prisma-models.js +1 -0
  96. package/dist/augments/promise/timed-promise.d.ts +7 -0
  97. package/dist/augments/promise/timed-promise.js +36 -0
  98. package/dist/augments/random/random-boolean.d.ts +18 -0
  99. package/dist/augments/random/random-boolean.js +26 -0
  100. package/dist/augments/random/random-integer.d.ts +12 -0
  101. package/dist/augments/random/random-integer.js +32 -0
  102. package/dist/augments/random/random-string.d.ts +14 -0
  103. package/dist/augments/random/random-string.js +63 -0
  104. package/dist/{types/augments/regexp.d.ts → augments/regexp/regexp-flags.d.ts} +1 -1
  105. package/dist/augments/regexp/regexp-flags.js +18 -0
  106. package/dist/augments/regexp/regexp-string.d.ts +5 -0
  107. package/dist/augments/regexp/regexp-string.js +7 -0
  108. package/dist/augments/regexp/safe-match.d.ts +1 -0
  109. package/dist/augments/regexp/safe-match.js +4 -0
  110. package/dist/augments/selection-set/select-collapsed.d.ts +11 -0
  111. package/dist/augments/selection-set/select-collapsed.js +25 -0
  112. package/dist/augments/selection-set/select-from.d.ts +3 -0
  113. package/dist/augments/selection-set/select-from.js +25 -0
  114. package/dist/augments/selection-set/selection-set.d.ts +18 -0
  115. package/dist/augments/selection-set/selection-set.js +4 -0
  116. package/dist/augments/string/casing/capitalization.d.ts +3 -0
  117. package/dist/augments/string/casing/capitalization.js +11 -0
  118. package/dist/augments/string/casing/casing.d.ts +27 -0
  119. package/dist/augments/string/casing/casing.js +40 -0
  120. package/dist/augments/string/casing/kebab-and-camel.d.ts +3 -0
  121. package/dist/augments/string/casing/kebab-and-camel.js +42 -0
  122. package/dist/augments/string/commas.d.ts +7 -0
  123. package/dist/augments/string/commas.js +29 -0
  124. package/dist/augments/string/join.d.ts +10 -0
  125. package/dist/augments/string/join.js +23 -0
  126. package/dist/{types/augments/string/prefixes.d.ts → augments/string/prefix.d.ts} +16 -1
  127. package/dist/{esm/augments/string/prefixes.js → augments/string/prefix.js} +11 -1
  128. package/dist/augments/string/replace.d.ts +1 -0
  129. package/dist/augments/string/replace.js +5 -0
  130. package/dist/augments/string/split.d.ts +4 -0
  131. package/dist/augments/string/split.js +28 -0
  132. package/dist/augments/string/substring-index.d.ts +13 -0
  133. package/dist/augments/string/substring-index.js +38 -0
  134. package/dist/augments/string/suffix.d.ts +74 -0
  135. package/dist/augments/string/suffix.js +73 -0
  136. package/dist/augments/string/white-space.d.ts +5 -0
  137. package/dist/augments/string/white-space.js +12 -0
  138. package/dist/augments/string/wrap-string.d.ts +4 -0
  139. package/dist/augments/string/wrap-string.js +5 -0
  140. package/dist/augments/type/ensure-type.d.ts +8 -0
  141. package/dist/augments/type/ensure-type.js +10 -0
  142. package/dist/augments/type/readonly.d.ts +2 -0
  143. package/dist/augments/type/readonly.js +3 -0
  144. package/dist/augments/type/type-recursion.js +1 -0
  145. package/dist/augments/type/void-type.d.ts +5 -0
  146. package/dist/augments/type/void-type.js +1 -0
  147. package/dist/augments/type/writable.d.ts +3 -0
  148. package/dist/augments/type/writable.js +3 -0
  149. package/dist/index.d.ts +81 -0
  150. package/dist/index.js +81 -0
  151. package/package.json +33 -15
  152. package/README.md +0 -7
  153. package/dist/cjs/augments/ansi.js +0 -27
  154. package/dist/cjs/augments/array/array.js +0 -68
  155. package/dist/cjs/augments/array/remove-duplicates.js +0 -16
  156. package/dist/cjs/augments/async.js +0 -28
  157. package/dist/cjs/augments/boolean.js +0 -19
  158. package/dist/cjs/augments/common-number.js +0 -112
  159. package/dist/cjs/augments/common-string.js +0 -245
  160. package/dist/cjs/augments/debounce.js +0 -38
  161. package/dist/cjs/augments/environment.js +0 -6
  162. package/dist/cjs/augments/error.js +0 -52
  163. package/dist/cjs/augments/esm-path.js +0 -11
  164. package/dist/cjs/augments/function.js +0 -2
  165. package/dist/cjs/augments/json-compatible.js +0 -2
  166. package/dist/cjs/augments/json.js +0 -28
  167. package/dist/cjs/augments/object/any-object.js +0 -2
  168. package/dist/cjs/augments/object/enum.js +0 -48
  169. package/dist/cjs/augments/object/filter-object.js +0 -26
  170. package/dist/cjs/augments/object/get-or-set.js +0 -40
  171. package/dist/cjs/augments/object/has-key.js +0 -6
  172. package/dist/cjs/augments/object/jsonify.js +0 -6
  173. package/dist/cjs/augments/object/key-count.js +0 -2
  174. package/dist/cjs/augments/object/map-object.js +0 -61
  175. package/dist/cjs/augments/object/merge-deep.js +0 -52
  176. package/dist/cjs/augments/object/merge-property-arrays.js +0 -14
  177. package/dist/cjs/augments/object/object-entries.js +0 -40
  178. package/dist/cjs/augments/object/object.js +0 -18
  179. package/dist/cjs/augments/object/pick-deep.js +0 -2
  180. package/dist/cjs/augments/object/selection-set.js +0 -56
  181. package/dist/cjs/augments/object/typed-has-property.js +0 -29
  182. package/dist/cjs/augments/promise/deferred-promise.js +0 -31
  183. package/dist/cjs/augments/promise/promise.js +0 -79
  184. package/dist/cjs/augments/promise/wait.js +0 -66
  185. package/dist/cjs/augments/random.js +0 -123
  186. package/dist/cjs/augments/regexp.js +0 -19
  187. package/dist/cjs/augments/string/prefixes.js +0 -20
  188. package/dist/cjs/augments/string/suffixes.js +0 -40
  189. package/dist/cjs/augments/string/uuid.js +0 -7
  190. package/dist/cjs/augments/time.js +0 -35
  191. package/dist/cjs/augments/truncate-number.js +0 -153
  192. package/dist/cjs/augments/tuple.js +0 -20
  193. package/dist/cjs/augments/type-recursion.js +0 -2
  194. package/dist/cjs/augments/type.js +0 -36
  195. package/dist/cjs/augments/union.js +0 -2
  196. package/dist/cjs/augments/wrap-in-try.js +0 -37
  197. package/dist/cjs/index.js +0 -60
  198. package/dist/esm/augments/ansi.js +0 -24
  199. package/dist/esm/augments/array/array.js +0 -57
  200. package/dist/esm/augments/async.js +0 -23
  201. package/dist/esm/augments/common-number.js +0 -100
  202. package/dist/esm/augments/common-string.js +0 -227
  203. package/dist/esm/augments/debounce.js +0 -34
  204. package/dist/esm/augments/environment.js +0 -3
  205. package/dist/esm/augments/error.js +0 -45
  206. package/dist/esm/augments/json.js +0 -24
  207. package/dist/esm/augments/object/enum.js +0 -41
  208. package/dist/esm/augments/object/filter-object.js +0 -21
  209. package/dist/esm/augments/object/has-key.js +0 -3
  210. package/dist/esm/augments/object/jsonify.js +0 -3
  211. package/dist/esm/augments/object/object-entries.js +0 -32
  212. package/dist/esm/augments/object/object.js +0 -14
  213. package/dist/esm/augments/object/selection-set.js +0 -52
  214. package/dist/esm/augments/object/typed-has-property.js +0 -25
  215. package/dist/esm/augments/promise/deferred-promise.js +0 -28
  216. package/dist/esm/augments/promise/promise.js +0 -71
  217. package/dist/esm/augments/promise/wait.js +0 -60
  218. package/dist/esm/augments/random.js +0 -117
  219. package/dist/esm/augments/regexp.js +0 -14
  220. package/dist/esm/augments/string/suffixes.js +0 -31
  221. package/dist/esm/augments/string/uuid.js +0 -4
  222. package/dist/esm/augments/tuple.js +0 -16
  223. package/dist/esm/augments/type.js +0 -30
  224. package/dist/esm/index.js +0 -44
  225. package/dist/types/augments/ansi.d.ts +0 -1
  226. package/dist/types/augments/array/array.d.ts +0 -26
  227. package/dist/types/augments/async.d.ts +0 -14
  228. package/dist/types/augments/boolean.d.ts +0 -6
  229. package/dist/types/augments/common-number.d.ts +0 -52
  230. package/dist/types/augments/common-string.d.ts +0 -74
  231. package/dist/types/augments/debounce.d.ts +0 -15
  232. package/dist/types/augments/environment.d.ts +0 -1
  233. package/dist/types/augments/error.d.ts +0 -9
  234. package/dist/types/augments/function.d.ts +0 -25
  235. package/dist/types/augments/json.d.ts +0 -9
  236. package/dist/types/augments/object/any-object.d.ts +0 -2
  237. package/dist/types/augments/object/enum.d.ts +0 -8
  238. package/dist/types/augments/object/filter-object.d.ts +0 -4
  239. package/dist/types/augments/object/get-or-set.d.ts +0 -26
  240. package/dist/types/augments/object/has-key.d.ts +0 -1
  241. package/dist/types/augments/object/jsonify.d.ts +0 -2
  242. package/dist/types/augments/object/object-entries.d.ts +0 -8
  243. package/dist/types/augments/object/object.d.ts +0 -21
  244. package/dist/types/augments/object/pick-deep.d.ts +0 -14
  245. package/dist/types/augments/object/selection-set.d.ts +0 -27
  246. package/dist/types/augments/object/typed-has-property.d.ts +0 -7
  247. package/dist/types/augments/promise/deferred-promise.d.ts +0 -8
  248. package/dist/types/augments/promise/promise.d.ts +0 -15
  249. package/dist/types/augments/promise/wait.d.ts +0 -18
  250. package/dist/types/augments/random.d.ts +0 -39
  251. package/dist/types/augments/string/suffixes.d.ts +0 -17
  252. package/dist/types/augments/string/uuid.d.ts +0 -1
  253. package/dist/types/augments/time.d.ts +0 -14
  254. package/dist/types/augments/tuple.d.ts +0 -17
  255. package/dist/types/augments/type.d.ts +0 -69
  256. package/dist/types/augments/union.d.ts +0 -5
  257. package/dist/types/index.d.ts +0 -44
  258. /package/dist/{types/augments → augments}/array/remove-duplicates.d.ts +0 -0
  259. /package/dist/{esm/augments → augments}/array/remove-duplicates.js +0 -0
  260. /package/dist/{types/augments → augments/file}/esm-path.d.ts +0 -0
  261. /package/dist/{esm/augments → augments/file}/esm-path.js +0 -0
  262. /package/dist/{types/augments → augments/json}/json-compatible.d.ts +0 -0
  263. /package/dist/{esm/augments → augments/json}/json-compatible.js +0 -0
  264. /package/dist/{esm/augments/function.js → augments/log/log-writer.js} +0 -0
  265. /package/dist/{esm/augments/object/any-object.js → augments/number/coords.js} +0 -0
  266. /package/dist/{esm/augments/object/key-count.js → augments/number/digit.js} +0 -0
  267. /package/dist/{esm/augments/object/pick-deep.js → augments/number/dimensions.js} +0 -0
  268. /package/dist/{types/augments → augments/number}/truncate-number.d.ts +0 -0
  269. /package/dist/{esm/augments/type-recursion.js → augments/object/empty.js} +0 -0
  270. /package/dist/{esm/augments/union.js → augments/object/key-count.js} +0 -0
  271. /package/dist/{types/augments → augments}/object/merge-deep.d.ts +0 -0
  272. /package/dist/{types/augments → augments}/object/merge-property-arrays.d.ts +0 -0
  273. /package/dist/{types/augments → augments/type}/type-recursion.d.ts +0 -0
@@ -1,52 +0,0 @@
1
- import { isPrimitive } from 'run-time-assertions';
2
- import { isLengthAtLeast } from '../tuple';
3
- import { omitObjectKeys } from './filter-object';
4
- import { mapObjectValues } from './map-object';
5
- import { isObject } from './object';
6
- function shouldPreserve(input) {
7
- return isPrimitive(input) || input instanceof RegExp || input instanceof Promise;
8
- }
9
- export function selectFrom(originalObject, selectionSet) {
10
- if (Array.isArray(originalObject)) {
11
- return originalObject.map((originalEntry) => selectFrom(originalEntry, selectionSet));
12
- }
13
- const keysToRemove = [];
14
- return omitObjectKeys(mapObjectValues(originalObject, (key, value) => {
15
- const selection = selectionSet[key];
16
- if (selection === true) {
17
- return value;
18
- }
19
- else if (!selection) {
20
- keysToRemove.push(key);
21
- return undefined;
22
- }
23
- else if (shouldPreserve(value)) {
24
- return value;
25
- }
26
- else {
27
- return selectFrom(value, selection);
28
- }
29
- }), keysToRemove);
30
- }
31
- export function selectCollapsedFrom(originalObject, selectionSet) {
32
- const selected = selectFrom(originalObject, selectionSet);
33
- return collapseObject(selected, selectionSet);
34
- }
35
- function collapseObject(input, selectionSet) {
36
- if (shouldPreserve(input)) {
37
- return input;
38
- }
39
- const keys = Object.keys(input);
40
- if (Array.isArray(input)) {
41
- return input.map((innerInput) => collapseObject(innerInput, selectionSet));
42
- }
43
- else if (isLengthAtLeast(keys, 2)) {
44
- return input;
45
- }
46
- else if (isLengthAtLeast(keys, 1) && isObject(selectionSet)) {
47
- return collapseObject(input[keys[0]], selectionSet[keys[0]]);
48
- }
49
- else {
50
- return input;
51
- }
52
- }
@@ -1,25 +0,0 @@
1
- const hasPropertyAttempts = [
2
- (object, key) => {
3
- return key in object;
4
- },
5
- (object, key) => {
6
- /** This handles cases where the input object can't use `in` directly, like string literals */
7
- return key in object.constructor.prototype;
8
- },
9
- ];
10
- export function typedHasProperty(inputObject, inputKey) {
11
- if (!inputObject) {
12
- return false;
13
- }
14
- return hasPropertyAttempts.some((attemptCallback) => {
15
- try {
16
- return attemptCallback(inputObject, inputKey);
17
- }
18
- catch (error) {
19
- return false;
20
- }
21
- });
22
- }
23
- export function typedHasProperties(inputObject, inputKeys) {
24
- return inputObject && inputKeys.every((key) => typedHasProperty(inputObject, key));
25
- }
@@ -1,28 +0,0 @@
1
- export function createDeferredPromiseWrapper() {
2
- let resolve;
3
- let reject;
4
- let settled = false;
5
- const promise = new Promise((resolveCallback, rejectCallback) => {
6
- resolve = (value) => {
7
- settled = true;
8
- return resolveCallback(value);
9
- };
10
- reject = (reason) => {
11
- settled = true;
12
- rejectCallback(reason);
13
- };
14
- });
15
- // no way to test this edge case
16
- // istanbul ignore next
17
- if (!resolve || !reject) {
18
- throw new Error(`Reject and resolve callbacks were not set by the promise constructor for ${createDeferredPromiseWrapper.name}.`);
19
- }
20
- return {
21
- promise,
22
- resolve,
23
- reject,
24
- isSettled() {
25
- return settled;
26
- },
27
- };
28
- }
@@ -1,71 +0,0 @@
1
- import { typedHasProperty } from '../object/typed-has-property';
2
- export function isPromiseLike(input) {
3
- if (typedHasProperty(input, 'then') && typeof input.then === 'function') {
4
- return true;
5
- }
6
- else {
7
- return false;
8
- }
9
- }
10
- export class PromiseTimeoutError extends Error {
11
- constructor(durationMs, message = `Promised timed out after ${durationMs} ms.`) {
12
- super(message);
13
- Object.defineProperty(this, "durationMs", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: durationMs
18
- });
19
- Object.defineProperty(this, "message", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: message
24
- });
25
- Object.defineProperty(this, "name", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: 'PromiseTimeoutError'
30
- });
31
- }
32
- }
33
- export function wrapPromiseInTimeout(durationMs, originalPromise) {
34
- return new Promise(async (resolve, reject) => {
35
- const timeoutId = durationMs === Infinity
36
- ? undefined
37
- : setTimeout(() => {
38
- reject(new PromiseTimeoutError(durationMs));
39
- }, durationMs);
40
- try {
41
- const result = await originalPromise;
42
- resolve(result);
43
- }
44
- catch (error) {
45
- reject(error);
46
- }
47
- finally {
48
- clearTimeout(timeoutId);
49
- }
50
- });
51
- }
52
- /**
53
- * Call a function asynchronously without interrupting current synchronous execution, even if the
54
- * function was originally synchronous.
55
- */
56
- export async function callAsynchronously(callback) {
57
- return await Promise.resolve().then(() => callback());
58
- }
59
- export async function executeWithRetries(retryCount, callback) {
60
- let currentRetry = 0;
61
- while (currentRetry < retryCount) {
62
- try {
63
- const result = await callback();
64
- return result;
65
- }
66
- catch (error) {
67
- currentRetry++;
68
- }
69
- }
70
- throw new Error('Retry max reached.');
71
- }
@@ -1,60 +0,0 @@
1
- import { ensureErrorAndPrependMessage } from '../error';
2
- import { createDeferredPromiseWrapper } from './deferred-promise';
3
- export function wait(delayMs) {
4
- const deferredPromiseWrapper = createDeferredPromiseWrapper();
5
- if (delayMs !== Infinity) {
6
- setTimeout(() => {
7
- deferredPromiseWrapper.resolve();
8
- }, delayMs <= 0 ? 0 : delayMs);
9
- }
10
- return deferredPromiseWrapper.promise;
11
- }
12
- export async function waitValue(delayMs, returnValue) {
13
- return wait(delayMs).then(() => returnValue);
14
- }
15
- export const defaultWaitUntilDefinedOptions = {
16
- interval: {
17
- milliseconds: 100,
18
- },
19
- timeout: {
20
- milliseconds: 10_000,
21
- },
22
- };
23
- /**
24
- * Runs the predicate until it returns a truthy value, then returns that value. Use the options
25
- * input to modify the timeout and interval durations. Automatically catches errors and handles
26
- * async predicates.
27
- */
28
- export async function waitUntilTruthy(predicate, failureMessage, optionsInput = {}) {
29
- const options = {
30
- ...defaultWaitUntilDefinedOptions,
31
- ...optionsInput,
32
- };
33
- let lastValue = undefined;
34
- let lastError;
35
- async function checkCondition() {
36
- try {
37
- lastValue = await predicate();
38
- }
39
- catch (error) {
40
- lastValue = undefined;
41
- lastError = error;
42
- }
43
- }
44
- const startTime = Date.now();
45
- while (!lastValue) {
46
- await checkCondition();
47
- await wait(options.interval.milliseconds);
48
- if (Date.now() - startTime >= options.timeout.milliseconds) {
49
- const message = failureMessage ? `${failureMessage}: ` : '';
50
- const preMessage = `${message}Timeout of "${options.timeout.milliseconds}" exceeded waiting for value to be defined`;
51
- if (lastError) {
52
- throw ensureErrorAndPrependMessage(lastError, preMessage);
53
- }
54
- else {
55
- throw new Error(preMessage);
56
- }
57
- }
58
- }
59
- return lastValue;
60
- }
@@ -1,117 +0,0 @@
1
- import { clamp, ensureMinAndMax } from './common-number';
2
- function accessCrypto() {
3
- if (globalThis.crypto) {
4
- return globalThis.crypto;
5
- }
6
- else {
7
- return require('crypto').webcrypto;
8
- }
9
- }
10
- const crypto = accessCrypto();
11
- /**
12
- * Creates a random integer (decimal points are all cut off) between the given min and max
13
- * (inclusive).
14
- *
15
- * This function uses cryptographically secure randomness.
16
- */
17
- export function randomInteger({ min: rawMin, max: rawMax }) {
18
- const { min, max } = ensureMinAndMax({ min: Math.floor(rawMin), max: Math.floor(rawMax) });
19
- const range = max - min + 1;
20
- const neededByteCount = Math.ceil(Math.log2(range) / 8);
21
- const cutoff = Math.floor(256 ** neededByteCount / range) * range;
22
- const currentBytes = new Uint8Array(neededByteCount);
23
- let value;
24
- do {
25
- crypto.getRandomValues(currentBytes);
26
- value = currentBytes.reduce((accum, byte, index) => {
27
- return accum + byte * 256 ** index;
28
- }, 0);
29
- } while (value >= cutoff);
30
- return min + (value % range);
31
- }
32
- /**
33
- * Returns true at rate of the percentLikelyToBeTrue input. Inputs should be whole numbers which
34
- * will be treated like percents. Anything outside of 0-100 inclusively will be clamped. An input 0
35
- * will always return true. An input of 100 will always return true. Decimals on the input will be
36
- * chopped off, use whole numbers.
37
- *
38
- * This function uses cryptographically secure randomness.
39
- *
40
- * @example
41
- * randomBoolean(50); // 50% chance to return true
42
- *
43
- * @example
44
- * randomBoolean(0); // always false, 0% chance of being true
45
- *
46
- * @example
47
- * randomBoolean(100); // always true, 100% chance of being true
48
- *
49
- * @example
50
- * randomBoolean(59.67; // 59% chance of being true
51
- */
52
- export function randomBoolean(percentLikelyToBeTrue = 50) {
53
- return (randomInteger({ min: 0, max: 99 }) <
54
- clamp({
55
- value: Math.floor(percentLikelyToBeTrue),
56
- min: 0,
57
- max: 100,
58
- }));
59
- }
60
- /** Creates a cryptographically secure uuid. */
61
- export function createUuid() {
62
- return crypto.randomUUID();
63
- }
64
- const validStringCharacters = [
65
- 'a',
66
- 'b',
67
- 'c',
68
- 'd',
69
- 'e',
70
- 'f',
71
- 'g',
72
- 'h',
73
- 'i',
74
- 'j',
75
- 'k',
76
- 'l',
77
- 'm',
78
- 'n',
79
- 'o',
80
- 'p',
81
- 'q',
82
- 'r',
83
- 's',
84
- 't',
85
- 'u',
86
- 'v',
87
- 'w',
88
- 'x',
89
- 'y',
90
- 'z',
91
- 0,
92
- 1,
93
- 2,
94
- 3,
95
- 4,
96
- 5,
97
- 6,
98
- 7,
99
- 8,
100
- 9,
101
- ];
102
- /**
103
- * Creates a random string (including letters and numbers) of a given length.
104
- *
105
- * This function uses cryptographically secure randomness.
106
- */
107
- export function randomString(inputLength = 16) {
108
- let stringBuilder = '';
109
- for (let i = 0; i < inputLength; i++) {
110
- const index = randomInteger({
111
- min: 0,
112
- max: validStringCharacters.length - 1,
113
- });
114
- stringBuilder += validStringCharacters[index];
115
- }
116
- return stringBuilder;
117
- }
@@ -1,14 +0,0 @@
1
- export function deDupeRegExFlags(flags) {
2
- const deDuped = new Set(Array.from(flags.toLowerCase()));
3
- return Array.from(deDuped).join('');
4
- }
5
- export function addRegExpFlags(originalRegExp, flags) {
6
- return new RegExp(originalRegExp.source, deDupeRegExFlags([
7
- originalRegExp.flags,
8
- flags,
9
- ].join('')));
10
- }
11
- export function safeMatch(input, regExp) {
12
- const match = input.match(regExp);
13
- return match ?? [];
14
- }
@@ -1,31 +0,0 @@
1
- import { toEnsuredNumber } from '../common-number';
2
- export const percentSuffix = '%';
3
- export const pxSuffix = 'px';
4
- export function addPx(input) {
5
- return addSuffix({ value: input, suffix: pxSuffix });
6
- }
7
- export function removePx(input) {
8
- return toEnsuredNumber(removeSuffix({ value: input, suffix: pxSuffix }));
9
- }
10
- export function addPercent(input) {
11
- return addSuffix({ value: input, suffix: percentSuffix });
12
- }
13
- export function removePercent(input) {
14
- return toEnsuredNumber(removeSuffix({ value: input, suffix: percentSuffix }));
15
- }
16
- export function addSuffix({ value, suffix, }) {
17
- if (String(value).endsWith(suffix)) {
18
- return String(value);
19
- }
20
- else {
21
- return `${String(value)}${suffix}`;
22
- }
23
- }
24
- export function removeSuffix({ value, suffix, }) {
25
- if (value.endsWith(suffix)) {
26
- return value.substring(0, value.length - suffix.length);
27
- }
28
- else {
29
- return value;
30
- }
31
- }
@@ -1,4 +0,0 @@
1
- const uuidRegExp = /[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/;
2
- export function isUuid(maybeUuid) {
3
- return !!maybeUuid.match(uuidRegExp);
4
- }
@@ -1,16 +0,0 @@
1
- export function isLengthAtLeast(array, length) {
2
- try {
3
- assertLengthAtLeast(array, length);
4
- return true;
5
- }
6
- catch (error) {
7
- return false;
8
- }
9
- }
10
- export function assertLengthAtLeast(array, length, arrayName) {
11
- if (array.length < length) {
12
- throw new Error(arrayName
13
- ? `'${arrayName}' is not at least '${length}' in length.`
14
- : `Array is not at least '${length}' in length.`);
15
- }
16
- }
@@ -1,30 +0,0 @@
1
- export function makeWritable(input) {
2
- return input;
3
- }
4
- export function makeReadonly(input) {
5
- return input;
6
- }
7
- /**
8
- * This function returns another function that simply returns whatever input it's given. However, it
9
- * also checks that the input matches the original wrapNarrowTypeWithTypeCheck's generic, while
10
- * maintaining strict "const" like typing.
11
- *
12
- * Use like this: wrapNarrowTypeWithTypeCheck<EnforcedTypeHere>()(valueToEnforceTypeOn as const)
13
- *
14
- * Sometimes "as const" isn't required, usually it is for any object or array though.
15
- */
16
- export function wrapNarrowTypeWithTypeCheck() {
17
- return (input) => {
18
- return input;
19
- };
20
- }
21
- /**
22
- * This is a type helper that ensures the given input matches the given generic type. The generic is
23
- * setup in such a way that if it is omitted (which is typically allowed in TypeScript, resulting in
24
- * the generic being inferred from the inputs), there will actually be a type error. This forces
25
- * each usage of this function to explicitly specify the generic, thus giving us type safety for the
26
- * input.
27
- */
28
- export function ensureType(input) {
29
- return input;
30
- }
package/dist/esm/index.js DELETED
@@ -1,44 +0,0 @@
1
- export * from './augments/ansi';
2
- export * from './augments/array/array';
3
- export * from './augments/array/remove-duplicates';
4
- export * from './augments/async';
5
- export * from './augments/boolean';
6
- export * from './augments/common-number';
7
- export * from './augments/common-string';
8
- export * from './augments/debounce';
9
- export * from './augments/environment';
10
- export * from './augments/error';
11
- export * from './augments/esm-path';
12
- export * from './augments/function';
13
- export * from './augments/json';
14
- export * from './augments/json-compatible';
15
- export * from './augments/object/any-object';
16
- export * from './augments/object/enum';
17
- export * from './augments/object/filter-object';
18
- export * from './augments/object/get-or-set';
19
- export * from './augments/object/has-key';
20
- export * from './augments/object/jsonify';
21
- export * from './augments/object/key-count';
22
- export * from './augments/object/map-object';
23
- export * from './augments/object/merge-deep';
24
- export * from './augments/object/merge-property-arrays';
25
- export * from './augments/object/object';
26
- export * from './augments/object/object-entries';
27
- export * from './augments/object/pick-deep';
28
- export * from './augments/object/selection-set';
29
- export * from './augments/object/typed-has-property';
30
- export * from './augments/promise/deferred-promise';
31
- export * from './augments/promise/promise';
32
- export * from './augments/promise/wait';
33
- export * from './augments/random';
34
- export * from './augments/regexp';
35
- export * from './augments/string/prefixes';
36
- export * from './augments/string/suffixes';
37
- export * from './augments/string/uuid';
38
- export * from './augments/time';
39
- export * from './augments/truncate-number';
40
- export * from './augments/tuple';
41
- export * from './augments/type';
42
- export * from './augments/type-recursion';
43
- export * from './augments/union';
44
- export * from './augments/wrap-in-try';
@@ -1 +0,0 @@
1
- export declare const ansiRegex: RegExp;
@@ -1,26 +0,0 @@
1
- import { AtLeastTuple } from '../tuple';
2
- import { ArrayElement } from '../type';
3
- export declare function filterOutIndexes<T>(array: ReadonlyArray<T>, indexes: ReadonlyArray<number>): T[];
4
- export declare function flatten2dArray<T>(array2d: ReadonlyArray<ReadonlyArray<T>>): T[];
5
- export type AtLeastOneEntryArray<ArrayGeneric extends ReadonlyArray<any>> = AtLeastTuple<ArrayElement<ArrayGeneric>, 1>;
6
- export declare function trimArrayStrings(input: ReadonlyArray<string>): string[];
7
- export declare function typedArrayIncludes<T>(array: ReadonlyArray<T>, input: unknown): input is T;
8
- type MapCallbackType<ArrayType extends ReadonlyArray<any>, OutputType> = (value: ArrayElement<ArrayType>, index: number, array: ArrayType) => OutputType;
9
- /** Preserves tuple types. */
10
- export declare function typedMap<InputArrayGeneric extends ReadonlyArray<any>, OutputType>(arrayToMap: InputArrayGeneric, mapCallback: MapCallbackType<InputArrayGeneric, OutputType>): {
11
- [Index in keyof InputArrayGeneric]: OutputType;
12
- };
13
- export declare function repeatArray<T>(repeatCount: number, array: T[]): T[];
14
- /**
15
- * Polyfill for `Object.groupBy`:
16
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
17
- */
18
- export declare function groupArrayBy<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Partial<Record<NewKey, ElementType[]>>;
19
- /**
20
- * Like `groupArrayBy` but maps array entries to a single key. Meaning, the resulting object does
21
- * not have an array of elements (unless the original array itself contains arrays).
22
- */
23
- export declare function arrayToObject<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Partial<Record<NewKey, ElementType>>;
24
- export declare function filterMap<ElementType, MappedEntry, TypeGuarded extends MappedEntry>(inputArray: ReadonlyArray<ElementType>, mapCallback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MappedEntry, filterCallback: (mappedOutput: MappedEntry, originalEntry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => mappedOutput is TypeGuarded): TypeGuarded[];
25
- export declare function filterMap<ElementType, MappedEntry>(inputArray: ReadonlyArray<ElementType>, mapCallback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MappedEntry, filterCallback: (mappedOutput: MappedEntry, originalEntry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => boolean): MappedEntry[];
26
- export {};
@@ -1,14 +0,0 @@
1
- /**
2
- * Acts like calling Array.prototype.forEach in that all elements are executed upon in order, and
3
- * each execution is blocking. Meaning, the callback won't be called on element 2 until the callback
4
- * has finished its call on element 1.
5
- */
6
- export declare function awaitedForEach<OriginalGeneric>(input: ReadonlyArray<OriginalGeneric>, callback: (arrayElement: OriginalGeneric, index: number, wholeArray: ReadonlyArray<OriginalGeneric>) => void | PromiseLike<void>): Promise<void>;
7
- export declare function awaitedBlockingMap<OriginalGeneric, MappedGeneric>(input: ReadonlyArray<OriginalGeneric>, callback: (arrayElement: OriginalGeneric, index: number, wholeArray: ReadonlyArray<OriginalGeneric>) => MappedGeneric | PromiseLike<MappedGeneric>): Promise<Awaited<MappedGeneric>[]>;
8
- export declare function awaitedFilter<OriginalGeneric>(arrayInput: ReadonlyArray<OriginalGeneric>, filterCallback: (arrayElement: OriginalGeneric, index: number, wholeArray: ReadonlyArray<OriginalGeneric>) => Promise<unknown>, options?: {
9
- /**
10
- * Each call to the filter callback is blocking, meaning the next one won't start until the
11
- * current one finishes. By default this is false.
12
- */
13
- blocking?: boolean | undefined;
14
- }): Promise<OriginalGeneric[]>;
@@ -1,6 +0,0 @@
1
- export type FalsyTypes = undefined | null | false | 0 | '' | -0 | 0n;
2
- export type Truthy<T> = Exclude<T, FalsyTypes>;
3
- export type Falsy<T> = Extract<T, FalsyTypes>;
4
- export declare function isTruthy<T>(input: T): input is Truthy<T>;
5
- export declare function isFalsy<T>(input: T): input is Falsy<T>;
6
- export declare function ifTruthy<const InputType, IfTruthyType, IfFalsyType>(checkThis: InputType, ifTruthyCallback: (truthyInput: Truthy<InputType>) => IfTruthyType, ifFalsyCallback: (truthyInput: Falsy<InputType>) => IfFalsyType): IfTruthyType | IfFalsyType;
@@ -1,52 +0,0 @@
1
- export declare const NaNString: string;
2
- export declare function addCommasToNumber(input: number | string): string;
3
- export declare function doesRequireScientificNotation(input: number): boolean;
4
- /**
5
- * Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
6
- * that is not the case, the returned value is the given min and max values swapped.
7
- */
8
- export declare function ensureMinAndMax({ min, max }: {
9
- min: number;
10
- max: number;
11
- }): {
12
- min: number;
13
- max: number;
14
- };
15
- /**
16
- * Tries to convert the input into a number. Handles strings with commas. Note: this might return
17
- * `NaN`.
18
- */
19
- export declare function toNumber(input: unknown): number;
20
- /** @deprecated Use {@link toNumber} instead. */
21
- export declare const convertIntoNumber: typeof toNumber;
22
- /** Tries to convert the input into a number and throws an error if `NaN` is created. */
23
- export declare function toEnsuredNumber(input: unknown): number;
24
- /** Tries to convert the input into a number and returns `undefined` if `NaN` is created. */
25
- export declare function toMaybeNumber(input: unknown): number | undefined;
26
- /**
27
- * If the given value is outside the given min/max bounds, instead of clamping the number (as the
28
- * `clamp` function does), this function wraps the value around to the next bound.
29
- *
30
- * @example
31
- * wrapNumber({min: 0, max: 100, value: 101}) == 0;
32
- */
33
- export declare function wrapNumber({ max, min, value }: {
34
- value: number;
35
- max: number;
36
- min: number;
37
- }): number;
38
- export declare function round(inputs: {
39
- number: number;
40
- digits: number;
41
- }): number;
42
- /** Clamp's the given value to within the min and max bounds, inclusive. */
43
- export declare function clamp({ value, min, max }: {
44
- value: number;
45
- min: number;
46
- max: number;
47
- }): number;
48
- /** Standard box dimensions. */
49
- export type Dimensions = {
50
- width: number;
51
- height: number;
52
- };