@augment-vir/common 31.30.1 → 31.32.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.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Wraps the input in an array if it isn't already an array.
3
+ *
4
+ * @category Array
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export declare function ensureArray<T>(input: T | T[]): T[];
9
+ /**
10
+ * Wraps the input in an array if it isn't already an array.
11
+ *
12
+ * @category Array
13
+ * @category Package : @augment-vir/common
14
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
15
+ */
16
+ export declare function ensureArray<T>(input: T | ReadonlyArray<T>): ReadonlyArray<T>;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Wraps the input in an array if it isn't already an array.
3
+ *
4
+ * @category Array
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export function ensureArray(input) {
9
+ if (Array.isArray(input)) {
10
+ return input;
11
+ }
12
+ else {
13
+ return [input];
14
+ }
15
+ }
@@ -1,3 +1,6 @@
1
+ import { type PartialWithUndefined } from '@augment-vir/common';
2
+ import { type AtLeastOneDuration } from '@date-vir/duration';
3
+ import { type IsEqual } from 'type-fest';
1
4
  /**
2
5
  * Calls `callback` until it doesn't throw an error or throws an error when `maxRetries` is reached.
3
6
  * Similar to the `waitUntil` guard from '@augment-vir/assert' but doesn't check the callback's
@@ -21,4 +24,11 @@
21
24
  *
22
25
  * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
23
26
  */
24
- export declare function callWithRetries<const T>(maxRetries: number, callback: () => T): T;
27
+ export declare function retry<const T, const Duration extends AtLeastOneDuration | undefined = undefined>(maxRetries: number, callback: () => T, options?: PartialWithUndefined<{
28
+ /**
29
+ * Wait this duration between each retry.
30
+ *
31
+ * @default {seconds: 1}
32
+ */
33
+ interval: Duration;
34
+ }>): IsEqual<Duration, undefined> extends true ? T : Promise<Awaited<T>>;
@@ -1,4 +1,4 @@
1
- import { ensureErrorAndPrependMessage } from '@augment-vir/core';
1
+ import { ensureErrorAndPrependMessage, wait } from '@augment-vir/core';
2
2
  /**
3
3
  * Calls `callback` until it doesn't throw an error or throws an error when `maxRetries` is reached.
4
4
  * Similar to the `waitUntil` guard from '@augment-vir/assert' but doesn't check the callback's
@@ -22,16 +22,19 @@ import { ensureErrorAndPrependMessage } from '@augment-vir/core';
22
22
  *
23
23
  * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
24
24
  */
