@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
@@ -0,0 +1,4 @@
1
+ import JSON5 from 'json5';
2
+ export function jsonify(input) {
3
+ return JSON5.parse(JSON5.stringify(input));
4
+ }
@@ -0,0 +1,23 @@
1
+ export declare enum LogOutputType {
2
+ Standard = "stdout",
3
+ Error = "stderr"
4
+ }
5
+ export declare enum LogColorKey {
6
+ Bold = "bold",
7
+ Error = "error",
8
+ Faint = "faint",
9
+ Info = "info",
10
+ Mutate = "mutate",
11
+ NormalWeight = "normalWeight",
12
+ Plain = "plain",
13
+ Reset = "reset",
14
+ Success = "success",
15
+ Warning = "warning"
16
+ }
17
+ export type LogColorConfig = Readonly<Record<LogColorKey, {
18
+ /** Either an array of CSS property assignments */
19
+ colors: string[];
20
+ logType: LogOutputType;
21
+ }>>;
22
+ export declare const logColors: Readonly<Record<LogColorKey, string>>;
23
+ export declare const defaultLogColorConfig: LogColorConfig;
@@ -0,0 +1,116 @@
1
+ import { forEachEnv, RuntimeEnv } from '@augment-vir/core';
2
+ export var LogOutputType;
3
+ (function (LogOutputType) {
4
+ LogOutputType["Standard"] = "stdout";
5
+ LogOutputType["Error"] = "stderr";
6
+ })(LogOutputType || (LogOutputType = {}));
7
+ export var LogColorKey;
8
+ (function (LogColorKey) {
9
+ LogColorKey["Bold"] = "bold";
10
+ LogColorKey["Error"] = "error";
11
+ LogColorKey["Faint"] = "faint";
12
+ LogColorKey["Info"] = "info";
13
+ LogColorKey["Mutate"] = "mutate";
14
+ LogColorKey["NormalWeight"] = "normalWeight";
15
+ LogColorKey["Plain"] = "plain";
16
+ LogColorKey["Reset"] = "reset";
17
+ LogColorKey["Success"] = "success";
18
+ LogColorKey["Warning"] = "warning";
19
+ })(LogColorKey || (LogColorKey = {}));
20
+ async function determineDefaultLogColors() {
21
+ return await forEachEnv({
22
+ /** We calculate coverage in web, so the node code will never run in coverage tests. */
23
+ /* node:coverage disable */
24
+ async [RuntimeEnv.Node]() {
25
+ const styles = (await import('ansi-styles')).default;
26
+ return {
27
+ [LogColorKey.Bold]: styles.bold.open,
28
+ [LogColorKey.Error]: styles.red.open,
29
+ [LogColorKey.Faint]: styles.gray.open,
30
+ [LogColorKey.Info]: styles.cyan.open,
31
+ [LogColorKey.Mutate]: styles.magenta.open,
32
+ [LogColorKey.NormalWeight]: '\x1b[22m',
33
+ [LogColorKey.Plain]: '',
34
+ [LogColorKey.Reset]: styles.reset.open,
35
+ [LogColorKey.Success]: styles.green.open,
36
+ [LogColorKey.Warning]: styles.yellow.open,
37
+ };
38
+ },
39
+ /* node:coverage enable */
40
+ [RuntimeEnv.Web]() {
41
+ return Promise.resolve({
42
+ [LogColorKey.Bold]: 'font-weight: bold',
43
+ [LogColorKey.Error]: 'color: red',
44
+ [LogColorKey.Faint]: 'color: grey',
45
+ [LogColorKey.Info]: 'color: teal',
46
+ [LogColorKey.Mutate]: 'color: magenta',
47
+ [LogColorKey.NormalWeight]: '',
48
+ [LogColorKey.Plain]: '',
49
+ [LogColorKey.Reset]: '',
50
+ [LogColorKey.Success]: 'color: green',
51
+ [LogColorKey.Warning]: 'color: orange',
52
+ });
53
+ },
54
+ });
55
+ }
56
+ export const logColors = await determineDefaultLogColors();
57
+ export const defaultLogColorConfig = {
58
+ [LogColorKey.Bold]: {
59
+ colors: [
60
+ logColors.bold,
61
+ ],
62
+ logType: LogOutputType.Standard,
63
+ },
64
+ [LogColorKey.Faint]: {
65
+ colors: [
66
+ logColors.faint,
67
+ ],
68
+ logType: LogOutputType.Standard,
69
+ },
70
+ [LogColorKey.Info]: {
71
+ colors: [
72
+ logColors.info,
73
+ ],
74
+ logType: LogOutputType.Standard,
75
+ },
76
+ [LogColorKey.Mutate]: {
77
+ colors: [
78
+ logColors.mutate,
79
+ logColors.bold,
80
+ ],
81
+ logType: LogOutputType.Standard,
82
+ },
83
+ [LogColorKey.NormalWeight]: {
84
+ colors: [
85
+ logColors.normalWeight,
86
+ ],
87
+ logType: LogOutputType.Standard,
88
+ },
89
+ [LogColorKey.Plain]: { colors: [], logType: LogOutputType.Standard },
90
+ [LogColorKey.Reset]: {
91
+ colors: [
92
+ logColors.reset,
93
+ ],
94
+ logType: LogOutputType.Standard,
95
+ },
96
+ [LogColorKey.Success]: {
97
+ colors: [
98
+ logColors.success,
99
+ logColors.bold,
100
+ ],
101
+ logType: LogOutputType.Standard,
102
+ },
103
+ [LogColorKey.Error]: {
104
+ colors: [
105
+ logColors.error,
106
+ logColors.bold,
107
+ ],
108
+ logType: LogOutputType.Error,
109
+ },
110
+ [LogColorKey.Warning]: {
111
+ colors: [
112
+ logColors.warning,
113
+ ],
114
+ logType: LogOutputType.Error,
115
+ },
116
+ };
@@ -0,0 +1,14 @@
1
+ import { LogColorKey, type LogColorConfig } from './log-colors.js';
2
+ import { LogWriterParams } from './log-writer.js';
3
+ export type LoggerOptions = {
4
+ colorConfig: LogColorConfig;
5
+ omitColors: boolean;
6
+ };
7
+ export type ToLogStringParams = {
8
+ colorKey: LogColorKey;
9
+ args: ReadonlyArray<any>;
10
+ options: Readonly<LoggerOptions>;
11
+ };
12
+ type ToLogString = (params: Readonly<ToLogStringParams>) => LogWriterParams;
13
+ export declare const toLogString: ToLogString;
14
+ export {};
@@ -0,0 +1,66 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { forEachEnv, RuntimeEnv, stringify } from '@augment-vir/core';
3
+ import { filterMap } from '../array/filter.js';
4
+ import { removeSuffix } from '../string/suffix.js';
5
+ import { LogColorKey } from './log-colors.js';
6
+ async function createToLogString() {
7
+ return await forEachEnv({
8
+ /** We calculate coverage in web, so the node code will never run in coverage tests. */
9
+ /* node:coverage disable */
10
+ async [RuntimeEnv.Node]() {
11
+ const { inspect } = await import('node:util');
12
+ return ({ args, colorKey, options }) => {
13
+ const argStrings = args.map((arg) => {
14
+ if (typeof arg === 'string') {
15
+ return arg;
16
+ }
17
+ else {
18
+ return inspect(arg);
19
+ }
20
+ });
21
+ const colorsString = options.omitColors
22
+ ? ''
23
+ : options.colorConfig[colorKey].colors.join('');
24
+ const text = [
25
+ colorsString,
26
+ argStrings.join('\n'),
27
+ options.omitColors
28
+ ? ''
29
+ : options.colorConfig[LogColorKey.Reset].colors.join(''),
30
+ ].join('');
31
+ return { text, css: undefined };
32
+ };
33
+ },
34
+ /**
35
+ * We have no way to test color output in the browser console so this block is ignored in
36
+ * coverage as well.
37
+ */
38
+ [RuntimeEnv.Web]() {
39
+ return ({ args, colorKey, options }) => {
40
+ const css = options.omitColors
41
+ ? undefined
42
+ : filterMap(options.colorConfig[colorKey].colors, (cssString) => removeSuffix({
43
+ value: cssString,
44
+ suffix: ';',
45
+ }), check.isTruthy).join('; ');
46
+ const argStrings = args.map((arg) => {
47
+ if (typeof arg === 'string') {
48
+ return arg;
49
+ }
50
+ else {
51
+ return stringify(arg);
52
+ }
53
+ });
54
+ const text = [
55
+ argStrings.join('\n'),
56
+ options.omitColors
57
+ ? ''
58
+ : options.colorConfig[LogColorKey.Reset].colors.join(''),
59
+ ].join('');
60
+ return { text, css };
61
+ };
62
+ },
63
+ /* node:coverage enable */
64
+ });
65
+ }
66
+ export const toLogString = await createToLogString();
@@ -0,0 +1,8 @@
1
+ import { type LogOutputType } from './log-colors.js';
2
+ export type LogWriterParams = {
3
+ text: string;
4
+ /** Typically this is only relevant in a browser console. */
5
+ css: string | undefined;
6
+ };
7
+ export type LogWriter = (params: Readonly<LogWriterParams>) => void;
8
+ export type LogWriters = Record<LogOutputType, LogWriter>;
@@ -0,0 +1,13 @@
1
+ import { type PartialWithUndefined } from '@augment-vir/core';
2
+ import { LoggerOptions } from './log-string.js';
3
+ import { LogWriters } from './log-writer.js';
4
+ import { type Logger } from './logger.js';
5
+ export declare const defaultLogWriters: LogWriters;
6
+ export declare const log: Logger;
7
+ export declare function createLoggerWithStoredLogs(options?: PartialWithUndefined<LoggerOptions> | undefined): {
8
+ log: Logger;
9
+ logs: {
10
+ stdout: string[];
11
+ stderr: string[];
12
+ };
13
+ };
@@ -0,0 +1,42 @@
1
+ import { isRuntimeEnv, RuntimeEnv } from '@augment-vir/core';
2
+ import { addPrefix } from '../string/prefix.js';
3
+ import { LogOutputType } from './log-colors.js';
4
+ import { createLogger } from './logger.js';
5
+ export const defaultLogWriters =
6
+ /** We calculate coverage in web, so the node code will never run in coverage tests. */
7
+ /* node:coverage disable */
8
+ isRuntimeEnv(RuntimeEnv.Node)
9
+ ? {
10
+ [LogOutputType.Error]({ text }) {
11
+ process.stderr.write(text + '\n');
12
+ },
13
+ [LogOutputType.Standard]({ text }) {
14
+ process.stdout.write(text + '\n');
15
+ },
16
+ }
17
+ : /* node:coverage enable */
18
+ {
19
+ [LogOutputType.Error]({ text, css }) {
20
+ console.error(addPrefix({ value: text, prefix: '%c' }), css);
21
+ },
22
+ [LogOutputType.Standard]({ text, css }) {
23
+ // eslint-disable-next-line no-console
24
+ console.log(addPrefix({ value: text, prefix: '%c' }), css);
25
+ },
26
+ };
27
+ export const log = createLogger(defaultLogWriters);
28
+ export function createLoggerWithStoredLogs(options) {
29
+ const logs = {
30
+ stdout: [],
31
+ stderr: [],
32
+ };
33
+ const log = createLogger({
34
+ stderr({ text }) {
35
+ logs.stderr.push(text);
36
+ },
37
+ stdout({ text }) {
38
+ logs.stdout.push(text);
39
+ },
40
+ }, options);
41
+ return { log, logs };
42
+ }
@@ -0,0 +1,11 @@
1
+ import type { PartialWithUndefined } from '@augment-vir/core';
2
+ import { LogColorKey } from './log-colors.js';
3
+ import { type LoggerOptions } from './log-string.js';
4
+ import { type LogWriters } from './log-writer.js';
5
+ export type LoggerLogs = Readonly<Record<LogColorKey, (...args: ReadonlyArray<unknown>) => void>>;
6
+ export type Logger = LoggerLogs & {
7
+ if: (condition: boolean) => LoggerLogs;
8
+ };
9
+ export declare const defaultLoggerOptions: LoggerOptions;
10
+ export declare const emptyLog: Logger;
11
+ export declare function createLogger(logWriters: LogWriters, optionsOverride?: PartialWithUndefined<LoggerOptions> | undefined): Logger;
@@ -0,0 +1,38 @@
1
+ import { mapEnumToObject } from '../object/map-enum.js';
2
+ import { mergeDefinedProperties } from '../object/merge-defined-properties.js';
3
+ import { defaultLogColorConfig, LogColorKey, LogOutputType } from './log-colors.js';
4
+ import { toLogString } from './log-string.js';
5
+ export const defaultLoggerOptions = {
6
+ colorConfig: defaultLogColorConfig,
7
+ omitColors: false,
8
+ };
9
+ export const emptyLog = createLogger({
10
+ [LogOutputType.Error]() { },
11
+ [LogOutputType.Standard]() { },
12
+ });
13
+ export function createLogger(logWriters, optionsOverride) {
14
+ const options = mergeDefinedProperties(defaultLoggerOptions, optionsOverride);
15
+ function writeLog(params) {
16
+ logWriters[options.colorConfig[params.colorKey].logType](toLogString({
17
+ ...params,
18
+ options,
19
+ }));
20
+ }
21
+ const loggerLogs = mapEnumToObject(LogColorKey, (colorKey) => {
22
+ return (...args) => writeLog({
23
+ args,
24
+ colorKey,
25
+ });
26
+ });
27
+ return {
28
+ ...loggerLogs,
29
+ if(condition) {
30
+ if (condition) {
31
+ return loggerLogs;
32
+ }
33
+ else {
34
+ return emptyLog;
35
+ }
36
+ },
37
+ };
38
+ }
@@ -0,0 +1,7 @@
1
+ import { MinMax } from './min-max.js';
2
+ /**
3
+ * Clamp's the given value to within the min and max bounds, inclusive.
4
+ *
5
+ * @category Number:Common
6
+ */
7
+ export declare function clamp(value: number, { min, max }: MinMax): number;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Clamp's the given value to within the min and max bounds, inclusive.
3
+ *
4
+ * @category Number:Common
5
+ */
6
+ export function clamp(value, { min, max }) {
7
+ return Math.min(Math.max(value, min), max);
8
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A simple type for storing 2D coordinates.
3
+ *
4
+ * @category Number:Common
5
+ */
6
+ export type Coords = {
7
+ x: number;
8
+ y: number;
9
+ };
10
+ /**
11
+ * A simple type for storing 3D coordinates.
12
+ *
13
+ * @category Number:Common
14
+ */
15
+ export type Coords3d = {
16
+ x: number;
17
+ y: number;
18
+ z: number;
19
+ };
@@ -0,0 +1 @@
1
+ export type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A simple type for storing 2D dimensions.
3
+ *
4
+ * @category Number:Common
5
+ */
6
+ export type Dimensions = {
7
+ width: number;
8
+ height: number;
9
+ };
10
+ /**
11
+ * A simple type for storing 3D dimensions.
12
+ *
13
+ * @category Number:Common
14
+ */
15
+ export type Dimensions3d = {
16
+ width: number;
17
+ height: number;
18
+ depth: number;
19
+ };
@@ -0,0 +1,11 @@
1
+ export type MinMax = {
2
+ min: number;
3
+ max: number;
4
+ };
5
+ /**
6
+ * Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
7
+ * that is not the case, the returned value is the given min and max values swapped.
8
+ *
9
+ * @category Number:Common
10
+ */
11
+ export declare function ensureMinMax({ min, max }: MinMax): MinMax;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
3
+ * that is not the case, the returned value is the given min and max values swapped.
4
+ *
5
+ * @category Number:Common
6
+ */
7
+ export function ensureMinMax({ min, max }) {
8
+ if (min > max) {
9
+ return { min: max, max: min };
10
+ }
11
+ else {
12
+ return { min, max };
13
+ }
14
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Converts the input into a number and returns `NaN` if the conversion fails. This handles more
3
+ * edge cases than just plain `Number(input)`.
4
+ *
5
+ * @category Number:Common
6
+ * @returns The converted number or `NaN`.
7
+ */
8
+ export declare function toNumber(input: unknown): number;
9
+ /**
10
+ * Converts the input into a number and throws an error if the conversion fails.
11
+ *
12
+ * @category Number:Common
13
+ * @returns The converted number
14
+ * @throws `TypeError` if the conversion resulted in `NaN`
15
+ */
16
+ export declare function toEnsuredNumber(input: unknown): number;
17
+ /**
18
+ * Converts the input into a number and returns `undefined` if the conversion fails.
19
+ *
20
+ * @category Number:Common
21
+ * @returns The converted number or `undefined`.
22
+ */
23
+ export declare function toMaybeNumber(input: unknown): number | undefined;
@@ -0,0 +1,50 @@
1
+ import { removeCommas } from '../string/commas.js';
2
+ /**
3
+ * Converts the input into a number and returns `NaN` if the conversion fails. This handles more
4
+ * edge cases than just plain `Number(input)`.
5
+ *
6
+ * @category Number:Common
7
+ * @returns The converted number or `NaN`.
8
+ */
9
+ export function toNumber(input) {
10
+ if (typeof input === 'number') {
11
+ return input;
12
+ }
13
+ else if (typeof input === 'string') {
14
+ return Number(removeCommas(input));
15
+ }
16
+ else {
17
+ return Number(input);
18
+ }
19
+ }
20
+ /**
21
+ * Converts the input into a number and throws an error if the conversion fails.
22
+ *
23
+ * @category Number:Common
24
+ * @returns The converted number
25
+ * @throws `TypeError` if the conversion resulted in `NaN`
26
+ */
27
+ export function toEnsuredNumber(input) {
28
+ const maybeNumber = toMaybeNumber(input);
29
+ if (maybeNumber == undefined) {
30
+ throw new TypeError(`Cannot convert to a number: ${String(input)}`);
31
+ }
32
+ else {
33
+ return maybeNumber;
34
+ }
35
+ }
36
+ /**
37
+ * Converts the input into a number and returns `undefined` if the conversion fails.
38
+ *
39
+ * @category Number:Common
40
+ * @returns The converted number or `undefined`.
41
+ */
42
+ export function toMaybeNumber(input) {
43
+ const numeric = toNumber(input);
44
+ if (isNaN(numeric)) {
45
+ return undefined;
46
+ }
47
+ else {
48
+ return numeric;
49
+ }
50
+ }
@@ -0,0 +1,3 @@
1
+ export declare function round(value: number, { digits }: {
2
+ digits: number;
3
+ }): number;
@@ -0,0 +1,5 @@
1
+ export function round(value, { digits }) {
2
+ const digitFactor = Math.pow(10, digits);
3
+ const multiplied = value * digitFactor;
4
+ return Number((Math.round(multiplied) / digitFactor).toFixed(digits));
5
+ }
@@ -0,0 +1 @@
1
+ export declare function doesRequireScientificNotation(input: number): boolean;
@@ -0,0 +1,3 @@
1
+ export function doesRequireScientificNotation(input) {
2
+ return String(input).includes('e');
3
+ }
@@ -1,6 +1,8 @@
1
- import { addCommasToNumber, convertIntoNumber, doesRequireScientificNotation } from './common-number';
2
- import { typedSplit } from './common-string';
3
- import { safeMatch } from './regexp';
1
+ import { safeMatch } from '../regexp/safe-match.js';
2
+ import { addCommasToNumber } from '../string/commas.js';
3
+ import { safeSplit } from '../string/split.js';
4
+ import { toNumber } from './number-conversion.js';
5
+ import { doesRequireScientificNotation } from './scientific.js';
4
6
  const defaultTruncationSuffixes = [
5
7
  'k', // thousand
6
8
  'M', // million
@@ -32,23 +34,22 @@ function combineBeforeAndAfterDot({ beforeDot, afterDot = '', maxLength, }) {
32
34
  return beforeDot;
33
35
  }
34
36
  function truncateBigNumber(numberAsString, suffixes, maxLength) {
35
- const [beforeDot, afterDot,] = typedSplit(numberAsString, '.');
37
+ const [beforeDot, afterDot,] = safeSplit(numberAsString, '.');
36
38
  const withCommas = addCommasToNumber(beforeDot);
37
39
  const truncationDepth = safeMatch(withCommas, /,/g).length;
38
40
  const suffix = suffixes[truncationDepth - 1];
39
- const [beforeComma, afterComma,] = typedSplit(withCommas, ',');
41
+ const [beforeComma, afterComma,] = safeSplit(withCommas, ',');
40
42
  const trailing = [
41
43
  afterComma,
42
44
  afterDot,
43
45
  ].join('');
44
46
  if (beforeComma.length + 1 > maxLength) {
45
47
  // will look like 0.9M
46
- const minimumString = [
48
+ return [
47
49
  '0.',
48
50
  beforeComma[0],
49
51
  suffixes[truncationDepth],
50
52
  ].join('');
51
- return minimumString;
52
53
  }
53
54
  else {
54
55
  const combined = combineBeforeAndAfterDot({
@@ -65,15 +66,16 @@ function truncateBigNumber(numberAsString, suffixes, maxLength) {
65
66
  const minScientificNotationLength = '1e+'.length;
66
67
  function truncateScientificNotation({ input, maxLength, }) {
67
68
  const valueString = String(input);
68
- const [beforeExponent, rawExponent,] = typedSplit(valueString, 'e');
69
- const exponent = rawExponent.replace(/^[\-\+]/, '');
69
+ const [beforeExponent, rawExponent,] = safeSplit(valueString, 'e');
70
+ const exponent = rawExponent.replace(/^[-+]/, '');
71
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70
72
  const plusOrMinus = rawExponent[0];
71
73
  const eSuffix = [
72
74
  'e',
73
75
  plusOrMinus,
74
76
  exponent,
75
77
  ].join('');
76
- const [beforeDot, afterDot,] = typedSplit(beforeExponent, '.');
78
+ const [beforeDot, afterDot,] = safeSplit(beforeExponent, '.');
77
79
  const minLength = exponent.length + minScientificNotationLength;
78
80
  if (minLength === maxLength) {
79
81
  // this will look like "4e+4" or "5e-234"
@@ -105,7 +107,7 @@ function truncateScientificNotation({ input, maxLength, }) {
105
107
  }
106
108
  }
107
109
  function handleSmallNumbers(numberAsString, maxLength) {
108
- const [beforeDot, afterDot,] = typedSplit(addCommasToNumber(numberAsString), '.');
110
+ const [beforeDot, afterDot,] = safeSplit(addCommasToNumber(numberAsString), '.');
109
111
  if (beforeDot.length <= maxLength) {
110
112
  return combineBeforeAndAfterDot({
111
113
  beforeDot,
@@ -132,7 +134,7 @@ function handleSmallNumbers(numberAsString, maxLength) {
132
134
  * 'Y', // yotta- septillion
133
135
  */
134
136
  export function truncateNumber(originalValue, { customSuffixes = defaultTruncationSuffixes, maxLength = 6, } = {}) {
135
- const inputNumber = convertIntoNumber(originalValue);
137
+ const inputNumber = toNumber(originalValue);
136
138
  // handle edge cases
137
139
  if (isNaN(inputNumber) || inputNumber === Infinity) {
138
140
  return String(inputNumber);
@@ -0,0 +1,13 @@
1
+ import { MinMax } from './min-max.js';
2
+ /**
3
+ * If the given value is outside the given min/max bounds, instead of clamping the number (as the
4
+ * `clamp` function does), this function wraps the value around to the next bound.
5
+ *
6
+ * @category Number:Common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * wrapNumber({min: 0, max: 100, value: 101}); // 0
11
+ * ```
12
+ */
13
+ export declare function wrapNumber(value: number, minMax: MinMax): number;
@@ -0,0 +1,22 @@
1
+ import { ensureMinMax } from './min-max.js';
2
+ /**
3
+ * If the given value is outside the given min/max bounds, instead of clamping the number (as the
4
+ * `clamp` function does), this function wraps the value around to the next bound.
5
+ *
6
+ * @category Number:Common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * wrapNumber({min: 0, max: 100, value: 101}); // 0
11
+ * ```
12
+ */
13
+ export function wrapNumber(value, minMax) {
14
+ const { min, max } = ensureMinMax(minMax);
15
+ if (value > max) {
16
+ return min;
17
+ }
18
+ else if (value < min) {
19
+ return max;
20
+ }
21
+ return value;
22
+ }