@augment-vir/common 30.0.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 (164) hide show
  1. package/README.md +11 -0
  2. package/dist/augments/array/array-map.d.ts +29 -1
  3. package/dist/augments/array/array-map.js +29 -1
  4. package/dist/augments/array/array-to-object.d.ts +41 -5
  5. package/dist/augments/array/array-to-object.js +41 -5
  6. package/dist/augments/array/awaited/awaited-filter.d.ts +29 -0
  7. package/dist/augments/array/awaited/awaited-filter.js +29 -0
  8. package/dist/augments/array/awaited/awaited-for-each.d.ts +28 -3
  9. package/dist/augments/array/awaited/awaited-for-each.js +28 -3
  10. package/dist/augments/array/awaited/awaited-map.d.ts +32 -0
  11. package/dist/augments/array/awaited/awaited-map.js +32 -0
  12. package/dist/augments/array/filter.d.ts +29 -0
  13. package/dist/augments/array/filter.js +61 -0
  14. package/dist/augments/array/remove-duplicates.d.ts +60 -1
  15. package/dist/augments/array/remove-duplicates.js +61 -2
  16. package/dist/augments/array/repeat-array.d.ts +22 -0
  17. package/dist/augments/array/repeat-array.js +22 -0
  18. package/dist/augments/array/shuffle-array.d.ts +8 -1
  19. package/dist/augments/array/shuffle-array.js +8 -1
  20. package/dist/augments/array/string-array.d.ts +8 -0
  21. package/dist/augments/array/string-array.js +8 -0
  22. package/dist/augments/enum/enum-value-check.d.ts +30 -0
  23. package/dist/augments/enum/enum-value-check.js +30 -0
  24. package/dist/augments/error/combine-errors.d.ts +24 -3
  25. package/dist/augments/error/combine-errors.js +26 -4
  26. package/dist/augments/function/call-asynchronously.d.ts +19 -0
  27. package/dist/augments/function/call-asynchronously.js +19 -0
  28. package/dist/augments/function/call-with-retries.d.ts +23 -3
  29. package/dist/augments/function/call-with-retries.js +23 -0
  30. package/dist/augments/function/debounce.d.ts +71 -4
  31. package/dist/augments/function/debounce.js +66 -4
  32. package/dist/augments/function/execution-duration.d.ts +17 -5
  33. package/dist/augments/function/execution-duration.js +16 -3
  34. package/dist/augments/function/if-truthy.d.ts +28 -0
  35. package/dist/augments/function/if-truthy.js +35 -0
  36. package/dist/augments/function/wrap-in-try.d.ts +12 -4
  37. package/dist/augments/function/wrap-in-try.js +64 -0
  38. package/dist/augments/json/append-json.d.ts +3 -3
  39. package/dist/augments/json/append-json.js +33 -0
  40. package/dist/augments/json/copy-through-json.d.ts +32 -1
  41. package/dist/augments/json/copy-through-json.js +32 -1
  42. package/dist/augments/json/json-compatible.d.ts +37 -9
  43. package/dist/augments/json/jsonify.d.ts +30 -1
  44. package/dist/augments/json/jsonify.js +31 -2
  45. package/dist/augments/log/log-colors.d.ts +41 -0
  46. package/dist/augments/log/log-colors.js +43 -2
  47. package/dist/augments/log/log-string.d.ts +21 -0
  48. package/dist/augments/log/log-string.js +9 -2
  49. package/dist/augments/log/log-writer.d.ts +22 -0
  50. package/dist/augments/log/log.d.ts +44 -1
  51. package/dist/augments/log/log.js +46 -3
  52. package/dist/augments/log/logger.d.ts +49 -0
  53. package/dist/augments/log/logger.js +21 -0
  54. package/dist/augments/number/clamp.d.ts +13 -2
  55. package/dist/augments/number/clamp.js +12 -1
  56. package/dist/augments/number/coords.d.ts +6 -2
  57. package/dist/augments/number/digit.d.ts +7 -0
  58. package/dist/augments/number/dimensions.d.ts +6 -2
  59. package/dist/augments/number/min-max.d.ts +9 -2
  60. package/dist/augments/number/min-max.js +4 -2
  61. package/dist/augments/number/number-conversion.d.ts +9 -3
  62. package/dist/augments/number/number-conversion.js +10 -4
  63. package/dist/augments/number/round.d.ts +19 -0
  64. package/dist/augments/number/round.js +19 -0
  65. package/dist/augments/number/scientific.d.ts +18 -1
  66. package/dist/augments/number/scientific.js +18 -1
  67. package/dist/augments/number/truncate-number.d.ts +24 -3
  68. package/dist/augments/number/truncate-number.js +28 -8
  69. package/dist/augments/number/wrap-number.d.ts +9 -3
  70. package/dist/augments/number/wrap-number.js +8 -2
  71. package/dist/augments/object/diff.d.ts +27 -5
  72. package/dist/augments/object/diff.js +20 -4
  73. package/dist/augments/object/empty.d.ts +7 -1
  74. package/dist/augments/object/get-or-set.d.ts +6 -27
  75. package/dist/augments/object/get-or-set.js +66 -4
  76. package/dist/augments/object/key-count.d.ts +4 -0
  77. package/dist/augments/object/map-entries.js +23 -0
  78. package/dist/augments/object/map-enum.d.ts +7 -0
  79. package/dist/augments/object/map-enum.js +24 -0
  80. package/dist/augments/object/map-values.d.ts +25 -13
  81. package/dist/augments/object/map-values.js +29 -21
  82. package/dist/augments/object/merge-deep.d.ts +4 -0
  83. package/dist/augments/object/merge-deep.js +4 -0
  84. package/dist/augments/object/merge-defined-properties.d.ts +13 -0
  85. package/dist/augments/object/merge-defined-properties.js +13 -0
  86. package/dist/augments/object/merge-property-arrays.d.ts +28 -0
  87. package/dist/augments/object/merge-property-arrays.js +28 -0
  88. package/dist/augments/object/object-entries.d.ts +27 -0
  89. package/dist/augments/object/object-entries.js +27 -0
  90. package/dist/augments/object/object-filter.d.ts +20 -0
  91. package/dist/augments/object/object-filter.js +20 -0
  92. package/dist/augments/object/object-keys.d.ts +32 -0
  93. package/dist/augments/object/object-keys.js +32 -0
  94. package/dist/augments/object/object-values.d.ts +9 -0
  95. package/dist/augments/object/object-values.js +9 -0
  96. package/dist/augments/path/esm-path.d.ts +26 -0
  97. package/dist/augments/path/esm-path.js +31 -0
  98. package/dist/augments/prisma/prisma-models.d.ts +40 -3
  99. package/dist/augments/promise/timed-promise.d.ts +15 -0
  100. package/dist/augments/promise/timed-promise.js +15 -0
  101. package/dist/augments/random/random-boolean.d.ts +6 -0
  102. package/dist/augments/random/random-boolean.js +6 -0
  103. package/dist/augments/random/random-integer.d.ts +3 -1
  104. package/dist/augments/random/random-integer.js +3 -1
  105. package/dist/augments/random/random-string.d.ts +6 -2
  106. package/dist/augments/random/random-string.js +6 -2
  107. package/dist/augments/regexp/match.d.ts +10 -0
  108. package/dist/augments/regexp/match.js +13 -0
  109. package/dist/augments/regexp/regexp-flags.d.ts +56 -3
  110. package/dist/augments/regexp/regexp-flags.js +71 -14
  111. package/dist/augments/regexp/regexp-string.d.ts +4 -0
  112. package/dist/augments/regexp/regexp-string.js +5 -1
  113. package/dist/augments/selection-set/select-collapsed.d.ts +46 -3
  114. package/dist/augments/selection-set/select-collapsed.js +40 -2
  115. package/dist/augments/selection-set/select-from.d.ts +47 -2
  116. package/dist/augments/selection-set/select-from.js +48 -1
  117. package/dist/augments/selection-set/selection-set.d.ts +26 -7
  118. package/dist/augments/selection-set/selection-set.js +1 -4
  119. package/dist/augments/string/casing/capitalization.d.ts +15 -1
  120. package/dist/augments/string/casing/capitalization.js +14 -0
  121. package/dist/augments/string/casing/casing.d.ts +53 -7
  122. package/dist/augments/string/casing/casing.js +36 -11
  123. package/dist/augments/string/casing/kebab-and-camel.d.ts +14 -0
  124. package/dist/augments/string/casing/kebab-and-camel.js +20 -5
  125. package/dist/augments/string/comma.d.ts +26 -0
  126. package/dist/augments/string/{commas.js → comma.js} +21 -2
  127. package/dist/augments/string/join.d.ts +15 -4
  128. package/dist/augments/string/join.js +15 -4
  129. package/dist/augments/string/prefix.d.ts +6 -0
  130. package/dist/augments/string/prefix.js +4 -0
  131. package/dist/augments/string/remove-duplicate-characters.d.ts +17 -0
  132. package/dist/augments/string/remove-duplicate-characters.js +22 -0
  133. package/dist/augments/string/replace.d.ts +17 -0
  134. package/dist/augments/string/replace.js +17 -0
  135. package/dist/augments/string/split.d.ts +29 -2
  136. package/dist/augments/string/split.js +32 -7
  137. package/dist/augments/string/substring-index.d.ts +11 -5
  138. package/dist/augments/string/substring-index.js +12 -3
  139. package/dist/augments/string/suffix.d.ts +22 -0
  140. package/dist/augments/string/suffix.js +16 -0
  141. package/dist/augments/string/white-space.d.ts +8 -1
  142. package/dist/augments/string/white-space.js +8 -1
  143. package/dist/augments/string/wrap-string.d.ts +15 -0
  144. package/dist/augments/string/wrap-string.js +15 -0
  145. package/dist/augments/type/ensure-type.d.ts +4 -0
  146. package/dist/augments/type/ensure-type.js +4 -0
  147. package/dist/augments/type/readonly.d.ts +7 -0
  148. package/dist/augments/type/readonly.js +7 -0
  149. package/dist/augments/type/type-recursion.d.ts +93 -1
  150. package/dist/augments/type/union.d.ts +1 -0
  151. package/dist/augments/type/union.js +1 -0
  152. package/dist/augments/type/void-type.d.ts +6 -2
  153. package/dist/augments/type/writable.d.ts +7 -0
  154. package/dist/augments/type/writable.js +7 -0
  155. package/dist/index.d.ts +6 -4
  156. package/dist/index.js +6 -4
  157. package/package.json +15 -5
  158. package/dist/augments/boolean/if-truthy.d.ts +0 -2
  159. package/dist/augments/boolean/if-truthy.js +0 -9
  160. package/dist/augments/file/esm-path.d.ts +0 -4
  161. package/dist/augments/file/esm-path.js +0 -8
  162. package/dist/augments/regexp/safe-match.d.ts +0 -1
  163. package/dist/augments/regexp/safe-match.js +0 -4
  164. package/dist/augments/string/commas.d.ts +0 -7