25
- export function callWithRetries(maxRetries, callback) {
25
+ export function retry(maxRetries, callback, options = {}) {
26
26
  try {
27
27
  const result = callback();
28
28
  if (result instanceof Promise) {
29
- return result.catch((error) => {
29
+ return result.catch(async (error) => {
30
30
  if (maxRetries <= 1) {
31
31
  throw ensureErrorAndPrependMessage(error, 'Retry max reached');
32
32
  }
33
33
  else {
34
- return callWithRetries(maxRetries - 1, callback);
34
+ if (options.interval) {
35
+ await wait(options.interval);
36
+ }
37
+ return retry(maxRetries - 1, callback, options);
35
38
  }
36
39
  });
37
40
  }
@@ -43,8 +46,11 @@ export function callWithRetries(maxRetries, callback) {
43
46
  if (maxRetries <= 1) {
44
47
  throw ensureErrorAndPrependMessage(error, 'Retry max reached');
45
48
  }
49
+ else if (options.interval) {
50
+ return wait(options.interval).then(() => retry(maxRetries - 1, callback, options));
51
+ }
46
52
  else {
47
- return callWithRetries(maxRetries - 1, callback);
53
+ return retry(maxRetries - 1, callback, options);
48
54
  }
49
55
  }
50
56
  }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Logs each second as a countdown, then resolves.
3
+ *
4
+ * @category Log
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export declare function logCountdown(start: number, logCallback?: (value: string) => void): Promise<void>;
@@ -0,0 +1,19 @@
1
+ import { wait } from '@augment-vir/core';
2
+ import { log } from './log.js';
3
+ /**
4
+ * Logs each second as a countdown, then resolves.
5
+ *
6
+ * @category Log
7
+ * @category Package : @augment-vir/common
8
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
9
+ */
10
+ export async function logCountdown(start, logCallback = log.warning) {
11
+ logCallback(String(start));
12
+ await wait({ seconds: 1.5 });
13
+ if (start) {
14
+ return await logCountdown(start - 1, logCallback);
15
+ }
16
+ else {
17
+ return;
18
+ }
19
+ }
@@ -0,0 +1,12 @@
1
+ import { type Values } from '@augment-vir/core';
2
+ /**
3
+ * Race all the given values and return the one that finished first.
4
+ *
5
+ * @category Promise
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
9
+ export declare function racePromiseObject<const PromiseObject extends Readonly<Record<string, Promise<any>>>>(promises: PromiseObject): Promise<{
10
+ value: Awaited<Values<PromiseObject>>;
11
+ key: keyof PromiseObject;
12
+ }>;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Race all the given values and return the one that finished first.
3
+ *
4
+ * @category Promise
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export async function racePromiseObject(promises) {
9
+ return await Promise.race(Object.entries(promises).map(([key, value,]) => {
10
+ return value.then((value) => {
11
+ return {
12
+ value,
13
+ key,
14
+ };
15
+ });
16
+ }));
17
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Indent all lines in a string by 4 spaces * count.
3
+ *
4
+ * @category String
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export declare function indent(value: string, count?: number): string;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Indent all lines in a string by 4 spaces * count.
3
+ *
4
+ * @category String
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export function indent(value, count = 1) {
9
+ return value
10
+ .split('\n')
11
+ .map((line) => [
12
+ ' '.repeat(Math.round(count)),
13
+ line,
14
+ ].join(''))
15
+ .join('\n');
16
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A case insensitive match between strings.
3
+ *
4
+ * @category String
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export declare function match(haystack: string, needle: string): boolean;
@@ -0,0 +1,11 @@
1
+ import { escapeStringForRegExp } from '../regexp/regexp-string.js';
2
+ /**
3
+ * A case insensitive match between strings.
4
+ *
5
+ * @category String
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
9
+ export function match(haystack, needle) {
10
+ return !!needle && !!new RegExp(escapeStringForRegExp(needle), 'i').exec(haystack);
11
+ }
@@ -12,6 +12,7 @@ export function findSubstringIndexes({ searchIn, searchFor, caseSensitive, inclu
12
12
  const searchRegExp = addRegExpFlags(setRegExpCaseSensitivity(searchFor, { caseSensitive }), 'g');
13
13
  const indexes = [];
14
14
  const indexesAndLengths = [];
15
+ // eslint-disable-next-line sonarjs/no-ignored-return
15
16
  searchIn.replace(searchRegExp, (...matchResults) => {
16
17
  /**
17
18
  * Grabbing the second to last entry in the array (rather than the second) takes capture
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './augments/array/awaited/awaited-filter.js';
5
5
  export * from './augments/array/awaited/awaited-for-each.js';
6
6
  export * from './augments/array/awaited/awaited-map.js';
7
7
  export * from './augments/array/create-array.js';
8
+ export * from './augments/array/ensure-array.js';
8
9
  export * from './augments/array/extract-duplicates.js';
9
10
  export * from './augments/array/filter.js';
10
11
  export * from './augments/array/remove-duplicates.js';
@@ -15,16 +16,17 @@ export * from './augments/core-exports.js';
15
16
  export * from './augments/enum/enum-value-check.js';
16
17
  export * from './augments/error/combine-errors.js';
17
18
  export * from './augments/function/call-asynchronously.js';
18
- export * from './augments/function/call-with-retries.js';
19
19
  export * from './augments/function/debounce.js';
20
20
  export * from './augments/function/execution-duration.js';
21
21
  export * from './augments/function/if-truthy.js';
22
+ export * from './augments/function/retry.js';
22
23
  export * from './augments/function/wrap-in-try.js';
23
24
  export * from './augments/json/append-json.js';
24
25
  export * from './augments/json/copy-through-json.js';
25
26
  export * from './augments/json/json5.js';
26
27
  export * from './augments/json/jsonify.js';
27
28
  export * from './augments/log/log-colors.js';
29
+ export * from './augments/log/log-countdown.js';
28
30
  export * from './augments/log/log-string.js';
29
31
  export * from './augments/log/log-writer.js';
30
32
  export * from './augments/log/log.js';
@@ -62,6 +64,7 @@ export * from './augments/prisma/prisma-full-model.js';
62
64
  export * from './augments/prisma/prisma-model-create.js';
63
65
  export * from './augments/prisma/prisma-model-name.js';
64
66
  export * from './augments/promise/promise-object.js';
67
+ export * from './augments/promise/race-object.js';
65
68
  export * from './augments/promise/timed-promise.js';
66
69
  export * from './augments/random/random-boolean.js';
67
70
  export * from './augments/random/random-integer.js';
@@ -77,7 +80,9 @@ export * from './augments/string/casing/capitalization.js';
77
80
  export * from './augments/string/casing/casing.js';
78
81
  export * from './augments/string/casing/kebab-and-camel.js';
79
82
  export * from './augments/string/comma.js';
83
+ export * from './augments/string/indent.js';
80
84
  export * from './augments/string/join.js';
85
+ export * from './augments/string/match.js';
81
86
  export * from './augments/string/prefix.js';
82
87
  export * from './augments/string/remove-duplicate-characters.js';
83
88
  export * from './augments/string/replace.js';
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export * from './augments/array/awaited/awaited-filter.js';
5
5
  export * from './augments/array/awaited/awaited-for-each.js';
6
6
  export * from './augments/array/awaited/awaited-map.js';
7
7
  export * from './augments/array/create-array.js';
8
+ export * from './augments/array/ensure-array.js';
8
9
  export * from './augments/array/extract-duplicates.js';
9
10
  export * from './augments/array/filter.js';
10
11
  export * from './augments/array/remove-duplicates.js';
@@ -15,16 +16,17 @@ export * from './augments/core-exports.js';
15
16
  export * from './augments/enum/enum-value-check.js';
16
17
  export * from './augments/error/combine-errors.js';
17
18
  export * from './augments/function/call-asynchronously.js';
18
- export * from './augments/function/call-with-retries.js';
19
19
  export * from './augments/function/debounce.js';
20
20
  export * from './augments/function/execution-duration.js';
21
21
  export * from './augments/function/if-truthy.js';
22
+ export * from './augments/function/retry.js';
22
23
  export * from './augments/function/wrap-in-try.js';
23
24
  export * from './augments/json/append-json.js';
24
25
  export * from './augments/json/copy-through-json.js';
25
26
  export * from './augments/json/json5.js';
26
27
  export * from './augments/json/jsonify.js';
27
28
  export * from './augments/log/log-colors.js';
29
+ export * from './augments/log/log-countdown.js';
28
30
  export * from './augments/log/log-string.js';
29
31
  export * from './augments/log/log-writer.js';
30
32
  export * from './augments/log/log.js';
@@ -62,6 +64,7 @@ export * from './augments/prisma/prisma-full-model.js';
62
64
  export * from './augments/prisma/prisma-model-create.js';
63
65
  export * from './augments/prisma/prisma-model-name.js';
64
66
  export * from './augments/promise/promise-object.js';
67
+ export * from './augments/promise/race-object.js';
65
68
  export * from './augments/promise/timed-promise.js';
66
69
  export * from './augments/random/random-boolean.js';
67
70
  export * from './augments/random/random-integer.js';
@@ -77,7 +80,9 @@ export * from './augments/string/casing/capitalization.js';
77
80
  export * from './augments/string/casing/casing.js';
78
81
  export * from './augments/string/casing/kebab-and-camel.js';
79
82
  export * from './augments/string/comma.js';
83
+ export * from './augments/string/indent.js';
80
84
  export * from './augments/string/join.js';
85
+ export * from './augments/string/match.js';
81
86
  export * from './augments/string/prefix.js';
82
87
  export * from './augments/string/remove-duplicate-characters.js';
83
88
  export * from './augments/string/replace.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "31.30.1",
3
+ "version": "31.32.0",
4
4
  "description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
5
5
  "keywords": [
6
6
  "augment",
@@ -40,9 +40,9 @@
40
40
  "test:web": "virmator --no-deps test web"
41
41
  },
42
42
  "dependencies": {
43
- "@augment-vir/assert": "^31.30.1",
44
- "@augment-vir/core": "^31.30.1",
45
- "@date-vir/duration": "^7.3.2",
43
+ "@augment-vir/assert": "^31.32.0",
44
+ "@augment-vir/core": "^31.32.0",
45
+ "@date-vir/duration": "^7.4.0",
46
46
  "ansi-styles": "^6.2.1",
47
47
  "deepcopy-esm": "^2.1.1",
48
48
  "json5": "^2.2.3",
@@ -58,7 +58,7 @@
58
58
  "concurrently": "^9.2.0",
59
59
  "execute-in-browser": "^1.0.8",
60
60
  "istanbul-smart-text-reporter": "^1.1.5",
61
- "typescript": "^5.8.3"
61
+ "typescript": "^5.9.2"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=22"