@augment-vir/common 30.0.0 → 30.0.2
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.
- package/README.md +11 -0
- package/dist/augments/array/array-map.d.ts +29 -1
- package/dist/augments/array/array-map.js +29 -1
- package/dist/augments/array/array-to-object.d.ts +27 -9
- package/dist/augments/array/array-to-object.js +96 -12
- package/dist/augments/array/awaited/awaited-filter.d.ts +29 -0
- package/dist/augments/array/awaited/awaited-filter.js +29 -0
- package/dist/augments/array/awaited/awaited-for-each.d.ts +28 -3
- package/dist/augments/array/awaited/awaited-for-each.js +28 -3
- package/dist/augments/array/awaited/awaited-map.d.ts +32 -0
- package/dist/augments/array/awaited/awaited-map.js +32 -0
- package/dist/augments/array/filter.d.ts +29 -0
- package/dist/augments/array/filter.js +61 -0
- package/dist/augments/array/remove-duplicates.d.ts +60 -1
- package/dist/augments/array/remove-duplicates.js +61 -2
- package/dist/augments/array/repeat-array.d.ts +22 -0
- package/dist/augments/array/repeat-array.js +22 -0
- package/dist/augments/array/shuffle-array.d.ts +8 -1
- package/dist/augments/array/shuffle-array.js +8 -1
- package/dist/augments/array/string-array.d.ts +8 -0
- package/dist/augments/array/string-array.js +8 -0
- package/dist/augments/enum/enum-value-check.d.ts +30 -0
- package/dist/augments/enum/enum-value-check.js +30 -0
- package/dist/augments/error/combine-errors.d.ts +24 -3
- package/dist/augments/error/combine-errors.js +26 -4
- package/dist/augments/function/call-asynchronously.d.ts +19 -0
- package/dist/augments/function/call-asynchronously.js +19 -0
- package/dist/augments/function/call-with-retries.d.ts +23 -3
- package/dist/augments/function/call-with-retries.js +23 -0
- package/dist/augments/function/debounce.d.ts +71 -4
- package/dist/augments/function/debounce.js +66 -4
- package/dist/augments/function/execution-duration.d.ts +17 -5
- package/dist/augments/function/execution-duration.js +16 -3
- package/dist/augments/function/if-truthy.d.ts +28 -0
- package/dist/augments/function/if-truthy.js +35 -0
- package/dist/augments/function/wrap-in-try.d.ts +12 -4
- package/dist/augments/function/wrap-in-try.js +64 -0
- package/dist/augments/json/append-json.d.ts +3 -3
- package/dist/augments/json/append-json.js +33 -0
- package/dist/augments/json/copy-through-json.d.ts +32 -1
- package/dist/augments/json/copy-through-json.js +32 -1
- package/dist/augments/json/json-compatible.d.ts +37 -9
- package/dist/augments/json/jsonify.d.ts +30 -1
- package/dist/augments/json/jsonify.js +31 -2
- package/dist/augments/log/log-colors.d.ts +41 -0
- package/dist/augments/log/log-colors.js +43 -2
- package/dist/augments/log/log-string.d.ts +21 -0
- package/dist/augments/log/log-string.js +9 -2
- package/dist/augments/log/log-writer.d.ts +22 -0
- package/dist/augments/log/log.d.ts +44 -1
- package/dist/augments/log/log.js +46 -3
- package/dist/augments/log/logger.d.ts +49 -0
- package/dist/augments/log/logger.js +21 -0
- package/dist/augments/number/clamp.d.ts +13 -2
- package/dist/augments/number/clamp.js +12 -1
- package/dist/augments/number/coords.d.ts +6 -2
- package/dist/augments/number/digit.d.ts +7 -0
- package/dist/augments/number/dimensions.d.ts +6 -2
- package/dist/augments/number/min-max.d.ts +9 -2
- package/dist/augments/number/min-max.js +4 -2
- package/dist/augments/number/number-conversion.d.ts +9 -3
- package/dist/augments/number/number-conversion.js +10 -4
- package/dist/augments/number/round.d.ts +19 -0
- package/dist/augments/number/round.js +19 -0
- package/dist/augments/number/scientific.d.ts +18 -1
- package/dist/augments/number/scientific.js +18 -1
- package/dist/augments/number/truncate-number.d.ts +24 -3
- package/dist/augments/number/truncate-number.js +28 -8
- package/dist/augments/number/wrap-number.d.ts +9 -3
- package/dist/augments/number/wrap-number.js +8 -2
- package/dist/augments/object/diff.d.ts +27 -5
- package/dist/augments/object/diff.js +20 -4
- package/dist/augments/object/empty.d.ts +7 -1
- package/dist/augments/object/get-or-set.d.ts +6 -27
- package/dist/augments/object/get-or-set.js +66 -4
- package/dist/augments/object/key-count.d.ts +4 -0
- package/dist/augments/object/map-entries.js +23 -0
- package/dist/augments/object/map-enum.d.ts +7 -0
- package/dist/augments/object/map-enum.js +24 -0
- package/dist/augments/object/map-values.d.ts +25 -13
- package/dist/augments/object/map-values.js +29 -21
- package/dist/augments/object/merge-deep.d.ts +4 -0
- package/dist/augments/object/merge-deep.js +4 -0
- package/dist/augments/object/merge-defined-properties.d.ts +13 -0
- package/dist/augments/object/merge-defined-properties.js +13 -0
- package/dist/augments/object/merge-property-arrays.d.ts +28 -0
- package/dist/augments/object/merge-property-arrays.js +28 -0
- package/dist/augments/object/object-entries.d.ts +27 -0
- package/dist/augments/object/object-entries.js +27 -0
- package/dist/augments/object/object-filter.d.ts +20 -0
- package/dist/augments/object/object-filter.js +20 -0
- package/dist/augments/object/object-keys.d.ts +32 -0
- package/dist/augments/object/object-keys.js +32 -0
- package/dist/augments/object/object-values.d.ts +9 -0
- package/dist/augments/object/object-values.js +9 -0
- package/dist/augments/path/esm-path.d.ts +26 -0
- package/dist/augments/path/esm-path.js +31 -0
- package/dist/augments/prisma/base-prisma-types.d.ts +38 -0
- package/dist/augments/prisma/prisma-basic-model.d.ts +29 -0
- package/dist/augments/prisma/prisma-basic-model.js +1 -0
- package/dist/augments/prisma/prisma-full-model.d.ts +37 -0
- package/dist/augments/prisma/prisma-full-model.js +1 -0
- package/dist/augments/prisma/prisma-model-create.d.ts +159 -0
- package/dist/augments/prisma/prisma-model-create.js +58 -0
- package/dist/augments/prisma/prisma-model-name.d.ts +18 -0
- package/dist/augments/prisma/prisma-model-name.js +1 -0
- package/dist/augments/promise/timed-promise.d.ts +15 -0
- package/dist/augments/promise/timed-promise.js +15 -0
- package/dist/augments/random/random-boolean.d.ts +6 -0
- package/dist/augments/random/random-boolean.js +6 -0
- package/dist/augments/random/random-integer.d.ts +3 -1
- package/dist/augments/random/random-integer.js +3 -1
- package/dist/augments/random/random-string.d.ts +6 -2
- package/dist/augments/random/random-string.js +6 -2
- package/dist/augments/regexp/match.d.ts +10 -0
- package/dist/augments/regexp/match.js +13 -0
- package/dist/augments/regexp/regexp-flags.d.ts +56 -3
- package/dist/augments/regexp/regexp-flags.js +71 -14
- package/dist/augments/regexp/regexp-string.d.ts +4 -0
- package/dist/augments/regexp/regexp-string.js +5 -1
- package/dist/augments/selection-set/select-collapsed.d.ts +46 -3
- package/dist/augments/selection-set/select-collapsed.js +40 -2
- package/dist/augments/selection-set/select-from.d.ts +47 -2
- package/dist/augments/selection-set/select-from.js +48 -1
- package/dist/augments/selection-set/selection-set.d.ts +26 -7
- package/dist/augments/selection-set/selection-set.js +1 -4
- package/dist/augments/string/casing/capitalization.d.ts +15 -1
- package/dist/augments/string/casing/capitalization.js +14 -0
- package/dist/augments/string/casing/casing.d.ts +53 -7
- package/dist/augments/string/casing/casing.js +36 -11
- package/dist/augments/string/casing/kebab-and-camel.d.ts +14 -0
- package/dist/augments/string/casing/kebab-and-camel.js +20 -5
- package/dist/augments/string/comma.d.ts +26 -0
- package/dist/augments/string/{commas.js → comma.js} +21 -2
- package/dist/augments/string/join.d.ts +15 -4
- package/dist/augments/string/join.js +15 -4
- package/dist/augments/string/prefix.d.ts +6 -0
- package/dist/augments/string/prefix.js +4 -0
- package/dist/augments/string/remove-duplicate-characters.d.ts +17 -0
- package/dist/augments/string/remove-duplicate-characters.js +22 -0
- package/dist/augments/string/replace.d.ts +17 -0
- package/dist/augments/string/replace.js +17 -0
- package/dist/augments/string/split.d.ts +29 -2
- package/dist/augments/string/split.js +32 -7
- package/dist/augments/string/substring-index.d.ts +11 -5
- package/dist/augments/string/substring-index.js +12 -3
- package/dist/augments/string/suffix.d.ts +22 -0
- package/dist/augments/string/suffix.js +16 -0
- package/dist/augments/string/white-space.d.ts +8 -1
- package/dist/augments/string/white-space.js +8 -1
- package/dist/augments/string/wrap-string.d.ts +15 -0
- package/dist/augments/string/wrap-string.js +15 -0
- package/dist/augments/type/ensure-type.d.ts +4 -0
- package/dist/augments/type/ensure-type.js +4 -0
- package/dist/augments/type/readonly.d.ts +7 -0
- package/dist/augments/type/readonly.js +7 -0
- package/dist/augments/type/type-recursion.d.ts +93 -1
- package/dist/augments/type/union.d.ts +1 -0
- package/dist/augments/type/union.js +1 -0
- package/dist/augments/type/void-type.d.ts +6 -2
- package/dist/augments/type/writable.d.ts +7 -0
- package/dist/augments/type/writable.js +7 -0
- package/dist/index.d.ts +11 -5
- package/dist/index.js +11 -5
- package/package.json +19 -9
- package/dist/augments/boolean/if-truthy.d.ts +0 -2
- package/dist/augments/boolean/if-truthy.js +0 -9
- package/dist/augments/file/esm-path.d.ts +0 -4
- package/dist/augments/file/esm-path.js +0 -8
- package/dist/augments/prisma/prisma-models.d.ts +0 -106
- package/dist/augments/regexp/safe-match.d.ts +0 -1
- package/dist/augments/regexp/safe-match.js +0 -4
- package/dist/augments/string/commas.d.ts +0 -7
- /package/dist/augments/prisma/{prisma-models.js → base-prisma-types.js} +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Falsy, type Truthy } from '@augment-vir/assert';
|
|
2
|
+
/**
|
|
3
|
+
* Checks an input for truthiness then calls the respective callback, returning the callback's
|
|
4
|
+
* output.
|
|
5
|
+
*
|
|
6
|
+
* @category Function
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {ifTruthy} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* const result1 = ifTruthy(
|
|
14
|
+
* true,
|
|
15
|
+
* () => 1,
|
|
16
|
+
* () => 2,
|
|
17
|
+
* ); // result1 is `1`
|
|
18
|
+
* const result2 = ifTruthy(
|
|
19
|
+
* false,
|
|
20
|
+
* () => 1,
|
|
21
|
+
* () => 2,
|
|
22
|
+
* ); // result2 is `2`
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @returns The called callback's output.
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
28
|
+
export declare function ifTruthy<const InputType, IfTruthyType, IfFalsyType>(checkThis: InputType, ifTruthyCallback: (truthyInput: Truthy<InputType>) => IfTruthyType, ifFalsyCallback: (truthyInput: Falsy<InputType>) => IfFalsyType): IfTruthyType | IfFalsyType;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
/**
|
|
3
|
+
* Checks an input for truthiness then calls the respective callback, returning the callback's
|
|
4
|
+
* output.
|
|
5
|
+
*
|
|
6
|
+
* @category Function
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {ifTruthy} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* const result1 = ifTruthy(
|
|
14
|
+
* true,
|
|
15
|
+
* () => 1,
|
|
16
|
+
* () => 2,
|
|
17
|
+
* ); // result1 is `1`
|
|
18
|
+
* const result2 = ifTruthy(
|
|
19
|
+
* false,
|
|
20
|
+
* () => 1,
|
|
21
|
+
* () => 2,
|
|
22
|
+
* ); // result2 is `2`
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @returns The called callback's output.
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
28
|
+
export function ifTruthy(checkThis, ifTruthyCallback, ifFalsyCallback) {
|
|
29
|
+
if (check.isTruthy(checkThis)) {
|
|
30
|
+
return ifTruthyCallback(checkThis);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return ifFalsyCallback(checkThis);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { type NoInputsFunction, type PartialWithUndefined } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link wrapInTry}.
|
|
4
|
+
*
|
|
5
|
+
* @category Function
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export type WrapInTryOptions<FallbackValue> = PartialWithUndefined<{
|
|
3
10
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
11
|
+
* Call this function if the callback passed to {@link wrapInTry} throws an error. The thrown
|
|
12
|
+
* error is passed to this function. If a `fallbackValue` option is also provided, it will be
|
|
13
|
+
* ignored.
|
|
6
14
|
*/
|
|
7
15
|
handleError: (error: unknown) => FallbackValue;
|
|
8
16
|
/**
|
|
9
|
-
* Fallback to this value if the callback passed to
|
|
10
|
-
*
|
|
17
|
+
* Fallback to this value if the callback passed to {@link wrapInTry} throws an error. This will
|
|
18
|
+
* be ignored if a `handleError` option is also set.
|
|
11
19
|
*/
|
|
12
20
|
fallbackValue: FallbackValue;
|
|
13
21
|
}>;
|
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
import { check } from '@augment-vir/assert';
|
|
2
2
|
import { ensureError, } from '@augment-vir/core';
|
|
3
|
+
/**
|
|
4
|
+
* Calls the callback and returns its output. If the callback throws an error, it is handled in the
|
|
5
|
+
* following ways:
|
|
6
|
+
*
|
|
7
|
+
* - If a `handleError` function is provided in `options`, it is passed the thrown error. The output
|
|
8
|
+
* of `handleError` is returned by `wrapInTry`.
|
|
9
|
+
* - If a `fallbackValue` is provided, it is returned by `wrapInTry`. The thrown error is ignored.
|
|
10
|
+
* - If no options are provided, the thrown error is returned by `wrapInTry`.
|
|
11
|
+
*
|
|
12
|
+
* @category Function
|
|
13
|
+
* @category Package : @augment-vir/common
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import {wrapInTry} from '@augment-vir/common';
|
|
18
|
+
*
|
|
19
|
+
* // `result1` will be `'success'`.
|
|
20
|
+
* const result1 = wrapInTry(
|
|
21
|
+
* () => {
|
|
22
|
+
* return 'success';
|
|
23
|
+
* },
|
|
24
|
+
* {
|
|
25
|
+
* fallbackValue: 'failure',
|
|
26
|
+
* },
|
|
27
|
+
* );
|
|
28
|
+
* // `result2` will be `'failure'`.
|
|
29
|
+
* const result2 = wrapInTry(
|
|
30
|
+
* () => {
|
|
31
|
+
* throw new Error();
|
|
32
|
+
* return 'success';
|
|
33
|
+
* },
|
|
34
|
+
* {
|
|
35
|
+
* fallbackValue: 'failure',
|
|
36
|
+
* },
|
|
37
|
+
* );
|
|
38
|
+
* // `result3` will be `'failure also'`.
|
|
39
|
+
* const result3 = wrapInTry(
|
|
40
|
+
* () => {
|
|
41
|
+
* throw new Error();
|
|
42
|
+
* return 'success';
|
|
43
|
+
* },
|
|
44
|
+
* {
|
|
45
|
+
* handleError() {
|
|
46
|
+
* return 'failure also';
|
|
47
|
+
* },
|
|
48
|
+
* },
|
|
49
|
+
* );
|
|
50
|
+
* // `result4` will be `'failure also'`.
|
|
51
|
+
* const result4 = wrapInTry(
|
|
52
|
+
* () => {
|
|
53
|
+
* throw new Error();
|
|
54
|
+
* return 'success';
|
|
55
|
+
* },
|
|
56
|
+
* {
|
|
57
|
+
* handleError() {
|
|
58
|
+
* return 'failure also';
|
|
59
|
+
* },
|
|
60
|
+
* fallbackValue: 'ignored',
|
|
61
|
+
* },
|
|
62
|
+
* );
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
66
|
+
*/
|
|
3
67
|
export function wrapInTry(callback, options = {}) {
|
|
4
68
|
try {
|
|
5
69
|
const value = callback();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { JsonCompatibleArray, JsonCompatibleObject, JsonCompatibleValue } from './json-compatible.js';
|
|
2
|
-
|
|
3
|
-
export declare function appendJson(...entries: ReadonlyArray<
|
|
4
|
-
export declare function appendJson(...entries: ReadonlyArray<JsonCompatibleObject | JsonCompatibleArray | undefined>): JsonCompatibleObject
|
|
2
|
+
import { JsonCompatiblePrimitive } from './json-compatible.js';
|
|
3
|
+
export declare function appendJson(entry: JsonCompatibleArray | JsonCompatiblePrimitive, ...entries: ReadonlyArray<JsonCompatibleValue | undefined>): JsonCompatibleArray;
|
|
4
|
+
export declare function appendJson(entry: JsonCompatibleObject, ...entries: ReadonlyArray<JsonCompatibleObject | JsonCompatibleArray | undefined>): JsonCompatibleObject;
|
|
5
5
|
export declare function appendJson(...entries: ReadonlyArray<JsonCompatibleValue | undefined>): JsonCompatibleObject | JsonCompatibleArray;
|
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
import { check } from '@augment-vir/assert';
|
|
2
2
|
import { copyThroughJson } from './copy-through-json.js';
|
|
3
|
+
/**
|
|
4
|
+
* Appends all provided JSON values together. `undefined` values will be ignored. The first value
|
|
5
|
+
* determines whether the output will be an object or an array. Any value appended to an array will
|
|
6
|
+
* work just fine, but primitives append to an object will likely behave unexpectedly. Arrays
|
|
7
|
+
* appended to arrays will be flattened (but only by one level).
|
|
8
|
+
*
|
|
9
|
+
* @category JSON : Common
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {appendJson} from '@augment-vir/common';
|
|
15
|
+
*
|
|
16
|
+
* // `result1` will be `{a: 'q', b: 'b'}`
|
|
17
|
+
* const result1 = appendJson({a: 'a'}, {b: 'b'}, {a: 'q'});
|
|
18
|
+
* // `result2` will be `[{a: 'a'}, {b: 'b'}, {a: 'q'}, 'r']`
|
|
19
|
+
* const result2 = appendJson([{a: 'a'}], {b: 'b'}, {a: 'q'}, 'r');
|
|
20
|
+
* // `result3` will be `['a', ['b', 'c'], 'd', 'e']`
|
|
21
|
+
* const result3 = appendJson(
|
|
22
|
+
* ['a'],
|
|
23
|
+
* [
|
|
24
|
+
* [
|
|
25
|
+
* 'b',
|
|
26
|
+
* 'c',
|
|
27
|
+
* ],
|
|
28
|
+
* ],
|
|
29
|
+
* ['d'],
|
|
30
|
+
* 'e',
|
|
31
|
+
* );
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
35
|
+
*/
|
|
3
36
|
export function appendJson(...rawEntries) {
|
|
4
37
|
const entries = rawEntries.filter(check.isTruthy);
|
|
5
38
|
if (!check.isLengthAtLeast(entries, 1)) {
|
|
@@ -1,3 +1,34 @@
|
|
|
1
1
|
import { Jsonify, Writable } from 'type-fest';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Deeply copy an object through JSON. This is the fastest deep copy, but the input must already be
|
|
4
|
+
* JSON serializable otherwise the copy will not match the original.
|
|
5
|
+
*
|
|
6
|
+
* @category JSON : Common
|
|
7
|
+
* @category Copy
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {copyThroughJson} from '@augment-vir/common';
|
|
13
|
+
*
|
|
14
|
+
* // `copy1` will be `{a: 'a', b: 'b'}`
|
|
15
|
+
* const copy1 = copyThroughJson({a: 'a', b: 'b'});
|
|
16
|
+
* // `copy2` will be `{map: {}, b: 'b'}`
|
|
17
|
+
* const copy2 = copyThroughJson({
|
|
18
|
+
* map: new Map([
|
|
19
|
+
* [
|
|
20
|
+
* 'q',
|
|
21
|
+
* 'r',
|
|
22
|
+
* ],
|
|
23
|
+
* [
|
|
24
|
+
* 's',
|
|
25
|
+
* 't',
|
|
26
|
+
* ],
|
|
27
|
+
* ]),
|
|
28
|
+
* b: 'b',
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
33
|
+
*/
|
|
3
34
|
export declare function copyThroughJson<const T>(input: T): Writable<Jsonify<T>>;
|
|
@@ -1,4 +1,35 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Deeply copy an object through JSON. This is the fastest deep copy, but the input must already be
|
|
3
|
+
* JSON serializable otherwise the copy will not match the original.
|
|
4
|
+
*
|
|
5
|
+
* @category JSON : Common
|
|
6
|
+
* @category Copy
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {copyThroughJson} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* // `copy1` will be `{a: 'a', b: 'b'}`
|
|
14
|
+
* const copy1 = copyThroughJson({a: 'a', b: 'b'});
|
|
15
|
+
* // `copy2` will be `{map: {}, b: 'b'}`
|
|
16
|
+
* const copy2 = copyThroughJson({
|
|
17
|
+
* map: new Map([
|
|
18
|
+
* [
|
|
19
|
+
* 'q',
|
|
20
|
+
* 'r',
|
|
21
|
+
* ],
|
|
22
|
+
* [
|
|
23
|
+
* 's',
|
|
24
|
+
* 't',
|
|
25
|
+
* ],
|
|
26
|
+
* ]),
|
|
27
|
+
* b: 'b',
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
32
|
+
*/
|
|
2
33
|
export function copyThroughJson(input) {
|
|
3
34
|
try {
|
|
4
35
|
return JSON.parse(JSON.stringify(input));
|
|
@@ -1,22 +1,50 @@
|
|
|
1
1
|
import { Jsonify, Primitive } from 'type-fest';
|
|
2
2
|
/**
|
|
3
3
|
* These are similar in purpose, name, and structure to type-fest's JsonValue types but these are
|
|
4
|
-
* permissive. The goal here is to allow any types that do not get serialized into just empty
|
|
4
|
+
* more permissive. The goal here is to allow any types that do not get serialized into just empty
|
|
5
5
|
* objects. (For example, JSON.stringify(new Map()) returns "{}", so we don't want to allow that
|
|
6
6
|
* type.)
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* All primitives that are allowed in JSON.
|
|
10
|
+
*
|
|
11
|
+
* Note that while `undefined` is allowed here, it will be behave slightly differently than the
|
|
12
|
+
* others.
|
|
13
|
+
*
|
|
14
|
+
* - `JSON.stringify(undefined)` will output `undefined`, not a string.
|
|
15
|
+
* - `JSON.stringify({a: null, b: undefined})` will output `'{"a": null}'`, omitting the `b` key
|
|
16
|
+
* entirely.
|
|
17
|
+
*
|
|
18
|
+
* @category JSON : Common
|
|
19
|
+
* @category Package : @augment-vir/common
|
|
20
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
21
|
+
*/
|
|
22
|
+
export type JsonCompatiblePrimitive = Jsonify<Primitive> | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* An object that only contains JSON compatible values.
|
|
25
|
+
*
|
|
26
|
+
* @category JSON : Common
|
|
27
|
+
* @category Package : @augment-vir/common
|
|
28
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
29
|
+
*/
|
|
9
30
|
export type JsonCompatibleObject = Partial<{
|
|
10
31
|
readonly [key: string | number]: JsonCompatibleValue | Readonly<JsonCompatibleValue>;
|
|
11
32
|
}> | Partial<{
|
|
12
33
|
[key: string | number]: JsonCompatibleValue | Readonly<JsonCompatibleValue>;
|
|
13
34
|
}>;
|
|
14
|
-
export type JsonCompatibleArray = JsonCompatibleValue[]
|
|
15
35
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
36
|
+
* An array that only contains JSON compatible values.
|
|
37
|
+
*
|
|
38
|
+
* @category JSON : Common
|
|
39
|
+
* @category Package : @augment-vir/common
|
|
40
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
41
|
+
*/
|
|
42
|
+
export type JsonCompatibleArray = JsonCompatibleValue[] | ReadonlyArray<JsonCompatibleValue>;
|
|
43
|
+
/**
|
|
44
|
+
* Any JSON compatible value.
|
|
45
|
+
*
|
|
46
|
+
* @category JSON : Common
|
|
47
|
+
* @category Package : @augment-vir/common
|
|
48
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
49
|
*/
|
|
19
|
-
|
|
|
20
|
-
readonly [P in number]: JsonCompatibleValue;
|
|
21
|
-
} & ReadonlyArray<any>);
|
|
22
|
-
export type JsonCompatibleValue = JsonCompatiblePrimitiveValue | JsonCompatibleObject | JsonCompatibleArray;
|
|
50
|
+
export type JsonCompatibleValue = JsonCompatiblePrimitive | JsonCompatibleObject | JsonCompatibleArray;
|
|
@@ -1,2 +1,31 @@
|
|
|
1
1
|
import { Jsonify } from 'type-fest';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates a JSON compatible version of the value given. Under the hood this is actually the same as
|
|
4
|
+
* {@link copyThroughJson}.
|
|
5
|
+
*
|
|
6
|
+
* @category JSON : Common
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {jsonify} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* // `result` is `{b: 'b'}`
|
|
14
|
+
* const result = jsonify({
|
|
15
|
+
* map: new Map([
|
|
16
|
+
* [
|
|
17
|
+
* 'q',
|
|
18
|
+
* 'r',
|
|
19
|
+
* ],
|
|
20
|
+
* [
|
|
21
|
+
* 's',
|
|
22
|
+
* 't',
|
|
23
|
+
* ],
|
|
24
|
+
* ]),
|
|
25
|
+
* b: 'b',
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
30
|
+
*/
|
|
31
|
+
export declare function jsonify<T>(value: T): Jsonify<T>;
|
|
@@ -1,4 +1,33 @@
|
|
|
1
1
|
import JSON5 from 'json5';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates a JSON compatible version of the value given. Under the hood this is actually the same as
|
|
4
|
+
* {@link copyThroughJson}.
|
|
5
|
+
*
|
|
6
|
+
* @category JSON : Common
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {jsonify} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* // `result` is `{b: 'b'}`
|
|
14
|
+
* const result = jsonify({
|
|
15
|
+
* map: new Map([
|
|
16
|
+
* [
|
|
17
|
+
* 'q',
|
|
18
|
+
* 'r',
|
|
19
|
+
* ],
|
|
20
|
+
* [
|
|
21
|
+
* 's',
|
|
22
|
+
* 't',
|
|
23
|
+
* ],
|
|
24
|
+
* ]),
|
|
25
|
+
* b: 'b',
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
30
|
+
*/
|
|
31
|
+
export function jsonify(value) {
|
|
32
|
+
return JSON5.parse(JSON5.stringify(value));
|
|
4
33
|
}
|
|
@@ -1,9 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported log output types.
|
|
3
|
+
*
|
|
4
|
+
* @category Log : Util
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export declare enum LogOutputType {
|
|
9
|
+
/** Logged to stdout if the current environment supports it, or just `console.log`. */
|
|
2
10
|
Standard = "stdout",
|
|
11
|
+
/** Logged to stderr if the current environment supports it, or just `console.error`. */
|
|
3
12
|
Error = "stderr"
|
|
4
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Standardized color keys for logging. If you want to use customized colors, use
|
|
16
|
+
* [ansi-styles](https://www.npmjs.com/package/ansi-styles) in Node.js or [custom
|
|
17
|
+
* CSS](https://developer.mozilla.org/docs/Web/API/console#styling_console_output) in browsers.
|
|
18
|
+
*
|
|
19
|
+
* @category Log : Util
|
|
20
|
+
* @category Package : @augment-vir/common
|
|
21
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
22
|
+
*/
|
|
5
23
|
export declare enum LogColorKey {
|
|
6
24
|
Bold = "bold",
|
|
25
|
+
Debug = "debug",
|
|
7
26
|
Error = "error",
|
|
8
27
|
Faint = "faint",
|
|
9
28
|
Info = "info",
|
|
@@ -14,10 +33,32 @@ export declare enum LogColorKey {
|
|
|
14
33
|
Success = "success",
|
|
15
34
|
Warning = "warning"
|
|
16
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Configuration for creating a logger. This is not required, as a default configuration is built-in
|
|
38
|
+
* already.
|
|
39
|
+
*
|
|
40
|
+
* @category Log : Util
|
|
41
|
+
* @category Package : @augment-vir/common
|
|
42
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
43
|
+
*/
|
|
17
44
|
export type LogColorConfig = Readonly<Record<LogColorKey, {
|
|
18
45
|
/** Either an array of CSS property assignments */
|
|
19
46
|
colors: string[];
|
|
20
47
|
logType: LogOutputType;
|
|
21
48
|
}>>;
|
|
49
|
+
/**
|
|
50
|
+
* Mapping of color keys to the current color string.
|
|
51
|
+
*
|
|
52
|
+
* @category Log : Util
|
|
53
|
+
* @category Package : @augment-vir/common
|
|
54
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
55
|
+
*/
|
|
22
56
|
export declare const logColors: Readonly<Record<LogColorKey, string>>;
|
|
57
|
+
/**
|
|
58
|
+
* Default implementation of {@link LogColorConfig}.
|
|
59
|
+
*
|
|
60
|
+
* @category Log : Util
|
|
61
|
+
* @category Package : @augment-vir/common
|
|
62
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
63
|
+
*/
|
|
23
64
|
export declare const defaultLogColorConfig: LogColorConfig;
|
|
@@ -1,12 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { perEnv, RuntimeEnv } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Supported log output types.
|
|
4
|
+
*
|
|
5
|
+
* @category Log : Util
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export var LogOutputType;
|
|
3
10
|
(function (LogOutputType) {
|
|
11
|
+
/** Logged to stdout if the current environment supports it, or just `console.log`. */
|
|
4
12
|
LogOutputType["Standard"] = "stdout";
|
|
13
|
+
/** Logged to stderr if the current environment supports it, or just `console.error`. */
|
|
5
14
|
LogOutputType["Error"] = "stderr";
|
|
6
15
|
})(LogOutputType || (LogOutputType = {}));
|
|
16
|
+
/**
|
|
17
|
+
* Standardized color keys for logging. If you want to use customized colors, use
|
|
18
|
+
* [ansi-styles](https://www.npmjs.com/package/ansi-styles) in Node.js or [custom
|
|
19
|
+
* CSS](https://developer.mozilla.org/docs/Web/API/console#styling_console_output) in browsers.
|
|
20
|
+
*
|
|
21
|
+
* @category Log : Util
|
|
22
|
+
* @category Package : @augment-vir/common
|
|
23
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
24
|
+
*/
|
|
7
25
|
export var LogColorKey;
|
|
8
26
|
(function (LogColorKey) {
|
|
9
27
|
LogColorKey["Bold"] = "bold";
|
|
28
|
+
LogColorKey["Debug"] = "debug";
|
|
10
29
|
LogColorKey["Error"] = "error";
|
|
11
30
|
LogColorKey["Faint"] = "faint";
|
|
12
31
|
LogColorKey["Info"] = "info";
|
|
@@ -18,13 +37,14 @@ export var LogColorKey;
|
|
|
18
37
|
LogColorKey["Warning"] = "warning";
|
|
19
38
|
})(LogColorKey || (LogColorKey = {}));
|
|
20
39
|
async function determineDefaultLogColors() {
|
|
21
|
-
return await
|
|
40
|
+
return await perEnv({
|
|
22
41
|
/** We calculate coverage in web, so the node code will never run in coverage tests. */
|
|
23
42
|
/* node:coverage disable */
|
|
24
43
|
async [RuntimeEnv.Node]() {
|
|
25
44
|
const styles = (await import('ansi-styles')).default;
|
|
26
45
|
return {
|
|
27
46
|
[LogColorKey.Bold]: styles.bold.open,
|
|
47
|
+
[LogColorKey.Debug]: styles.blueBright.open,
|
|
28
48
|
[LogColorKey.Error]: styles.red.open,
|
|
29
49
|
[LogColorKey.Faint]: styles.gray.open,
|
|
30
50
|
[LogColorKey.Info]: styles.cyan.open,
|
|
@@ -40,6 +60,7 @@ async function determineDefaultLogColors() {
|
|
|
40
60
|
[RuntimeEnv.Web]() {
|
|
41
61
|
return Promise.resolve({
|
|
42
62
|
[LogColorKey.Bold]: 'font-weight: bold',
|
|
63
|
+
[LogColorKey.Debug]: 'color: blue',
|
|
43
64
|
[LogColorKey.Error]: 'color: red',
|
|
44
65
|
[LogColorKey.Faint]: 'color: grey',
|
|
45
66
|
[LogColorKey.Info]: 'color: teal',
|
|
@@ -53,7 +74,21 @@ async function determineDefaultLogColors() {
|
|
|
53
74
|
},
|
|
54
75
|
});
|
|
55
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Mapping of color keys to the current color string.
|
|
79
|
+
*
|
|
80
|
+
* @category Log : Util
|
|
81
|
+
* @category Package : @augment-vir/common
|
|
82
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
83
|
+
*/
|
|
56
84
|
export const logColors = await determineDefaultLogColors();
|
|
85
|
+
/**
|
|
86
|
+
* Default implementation of {@link LogColorConfig}.
|
|
87
|
+
*
|
|
88
|
+
* @category Log : Util
|
|
89
|
+
* @category Package : @augment-vir/common
|
|
90
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
91
|
+
*/
|
|
57
92
|
export const defaultLogColorConfig = {
|
|
58
93
|
[LogColorKey.Bold]: {
|
|
59
94
|
colors: [
|
|
@@ -61,6 +96,12 @@ export const defaultLogColorConfig = {
|
|
|
61
96
|
],
|
|
62
97
|
logType: LogOutputType.Standard,
|
|
63
98
|
},
|
|
99
|
+
[LogColorKey.Debug]: {
|
|
100
|
+
colors: [
|
|
101
|
+
logColors.debug,
|
|
102
|
+
],
|
|
103
|
+
logType: LogOutputType.Standard,
|
|
104
|
+
},
|
|
64
105
|
[LogColorKey.Faint]: {
|
|
65
106
|
colors: [
|
|
66
107
|
logColors.faint,
|
|
@@ -1,14 +1,35 @@
|
|
|
1
1
|
import { LogColorKey, type LogColorConfig } from './log-colors.js';
|
|
2
2
|
import { LogWriterParams } from './log-writer.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for a custom Logger.
|
|
5
|
+
*
|
|
6
|
+
* @category Log : Util
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
3
10
|
export type LoggerOptions = {
|
|
4
11
|
colorConfig: LogColorConfig;
|
|
5
12
|
omitColors: boolean;
|
|
6
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Parameters for {@link toLogString}.
|
|
16
|
+
*
|
|
17
|
+
* @category Log : Util
|
|
18
|
+
* @category Package : @augment-vir/common
|
|
19
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
20
|
+
*/
|
|
7
21
|
export type ToLogStringParams = {
|
|
8
22
|
colorKey: LogColorKey;
|
|
9
23
|
args: ReadonlyArray<any>;
|
|
10
24
|
options: Readonly<LoggerOptions>;
|
|
11
25
|
};
|
|
12
26
|
type ToLogString = (params: Readonly<ToLogStringParams>) => LogWriterParams;
|
|
27
|
+
/**
|
|
28
|
+
* Converts log arguments into a single {@link LogWriterParams}.
|
|
29
|
+
*
|
|
30
|
+
* @category Log : Util
|
|
31
|
+
* @category Package : @augment-vir/common
|
|
32
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
33
|
+
*/
|
|
13
34
|
export declare const toLogString: ToLogString;
|
|
14
35
|
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { check } from '@augment-vir/assert';
|
|
2
|
-
import {
|
|
2
|
+
import { perEnv, RuntimeEnv, stringify } from '@augment-vir/core';
|
|
3
3
|
import { filterMap } from '../array/filter.js';
|
|
4
4
|
import { removeSuffix } from '../string/suffix.js';
|
|
5
5
|
import { LogColorKey } from './log-colors.js';
|
|
6
6
|
async function createToLogString() {
|
|
7
|
-
return await
|
|
7
|
+
return await perEnv({
|
|
8
8
|
/** We calculate coverage in web, so the node code will never run in coverage tests. */
|
|
9
9
|
/* node:coverage disable */
|
|
10
10
|
async [RuntimeEnv.Node]() {
|
|
@@ -63,4 +63,11 @@ async function createToLogString() {
|
|
|
63
63
|
/* node:coverage enable */
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Converts log arguments into a single {@link LogWriterParams}.
|
|
68
|
+
*
|
|
69
|
+
* @category Log : Util
|
|
70
|
+
* @category Package : @augment-vir/common
|
|
71
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
72
|
+
*/
|
|
66
73
|
export const toLogString = await createToLogString();
|
|
@@ -1,8 +1,30 @@
|
|
|
1
1
|
import { type LogOutputType } from './log-colors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Params for {@link LogWriter}
|
|
4
|
+
*
|
|
5
|
+
* @category Log : Util
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export type LogWriterParams = {
|
|
3
10
|
text: string;
|
|
4
11
|
/** Typically this is only relevant in a browser console. */
|
|
5
12
|
css: string | undefined;
|
|
6
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* The final step in writing a log. This will actually perform the logging of text to the console.
|
|
16
|
+
* CSS will be applied if this is called within a browser.
|
|
17
|
+
*
|
|
18
|
+
* @category Log : Util
|
|
19
|
+
* @category Package : @augment-vir/common
|
|
20
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
21
|
+
*/
|
|
7
22
|
export type LogWriter = (params: Readonly<LogWriterParams>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* A log writer for each log output type.
|
|
25
|
+
*
|
|
26
|
+
* @category Log : Util
|
|
27
|
+
* @category Package : @augment-vir/common
|
|
28
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
29
|
+
*/
|
|
8
30
|
export type LogWriters = Record<LogOutputType, LogWriter>;
|