@@ -1,2 +1,34 @@
1
+ /**
2
+ * Same as the TypeScript built-in type `Omit` except that it works on actual runtime values.
3
+ *
4
+ * @category Object
5
+ * @category Package : @augment-vir/common
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {omitObjectKeys} from '@augment-vir/common';
10
+ *
11
+ * omitObjectKeys({a: 'a', b: 'b'}, ['a']);
12
+ * // output is `{b: 'b'}`
13
+ * ```
14
+ *
15
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
16
+ */
1
17
  export declare function omitObjectKeys<const ObjectGeneric, const KeyGeneric extends keyof ObjectGeneric>(inputObject: Readonly<ObjectGeneric>, omitTheseKeys: ReadonlyArray<KeyGeneric>): Omit<ObjectGeneric, KeyGeneric>;
18
+ /**
19
+ * Same as the TypeScript built-in type `Pick` except that it works on actual runtime values.
20
+ *
21
+ * @category Object
22
+ * @category Package : @augment-vir/common
23
+ * @example
24
+ *
25
+ * ```ts
26
+ * import {pickObjectKeys} from '@augment-vir/common';
27
+ *
28
+ * pickObjectKeys({a: 'a', b: 'b'}, ['a']);
29
+ * // output is `{a: 'a'}`
30
+ * ```
31
+ *
32
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
33
+ */
2
34
  export declare function pickObjectKeys<const ObjectGeneric, const KeyGeneric extends keyof ObjectGeneric>(inputObject: Readonly<ObjectGeneric>, pickTheseKeys: ReadonlyArray<KeyGeneric>): Pick<ObjectGeneric, KeyGeneric>;
@@ -1,9 +1,41 @@
1
1
  import { filterObject } from './object-filter.js';
2
+ /**
3
+ * Same as the TypeScript built-in type `Omit` except that it works on actual runtime values.
4
+ *
5
+ * @category Object
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {omitObjectKeys} from '@augment-vir/common';
11
+ *
12
+ * omitObjectKeys({a: 'a', b: 'b'}, ['a']);
13
+ * // output is `{b: 'b'}`
14
+ * ```
15
+ *
16
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
17
+ */
2
18
  export function omitObjectKeys(inputObject, omitTheseKeys) {
3
19
  return filterObject(inputObject, (currentKey) => {
4
20
  return !omitTheseKeys.includes(currentKey);
5
21
  });
6
22
  }
23
+ /**
24
+ * Same as the TypeScript built-in type `Pick` except that it works on actual runtime values.
25
+ *
26
+ * @category Object
27
+ * @category Package : @augment-vir/common
28
+ * @example
29
+ *
30
+ * ```ts
31
+ * import {pickObjectKeys} from '@augment-vir/common';
32
+ *
33
+ * pickObjectKeys({a: 'a', b: 'b'}, ['a']);
34
+ * // output is `{a: 'a'}`
35
+ * ```
36
+ *
37
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
38
+ */
7
39
  export function pickObjectKeys(inputObject, pickTheseKeys) {
8
40
  return filterObject(inputObject, (currentKey) => {
9
41
  return pickTheseKeys.includes(currentKey);
@@ -1,2 +1,11 @@
1
1
  import { CompleteRequire } from '@augment-vir/core';
2
+ /**
3
+ * Gets an object's values. This is the same as
4
+ * [`Object.values`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/values)
5
+ * except that it has better TypeScript types.
6
+ *
7
+ * @category Object
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
10
+ */
2
11
  export declare function getObjectTypedValues<ObjectGeneric>(input: ObjectGeneric): CompleteRequire<ObjectGeneric>[keyof CompleteRequire<ObjectGeneric>][];
@@ -1,4 +1,13 @@
1
1
  import { getObjectTypedKeys } from '@augment-vir/core';
2
+ /**
3
+ * Gets an object's values. This is the same as
4
+ * [`Object.values`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/values)
5
+ * except that it has better TypeScript types.
6
+ *
7
+ * @category Object
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
10
+ */
2
11
  export function getObjectTypedValues(input) {
3
12
  return getObjectTypedKeys(input).map((key) => input[key]);
4
13
  }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Creates the equivalent of CJS's
3
+ * [`__dirname`](https://nodejs.org/docs/latest/api/globals.html#__dirname) and
4
+ * [`__filename`](https://nodejs.org/docs/latest/api/globals.html#__filename) for ESM modules.
5
+ *
6
+ * This is the equivalent of
7
+ * [`import.meta.dirname`](https://nodejs.org/api/esm.html#importmetadirname) and
8
+ * [`import.meta.filename`](https://nodejs.org/api/esm.html#importmetafilename) added to Node.js
9
+ * v20.11.0 but is compatible with older versions of Node.js as well as browsers.
10
+ *
11
+ * @category Path : Common
12
+ * @category Package : @augment-vir/common
13
+ * @example
14
+ *
15
+ * ```ts
16
+ * import {getEsmPath} from '@augment-vir/common';
17
+ *
18
+ * const {filePath, dirPath} = getEsmPath(import.meta);
19
+ * ```
20
+ *
21
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
22
+ */
23
+ export declare function getEsmPath(importMeta: ImportMeta): {
24
+ filePath: string;
25
+ dirPath: string;
26
+ };
@@ -0,0 +1,31 @@
1
+ import { removeSuffix } from '../string/suffix.js';
2
+ /**
3
+ * Creates the equivalent of CJS's
4
+ * [`__dirname`](https://nodejs.org/docs/latest/api/globals.html#__dirname) and
5
+ * [`__filename`](https://nodejs.org/docs/latest/api/globals.html#__filename) for ESM modules.
6
+ *
7
+ * This is the equivalent of
8
+ * [`import.meta.dirname`](https://nodejs.org/api/esm.html#importmetadirname) and
9
+ * [`import.meta.filename`](https://nodejs.org/api/esm.html#importmetafilename) added to Node.js
10
+ * v20.11.0 but is compatible with older versions of Node.js as well as browsers.
11
+ *
12
+ * @category Path : Common
13
+ * @category Package : @augment-vir/common
14
+ * @example
15
+ *
16
+ * ```ts
17
+ * import {getEsmPath} from '@augment-vir/common';
18
+ *
19
+ * const {filePath, dirPath} = getEsmPath(import.meta);
20
+ * ```
21
+ *
22
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
23
+ */
24
+ export function getEsmPath(importMeta) {
25
+ const filePath = new URL('', importMeta.url).pathname;
26
+ const dirPath = removeSuffix({ value: new URL('.', importMeta.url).pathname, suffix: '/' });
27
+ return {
28
+ filePath,
29
+ dirPath,
30
+ };
31
+ }
@@ -2,25 +2,35 @@ import type { AnyFunction, AnyObject } from '@augment-vir/core';
2
2
  /**
3
3
  * Extracts all model names from a generated `Prisma` object.
4
4
  *
5
+ * @category Prisma : Common
6
+ * @category Package : @augment-vir/common
5
7
  * @example
6
8
  *
7
9
  * ```ts
8
10
  * import type {Prisma} from '@prisma/client';
11
+ * import type {ModelNameFromPrismaTypeMap} from '@augment-vir/common';
9
12
  *
10
13
  * function doThing(modelName: ModelNameFromPrismaTypeMap<Prisma.TypeMap>) {}
11
14
  * ```
15
+ *
16
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
12
17
  */
13
18
  export type ModelNameFromPrismaTypeMap<PrismaTypeMap extends BasePrismaTypeMap> = PrismaTypeMap['meta']['modelProps'];
14
19
  /**
15
20
  * Extracts all model names from a generated `PrismaClient`.
16
21
  *
22
+ * @category Prisma : Common
23
+ * @category Package : @augment-vir/common
17
24
  * @example
18
25
  *
19
26
  * ```ts
20
27
  * import type {PrismaClient} from '@prisma/client';
28
+ * import type {ModelNameFromPrismaClient} from '@augment-vir/common';
21
29
  *
22
30
  * function doThing(modelName: ModelNameFromPrismaClient<PrismaClient>) {}
23
31
  * ```
32
+ *
33
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
24
34
  */
25
35
  export type ModelNameFromPrismaClient<PrismaClient extends AnyObject> = keyof {
26
36
  [Model in Extract<keyof PrismaClient, string> as PrismaClient[Model] extends {
@@ -30,20 +40,29 @@ export type ModelNameFromPrismaClient<PrismaClient extends AnyObject> = keyof {
30
40
  /**
31
41
  * Extracts the creation data for a model from the given `PrismaClient` type.
32
42
  *
43
+ * @category Prisma : Common
44
+ * @category Package : @augment-vir/common
33
45
  * @example
34
46
  *
35
47
  * ```ts
36
48
  * import type {PrismaClient} from '@prisma/client';
49
+ * import type {ModelCreationEntry} from '@augment-vir/common';
37
50
  *
38
51
  * function doThing(entry: ModelCreationEntry<PrismaClient, 'User'>) {}
39
52
  * ```
53
+ *
54
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
40
55
  */
41
56
  export type PrismaModelCreationEntry<PrismaClient extends AnyObject, Model extends ModelNameFromPrismaClient<PrismaClient>> = NonNullable<Parameters<PrismaClient[Model]['create']>[0]> extends {
42
57
  data?: infer Data;
43
58
  } ? NonNullable<Data> : `ERROR: failed to infer creation entry for model '${Model}'`;
44
59
  /**
45
60
  * A base type for `Prisma.TypeMap` because Prisma doesn't give us one. This currently only includes
46
- * the properties that are used within `@augment-vir/prisma-node-js`.
61
+ * the properties that are used within `@augment-vir/common`.
62
+ *
63
+ * @category Prisma : Common
64
+ * @category Package : @augment-vir/common
65
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
47
66
  */
48
67
  export type BasePrismaTypeMap = {
49
68
  meta: {
@@ -59,6 +78,10 @@ export type BasePrismaTypeMap = {
59
78
  *
60
79
  * Note: this omits the `composites` property because I don't have any examples of what those
61
80
  * actually are.
81
+ *
82
+ * @category Prisma : Common
83
+ * @category Package : @augment-vir/common
84
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
62
85
  */
63
86
  export type BasePrismaPayload = {
64
87
  name: string;
@@ -68,32 +91,44 @@ export type BasePrismaPayload = {
68
91
  /**
69
92
  * A full model entry with all relations from the given Prisma type map and model name.
70
93
  *
94
+ * @category Prisma : Common
95
+ * @category Package : @augment-vir/common
71
96
  * @example
72
97
  *
73
98
  * ```ts
74
99
  * import type {Prisma} from '@prisma/client';
100
+ * import type {FullPrismaModel} from '@augment-vir/common';
75
101
  *
76
102
  * function doThing(fullModel: FullModel<Prisma.TypeMap, 'User'>) {}
77
103
  * ```
104
+ *
105
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
78
106
  */
79
107
  export type FullPrismaModel<PrismaTypeMap extends BasePrismaTypeMap, Model extends ModelNameFromPrismaTypeMap<PrismaTypeMap>> = ExpandPrismaTypeMapPayload<PrismaTypeMap['model'][Model]['payload']>;
80
108
  /**
81
109
  * A base model entry with only its immediate properties from the given Prisma type map and model
82
110
  * name.
83
111
  *
112
+ * @category Prisma : Common
113
+ * @category Package : @augment-vir/common
84
114
  * @example
85
115
  *
86
116
  * ```ts
87
117
  * import type {Prisma} from '@prisma/client';
118
+ * import type {BasePrismaModel} from '@augment-vir/common';
88
119
  *
89
120
  * function doThing(fullModel: BaseModel<Prisma.TypeMap, 'User'>) {}
90
121
  * ```
122
+ *
123
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
91
124
  */
92
125
  export type BasePrismaModel<PrismaTypeMap extends BasePrismaTypeMap, Model extends ModelNameFromPrismaTypeMap<PrismaTypeMap>> = PrismaTypeMap['model'][Model]['payload']['scalars'];
93
126
  /**
94
127
  * Expand a Prisma payload into its scalars and recursive relations.
95
128
  *
96
- * @category Internals
129
+ * @category Prisma : Common : Util
130
+ * @category Package : @augment-vir/common
131
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
97
132
  */
98
133
  export type ExpandPrismaTypeMapPayload<Payload extends BasePrismaPayload> = Payload['scalars'] & {
99
134
  [Key in keyof Payload['objects']]: ExpandPotentialArrayPrismaTypeMapPayload<NonNullable<Payload['objects'][Key]>> | (null extends Payload['objects'][Key] ? null : never);
@@ -101,6 +136,8 @@ export type ExpandPrismaTypeMapPayload<Payload extends BasePrismaPayload> = Payl
101
136
  /**
102
137
  * Expand a payload that might be inside of an array, keeping it inside of an array if it is.
103
138
  *
104
- * @category Internals
139
+ * @category Prisma : Common : Util
140
+ * @category Package : @augment-vir/common
141
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
105
142
  */
106
143
  export type ExpandPotentialArrayPrismaTypeMapPayload<PayloadWrapper extends BasePrismaPayload | BasePrismaPayload[]> = PayloadWrapper extends (infer ActualPayload extends BasePrismaPayload)[] ? ExpandPrismaTypeMapPayload<ActualPayload>[] : PayloadWrapper extends BasePrismaPayload ? ExpandPrismaTypeMapPayload<PayloadWrapper> : never;
@@ -1,7 +1,22 @@
1
1
  import { AnyDuration } from '@date-vir/duration';
2
+ /**
3
+ * An error thrown by {@link wrapPromiseInTimeout} when the timeout is reached.
4
+ *
5
+ * @category Promise
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
2
9
  export declare class PromiseTimeoutError extends Error {
3
10
  readonly duration: AnyDuration;
4
11
  readonly name = "PromiseTimeoutError";
5
12
  constructor(duration: AnyDuration, failureMessage?: string | undefined);
6
13
  }
14
+ /**
15
+ * Wraps an already-created Promise in a timeout, causing a rejection if the original Promise isn't
16
+ * resolved by then.
17
+ *
18
+ * @category Promise
19
+ * @category Package : @augment-vir/common
20
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
21
+ */
7
22
  export declare function wrapPromiseInTimeout<T>(duration: Readonly<AnyDuration>, originalPromise: PromiseLike<T>, failureMessage?: string | undefined): Promise<T>;
@@ -1,6 +1,13 @@
1
1
  import { check } from '@augment-vir/assert';
2
2
  import { ensureError } from '@augment-vir/core';
3
3
  import { convertDuration, DurationUnit } from '@date-vir/duration';
4
+ /**
5
+ * An error thrown by {@link wrapPromiseInTimeout} when the timeout is reached.
6
+ *
7
+ * @category Promise
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
10
+ */
4
11
  export class PromiseTimeoutError extends Error {
5
12
  duration;
6
13
  name = 'PromiseTimeoutError';
@@ -14,6 +21,14 @@ export class PromiseTimeoutError extends Error {
14
21
  this.duration = duration;
15
22
  }
16
23
  }
24
+ /**
25
+ * Wraps an already-created Promise in a timeout, causing a rejection if the original Promise isn't
26
+ * resolved by then.
27
+ *
28
+ * @category Promise
29
+ * @category Package : @augment-vir/common
30
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
31
+ */
17
32
  export function wrapPromiseInTimeout(duration, originalPromise, failureMessage) {
18
33
  const milliseconds = convertDuration(duration, DurationUnit.Milliseconds).milliseconds;
19
34
  return new Promise(async (resolve, reject) => {
@@ -6,13 +6,19 @@
6
6
  *
7
7
  * This function uses cryptographically secure randomness.
8
8
  *
9
+ * @category Random
10
+ * @category Package : @augment-vir/common
9
11
  * @example
10
12
  *
11
13
  * ```ts
14
+ * import {randomBoolean} from '@augment-vir/common';
15
+ *
12
16
  * randomBoolean(50); // 50% chance to return true
13
17
  * randomBoolean(0); // always false, 0% chance of being true
14
18
  * randomBoolean(100); // always true, 100% chance of being true
15
19
  * randomBoolean(59.67); // 59% chance of being true
16
20
  * ```
21
+ *
22
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
17
23
  */
18
24
  export declare function randomBoolean(percentLikelyToBeTrue?: number): boolean;
@@ -8,14 +8,20 @@ import { randomInteger } from './random-integer.js';
8
8
  *
9
9
  * This function uses cryptographically secure randomness.
10
10
  *
11
+ * @category Random
12
+ * @category Package : @augment-vir/common
11
13
  * @example
12
14
  *
13
15
  * ```ts
16
+ * import {randomBoolean} from '@augment-vir/common';
17
+ *
14
18
  * randomBoolean(50); // 50% chance to return true
15
19
  * randomBoolean(0); // always false, 0% chance of being true
16
20
  * randomBoolean(100); // always true, 100% chance of being true
17
21
  * randomBoolean(59.67); // 59% chance of being true
18
22
  * ```
23
+ *
24
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
19
25
  */
20
26
  export function randomBoolean(percentLikelyToBeTrue = 50) {
21
27
  return (randomInteger({ min: 0, max: 99 }) <
@@ -4,7 +4,9 @@
4
4
  *
5
5
  * This function uses cryptographically secure randomness.
6
6
  *
7
- * @category Random:Common
7
+ * @category Random
8
+ * @category Package : @augment-vir/common
9
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
10
  */
9
11
  export declare function randomInteger({ min: rawMin, max: rawMax }: {
10
12
  min: number;
@@ -5,7 +5,9 @@ import { ensureMinMax } from '../number/min-max.js';
5
5
  *
6
6
  * This function uses cryptographically secure randomness.
7
7
  *
8
- * @category Random:Common
8
+ * @category Random
9
+ * @category Package : @augment-vir/common
10
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
9
11
  */
10
12
  export function randomInteger({ min: rawMin, max: rawMax }) {
11
13
  const { min, max } = ensureMinMax({ min: Math.floor(rawMin), max: Math.floor(rawMax) });
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * All letters allowed in {@link randomString}.
3
3
  *
4
- * @category Random:Common
4
+ * @category Random : Util
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
5
7
  */
6
8
  export declare const allowedRandomStringLetters: ReadonlyArray<string>;
7
9
  /**
@@ -9,6 +11,8 @@ export declare const allowedRandomStringLetters: ReadonlyArray<string>;
9
11
  *
10
12
  * This function uses cryptographically secure randomness.
11
13
  *
12
- * @category Random:Common
14
+ * @category Random
15
+ * @category Package : @augment-vir/common
16
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
13
17
  */
14
18
  export declare function randomString(generatedStringLength?: number): string;
@@ -2,7 +2,9 @@ import { randomInteger } from './random-integer.js';
2
2
  /**
3
3
  * All letters allowed in {@link randomString}.
4
4
  *
5
- * @category Random:Common
5
+ * @category Random : Util
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
6
8
  */
7
9
  export const allowedRandomStringLetters = [
8
10
  'a',
@@ -47,7 +49,9 @@ export const allowedRandomStringLetters = [
47
49
  *
48
50
  * This function uses cryptographically secure randomness.
49
51
  *
50
- * @category Random:Common
52
+ * @category Random
53
+ * @category Package : @augment-vir/common
54
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
51
55
  */
52
56
  export function randomString(generatedStringLength = 16) {
53
57
  let stringBuilder = '';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Performs
3
+ * [`''.match`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/match)
4
+ * but falls back to an empty array if no match was found.
5
+ *
6
+ * @category RegExp
7
+ * @category Package : @augment-vir/common
8
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
9
+ */
10
+ export declare function safeMatch(input: string, regExp: RegExp): string[];
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Performs
3
+ * [`''.match`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/match)
4
+ * but falls back to an empty array if no match was found.
5
+ *
6
+ * @category RegExp
7
+ * @category Package : @augment-vir/common
8
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
9
+ */
10
+ export function safeMatch(input, regExp) {
11
+ const match = input.match(regExp);
12
+ return match ?? [];
13
+ }
@@ -1,3 +1,56 @@
1
- export declare function deDupeRegExFlags(flags: string): string;
2
- export declare function addRegExpFlags(originalRegExp: RegExp, flags: string): RegExp;
3
- export declare function makeCaseInsensitiveRegExp(searchForInput: string | RegExp, caseSensitive: boolean): RegExp;
1
+ /**
2
+ * Creates a new RegExp by adding the given `flags` to the original RegExp.
3
+ *
4
+ * @category RegExp
5
+ * @category Package : @augment-vir/common
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {addRegExpFlags} from '@augment-vir/common';
10
+ *
11
+ * addRegExpFlags(/a/i, 'gm');
12
+ * // output is `/a/igm`
13
+ * ```
14
+ *
15
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
16
+ */
17
+ export declare function addRegExpFlags(originalRegExpOrString: RegExp | string, flags: string): RegExp;
18
+ /**
19
+ * Creates a new RegExp with the given `flags`.
20
+ *
21
+ * @category RegExp
22
+ * @category Package : @augment-vir/common
23
+ * @example
24
+ *
25
+ * ```ts
26
+ * import {setRegExpFlags} from '@augment-vir/common';
27
+ *
28
+ * setRegExpFlags(/a/i, 'gm');
29
+ * // output is `/a/gm`
30
+ * ```
31
+ *
32
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
33
+ */
34
+ export declare function setRegExpFlags(originalRegExpOrString: RegExp | string, flags: string): RegExp;
35
+ /**
36
+ * Creates a new RegExp by adding or removing the case insensitivity flag `'i'`, based on the given
37
+ * `caseSensitive` input. The first input can also be a string and it will be converted into a
38
+ * RegExp.
39
+ *
40
+ * @category RegExp
41
+ * @category Package : @augment-vir/common
42
+ * @example
43
+ *
44
+ * ```ts
45
+ * import {setRegExpCaseSensitivity} from '@augment-vir/common';
46
+ *
47
+ * setRegExpCaseSensitivity(/abc/i, {caseSensitive: true}); // output is `/abc/`
48
+ * setRegExpCaseSensitivity(/abc/, {caseSensitive: false}); // output is `/abc/i`
49
+ * setRegExpCaseSensitivity('abc', {caseSensitive: true}); // output is `/abc/i`
50
+ * ```
51
+ *
52
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
53
+ */
54
+ export declare function setRegExpCaseSensitivity(originalRegExpOrString: string | RegExp, { caseSensitive }: {
55
+ caseSensitive: boolean;
56
+ }): RegExp;
@@ -1,18 +1,75 @@
1
+ import { removeDuplicateCharacters } from '../string/remove-duplicate-characters.js';
1
2
  import { escapeStringForRegExp } from './regexp-string.js';
2
- export function deDupeRegExFlags(flags) {
3
- const deDuped = new Set(Array.from(flags.toLowerCase()));
4
- return Array.from(deDuped).join('');
5
- }
6
- export function addRegExpFlags(originalRegExp, flags) {
7
- return new RegExp(originalRegExp.source, deDupeRegExFlags([
8
- originalRegExp.flags,
3
+ /**
4
+ * Creates a new RegExp by adding the given `flags` to the original RegExp.
5
+ *
6
+ * @category RegExp
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {addRegExpFlags} from '@augment-vir/common';
12
+ *
13
+ * addRegExpFlags(/a/i, 'gm');
14
+ * // output is `/a/igm`
15
+ * ```
16
+ *
17
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
18
+ */
19
+ export function addRegExpFlags(originalRegExpOrString, flags) {
20
+ const allFlags = removeDuplicateCharacters([
21
+ typeof originalRegExpOrString === 'string' ? '' : originalRegExpOrString.flags,
9
22
  flags,
10
- ].join('')));
23
+ ]
24
+ .join('')
25
+ .toLowerCase());
26
+ return setRegExpFlags(originalRegExpOrString, allFlags);
27
+ }
28
+ /**
29
+ * Creates a new RegExp with the given `flags`.
30
+ *
31
+ * @category RegExp
32
+ * @category Package : @augment-vir/common
33
+ * @example
34
+ *
35
+ * ```ts
36
+ * import {setRegExpFlags} from '@augment-vir/common';
37
+ *
38
+ * setRegExpFlags(/a/i, 'gm');
39
+ * // output is `/a/gm`
40
+ * ```
41
+ *
42
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
43
+ */
44
+ export function setRegExpFlags(originalRegExpOrString, flags) {
45
+ const allFlags = removeDuplicateCharacters(flags);
46
+ if (typeof originalRegExpOrString === 'string') {
47
+ return new RegExp(escapeStringForRegExp(originalRegExpOrString), allFlags);
48
+ }
49
+ else {
50
+ return new RegExp(originalRegExpOrString.source, allFlags);
51
+ }
11
52
  }
12
- export function makeCaseInsensitiveRegExp(searchForInput, caseSensitive) {
13
- const regExpFlags = `g${!caseSensitive && typeof searchForInput === 'string' ? 'i' : ''}`;
14
- const searchFor = searchForInput instanceof RegExp
15
- ? new RegExp(searchForInput.source, deDupeRegExFlags(`${searchForInput.flags}${regExpFlags}`))
16
- : new RegExp(escapeStringForRegExp(searchForInput), regExpFlags);
17
- return searchFor;
53
+ /**
54
+ * Creates a new RegExp by adding or removing the case insensitivity flag `'i'`, based on the given
55
+ * `caseSensitive` input. The first input can also be a string and it will be converted into a
56
+ * RegExp.
57
+ *
58
+ * @category RegExp
59
+ * @category Package : @augment-vir/common
60
+ * @example
61
+ *
62
+ * ```ts
63
+ * import {setRegExpCaseSensitivity} from '@augment-vir/common';
64
+ *
65
+ * setRegExpCaseSensitivity(/abc/i, {caseSensitive: true}); // output is `/abc/`
66
+ * setRegExpCaseSensitivity(/abc/, {caseSensitive: false}); // output is `/abc/i`
67
+ * setRegExpCaseSensitivity('abc', {caseSensitive: true}); // output is `/abc/i`
68
+ * ```
69
+ *
70
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
71
+ */
72
+ export function setRegExpCaseSensitivity(originalRegExpOrString, { caseSensitive }) {
73
+ const caseSensitivityFlag = caseSensitive ? '' : 'i';
74
+ return addRegExpFlags(originalRegExpOrString, caseSensitivityFlag);
18
75
  }
@@ -1,5 +1,9 @@
1
1
  /**
2
2
  * Escapes characters from the given string so that it can be used within a RegExp without being
3
3
  * parsed as RegExp syntax.
4
+ *
5
+ * @category RegExp
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
4
8
  */
5
9
  export declare function escapeStringForRegExp(input: string): string;
@@ -1,7 +1,11 @@
1
1
  /**
2
2
  * Escapes characters from the given string so that it can be used within a RegExp without being
3
3
  * parsed as RegExp syntax.
4
+ *
5
+ * @category RegExp
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
4
8
  */
5
9
  export function escapeStringForRegExp(input) {
6
- return input.replace(/[\^$\\.*+?()[\]{}|]/g, String.raw `\$&`);
10
+ return input.replaceAll(/[\^$\\.*+?()[\]{}|]/g, String.raw `\$&`);
7
11
  }