@augment-vir/common 29.3.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.
- package/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +7 -3
- package/dist/augments/array/array-map.d.ts +34 -0
- package/dist/augments/array/array-map.js +32 -0
- package/dist/augments/array/array-to-object.d.ts +53 -0
- package/dist/augments/array/array-to-object.js +67 -0
- package/dist/augments/array/awaited/awaited-filter.d.ts +36 -0
- package/dist/augments/array/awaited/awaited-filter.js +36 -0
- package/dist/augments/array/awaited/awaited-for-each.d.ts +31 -0
- package/dist/augments/array/awaited/awaited-for-each.js +34 -0
- package/dist/augments/array/awaited/awaited-map.d.ts +33 -0
- package/dist/augments/array/awaited/awaited-map.js +40 -0
- package/dist/augments/array/filter.d.ts +32 -0
- package/dist/augments/array/filter.js +74 -0
- package/dist/augments/array/remove-duplicates.d.ts +60 -0
- package/dist/augments/array/remove-duplicates.js +72 -0
- package/dist/augments/array/repeat-array.d.ts +23 -0
- package/dist/augments/array/repeat-array.js +25 -0
- package/dist/augments/array/shuffle-array.d.ts +9 -0
- package/dist/augments/array/shuffle-array.js +17 -0
- package/dist/augments/array/string-array.d.ts +9 -0
- package/dist/augments/array/string-array.js +11 -0
- package/dist/augments/core-exports.d.ts +1 -0
- package/dist/augments/core-exports.js +1 -0
- package/dist/augments/enum/enum-value-check.d.ts +32 -0
- package/dist/augments/enum/enum-value-check.js +34 -0
- package/dist/augments/error/combine-errors.d.ts +24 -0
- package/dist/augments/error/combine-errors.js +34 -0
- package/dist/augments/function/call-asynchronously.d.ts +25 -0
- package/dist/augments/function/call-asynchronously.js +26 -0
- package/dist/augments/function/call-with-retries.d.ts +24 -0
- package/dist/augments/function/call-with-retries.js +50 -0
- package/dist/augments/function/debounce.d.ts +89 -0
- package/dist/augments/function/debounce.js +112 -0
- package/dist/augments/function/execution-duration.d.ts +20 -0
- package/dist/augments/function/execution-duration.js +39 -0
- package/dist/augments/function/if-truthy.d.ts +28 -0
- package/dist/augments/function/if-truthy.js +35 -0
- package/dist/{types/augments → augments/function}/wrap-in-try.d.ts +14 -7
- package/dist/augments/function/wrap-in-try.js +98 -0
- package/dist/augments/json/append-json.d.ts +5 -0
- package/dist/augments/json/append-json.js +59 -0
- package/dist/augments/json/copy-through-json.d.ts +34 -0
- package/dist/augments/json/copy-through-json.js +42 -0
- package/dist/augments/json/json-compatible.d.ts +50 -0
- package/dist/augments/json/jsonify.d.ts +31 -0
- package/dist/augments/json/jsonify.js +33 -0
- package/dist/augments/log/log-colors.d.ts +64 -0
- package/dist/augments/log/log-colors.js +157 -0
- package/dist/augments/log/log-string.d.ts +35 -0
- package/dist/augments/log/log-string.js +73 -0
- package/dist/augments/log/log-writer.d.ts +30 -0
- package/dist/augments/log/log.d.ts +56 -0
- package/dist/augments/log/log.js +85 -0
- package/dist/augments/log/logger.d.ts +60 -0
- package/dist/augments/log/logger.js +59 -0
- package/dist/augments/number/clamp.d.ts +18 -0
- package/dist/augments/number/clamp.js +19 -0
- package/dist/augments/number/coords.d.ts +23 -0
- package/dist/augments/number/digit.d.ts +8 -0
- package/dist/augments/number/dimensions.d.ts +23 -0
- package/dist/augments/number/min-max.d.ts +18 -0
- package/dist/augments/number/min-max.js +16 -0
- package/dist/augments/number/number-conversion.d.ts +29 -0
- package/dist/augments/number/number-conversion.js +56 -0
- package/dist/augments/number/round.d.ts +22 -0
- package/dist/augments/number/round.js +24 -0
- package/dist/augments/number/scientific.d.ts +18 -0
- package/dist/augments/number/scientific.js +20 -0
- package/dist/augments/number/truncate-number.d.ts +40 -0
- package/dist/{esm/augments → augments/number}/truncate-number.js +39 -17
- package/dist/augments/number/wrap-number.d.ts +19 -0
- package/dist/augments/number/wrap-number.js +28 -0
- package/dist/augments/object/diff.d.ts +51 -0
- package/dist/augments/object/diff.js +149 -0
- package/dist/augments/object/empty.d.ts +9 -0
- package/dist/augments/object/get-or-set.d.ts +10 -0
- package/dist/augments/object/get-or-set.js +100 -0
- package/dist/augments/object/key-count.d.ts +21 -0
- package/dist/augments/object/map-entries.d.ts +13 -0
- package/dist/augments/object/map-entries.js +80 -0
- package/dist/augments/object/map-enum.d.ts +11 -0
- package/dist/augments/object/map-enum.js +45 -0
- package/dist/augments/object/map-values.d.ts +35 -0
- package/dist/augments/object/map-values.js +65 -0
- package/dist/{types/augments → augments}/object/merge-deep.d.ts +4 -0
- package/dist/{esm/augments → augments}/object/merge-deep.js +9 -6
- package/dist/augments/object/merge-defined-properties.d.ts +19 -0
- package/dist/augments/object/merge-defined-properties.js +32 -0
- package/dist/augments/object/merge-property-arrays.d.ts +29 -0
- package/dist/augments/object/merge-property-arrays.js +39 -0
- package/dist/augments/object/object-entries.d.ts +31 -0
- package/dist/augments/object/object-entries.js +40 -0
- package/dist/augments/object/object-filter.d.ts +22 -0
- package/dist/augments/object/object-filter.js +27 -0
- package/dist/augments/object/object-keys.d.ts +34 -0
- package/dist/augments/object/object-keys.js +43 -0
- package/dist/augments/object/object-values.d.ts +11 -0
- package/dist/augments/object/object-values.js +13 -0
- package/dist/augments/path/esm-path.d.ts +26 -0
- package/dist/augments/path/esm-path.js +31 -0
- package/dist/augments/prisma/prisma-models.d.ts +143 -0
- package/dist/augments/prisma/prisma-models.js +1 -0
- package/dist/augments/promise/timed-promise.d.ts +22 -0
- package/dist/augments/promise/timed-promise.js +51 -0
- package/dist/augments/random/random-boolean.d.ts +24 -0
- package/dist/augments/random/random-boolean.js +32 -0
- package/dist/augments/random/random-integer.d.ts +14 -0
- package/dist/augments/random/random-integer.js +34 -0
- package/dist/augments/random/random-string.d.ts +18 -0
- package/dist/augments/random/random-string.js +67 -0
- 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 -0
- package/dist/augments/regexp/regexp-flags.js +75 -0
- package/dist/augments/regexp/regexp-string.d.ts +9 -0
- package/dist/augments/regexp/regexp-string.js +11 -0
- package/dist/augments/selection-set/select-collapsed.d.ts +54 -0
- package/dist/augments/selection-set/select-collapsed.js +63 -0
- package/dist/augments/selection-set/select-from.d.ts +48 -0
- package/dist/augments/selection-set/select-from.js +72 -0
- package/dist/augments/selection-set/selection-set.d.ts +37 -0
- package/dist/augments/selection-set/selection-set.js +1 -0
- package/dist/augments/string/casing/capitalization.d.ts +17 -0
- package/dist/augments/string/casing/capitalization.js +25 -0
- package/dist/augments/string/casing/casing.d.ts +73 -0
- package/dist/augments/string/casing/casing.js +65 -0
- package/dist/augments/string/casing/kebab-and-camel.d.ts +17 -0
- package/dist/augments/string/casing/kebab-and-camel.js +57 -0
- package/dist/augments/string/comma.d.ts +26 -0
- package/dist/augments/string/comma.js +48 -0
- package/dist/augments/string/join.d.ts +21 -0
- package/dist/augments/string/join.js +34 -0
- package/dist/augments/string/prefix.d.ts +30 -0
- package/dist/augments/string/prefix.js +30 -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 +18 -0
- package/dist/augments/string/replace.js +22 -0
- package/dist/augments/string/split.d.ts +31 -0
- package/dist/augments/string/split.js +53 -0
- package/dist/augments/string/substring-index.d.ts +19 -0
- package/dist/augments/string/substring-index.js +47 -0
- package/dist/augments/string/suffix.d.ts +96 -0
- package/dist/augments/string/suffix.js +89 -0
- package/dist/augments/string/white-space.d.ts +12 -0
- package/dist/augments/string/white-space.js +19 -0
- package/dist/augments/string/wrap-string.d.ts +19 -0
- package/dist/augments/string/wrap-string.js +20 -0
- package/dist/augments/type/ensure-type.d.ts +12 -0
- package/dist/augments/type/ensure-type.js +14 -0
- package/dist/augments/type/readonly.d.ts +9 -0
- package/dist/augments/type/readonly.js +10 -0
- package/dist/augments/type/type-recursion.d.ts +253 -0
- package/dist/augments/type/type-recursion.js +1 -0
- 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 +9 -0
- package/dist/augments/type/void-type.js +1 -0
- package/dist/augments/type/writable.d.ts +10 -0
- package/dist/augments/type/writable.js +10 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +83 -0
- package/package.json +43 -15
- package/dist/cjs/augments/ansi.js +0 -27
- package/dist/cjs/augments/array/array.js +0 -68
- package/dist/cjs/augments/array/remove-duplicates.js +0 -16
- package/dist/cjs/augments/async.js +0 -28
- package/dist/cjs/augments/boolean.js +0 -19
- package/dist/cjs/augments/common-number.js +0 -112
- package/dist/cjs/augments/common-string.js +0 -245
- package/dist/cjs/augments/debounce.js +0 -38
- package/dist/cjs/augments/environment.js +0 -6
- package/dist/cjs/augments/error.js +0 -52
- package/dist/cjs/augments/esm-path.js +0 -11
- package/dist/cjs/augments/function.js +0 -2
- package/dist/cjs/augments/json-compatible.js +0 -2
- package/dist/cjs/augments/json.js +0 -28
- package/dist/cjs/augments/object/any-object.js +0 -2
- package/dist/cjs/augments/object/enum.js +0 -48
- package/dist/cjs/augments/object/filter-object.js +0 -26
- package/dist/cjs/augments/object/get-or-set.js +0 -40
- package/dist/cjs/augments/object/has-key.js +0 -6
- package/dist/cjs/augments/object/jsonify.js +0 -6
- package/dist/cjs/augments/object/key-count.js +0 -2
- package/dist/cjs/augments/object/map-object.js +0 -61
- package/dist/cjs/augments/object/merge-deep.js +0 -52
- package/dist/cjs/augments/object/merge-property-arrays.js +0 -14
- package/dist/cjs/augments/object/object-entries.js +0 -40
- package/dist/cjs/augments/object/object.js +0 -18
- package/dist/cjs/augments/object/pick-deep.js +0 -2
- package/dist/cjs/augments/object/selection-set.js +0 -56
- package/dist/cjs/augments/object/typed-has-property.js +0 -29
- package/dist/cjs/augments/promise/deferred-promise.js +0 -31
- package/dist/cjs/augments/promise/promise.js +0 -79
- package/dist/cjs/augments/promise/wait.js +0 -66
- package/dist/cjs/augments/random.js +0 -118
- package/dist/cjs/augments/regexp.js +0 -19
- package/dist/cjs/augments/string/prefixes.js +0 -20
- package/dist/cjs/augments/string/suffixes.js +0 -40
- package/dist/cjs/augments/string/uuid.js +0 -13
- package/dist/cjs/augments/time.js +0 -35
- package/dist/cjs/augments/truncate-number.js +0 -153
- package/dist/cjs/augments/tuple.js +0 -20
- package/dist/cjs/augments/type-recursion.js +0 -2
- package/dist/cjs/augments/type.js +0 -36
- package/dist/cjs/augments/union.js +0 -2
- package/dist/cjs/augments/wrap-in-try.js +0 -37
- package/dist/cjs/index.js +0 -60
- package/dist/esm/augments/ansi.js +0 -24
- package/dist/esm/augments/array/array.js +0 -57
- package/dist/esm/augments/array/remove-duplicates.js +0 -13
- package/dist/esm/augments/async.js +0 -23
- package/dist/esm/augments/boolean.js +0 -14
- package/dist/esm/augments/common-number.js +0 -100
- package/dist/esm/augments/common-string.js +0 -227
- package/dist/esm/augments/debounce.js +0 -34
- package/dist/esm/augments/environment.js +0 -3
- package/dist/esm/augments/error.js +0 -45
- package/dist/esm/augments/esm-path.js +0 -8
- package/dist/esm/augments/json.js +0 -24
- package/dist/esm/augments/object/enum.js +0 -41
- package/dist/esm/augments/object/filter-object.js +0 -21
- package/dist/esm/augments/object/get-or-set.js +0 -36
- package/dist/esm/augments/object/has-key.js +0 -3
- package/dist/esm/augments/object/jsonify.js +0 -3
- package/dist/esm/augments/object/map-object.js +0 -57
- package/dist/esm/augments/object/merge-property-arrays.js +0 -11
- package/dist/esm/augments/object/object-entries.js +0 -32
- package/dist/esm/augments/object/object.js +0 -14
- package/dist/esm/augments/object/selection-set.js +0 -52
- package/dist/esm/augments/object/typed-has-property.js +0 -25
- package/dist/esm/augments/promise/deferred-promise.js +0 -28
- package/dist/esm/augments/promise/promise.js +0 -71
- package/dist/esm/augments/promise/wait.js +0 -60
- package/dist/esm/augments/random.js +0 -113
- package/dist/esm/augments/regexp.js +0 -14
- package/dist/esm/augments/string/prefixes.js +0 -16
- package/dist/esm/augments/string/suffixes.js +0 -31
- package/dist/esm/augments/string/uuid.js +0 -9
- package/dist/esm/augments/time.js +0 -31
- package/dist/esm/augments/tuple.js +0 -16
- package/dist/esm/augments/type.js +0 -30
- package/dist/esm/augments/wrap-in-try.js +0 -34
- package/dist/esm/index.js +0 -44
- package/dist/types/augments/ansi.d.ts +0 -1
- package/dist/types/augments/array/array.d.ts +0 -26
- package/dist/types/augments/array/remove-duplicates.d.ts +0 -1
- package/dist/types/augments/async.d.ts +0 -14
- package/dist/types/augments/boolean.d.ts +0 -6
- package/dist/types/augments/common-number.d.ts +0 -52
- package/dist/types/augments/common-string.d.ts +0 -74
- package/dist/types/augments/debounce.d.ts +0 -15
- package/dist/types/augments/environment.d.ts +0 -1
- package/dist/types/augments/error.d.ts +0 -9
- package/dist/types/augments/esm-path.d.ts +0 -4
- package/dist/types/augments/function.d.ts +0 -25
- package/dist/types/augments/json-compatible.d.ts +0 -22
- package/dist/types/augments/json.d.ts +0 -9
- package/dist/types/augments/object/any-object.d.ts +0 -2
- package/dist/types/augments/object/enum.d.ts +0 -8
- package/dist/types/augments/object/filter-object.d.ts +0 -4
- package/dist/types/augments/object/get-or-set.d.ts +0 -26
- package/dist/types/augments/object/has-key.d.ts +0 -1
- package/dist/types/augments/object/jsonify.d.ts +0 -2
- package/dist/types/augments/object/key-count.d.ts +0 -13
- package/dist/types/augments/object/map-object.d.ts +0 -20
- package/dist/types/augments/object/merge-property-arrays.d.ts +0 -1
- package/dist/types/augments/object/object-entries.d.ts +0 -8
- package/dist/types/augments/object/object.d.ts +0 -21
- package/dist/types/augments/object/pick-deep.d.ts +0 -14
- package/dist/types/augments/object/selection-set.d.ts +0 -27
- package/dist/types/augments/object/typed-has-property.d.ts +0 -7
- package/dist/types/augments/promise/deferred-promise.d.ts +0 -8
- package/dist/types/augments/promise/promise.d.ts +0 -15
- package/dist/types/augments/promise/wait.d.ts +0 -18
- package/dist/types/augments/random.d.ts +0 -37
- package/dist/types/augments/regexp.d.ts +0 -3
- package/dist/types/augments/string/prefixes.d.ts +0 -9
- package/dist/types/augments/string/suffixes.d.ts +0 -17
- package/dist/types/augments/string/uuid.d.ts +0 -5
- package/dist/types/augments/time.d.ts +0 -14
- package/dist/types/augments/truncate-number.d.ts +0 -19
- package/dist/types/augments/tuple.d.ts +0 -17
- package/dist/types/augments/type-recursion.d.ts +0 -161
- package/dist/types/augments/type.d.ts +0 -69
- package/dist/types/augments/union.d.ts +0 -5
- package/dist/types/index.d.ts +0 -44
- /package/dist/{esm/augments → augments/json}/json-compatible.js +0 -0
- /package/dist/{esm/augments/function.js → augments/log/log-writer.js} +0 -0
- /package/dist/{esm/augments/object/any-object.js → augments/number/coords.js} +0 -0
- /package/dist/{esm/augments/object/key-count.js → augments/number/digit.js} +0 -0
- /package/dist/{esm/augments/object/pick-deep.js → augments/number/dimensions.js} +0 -0
- /package/dist/{esm/augments/type-recursion.js → augments/object/empty.js} +0 -0
- /package/dist/{esm/augments/union.js → augments/object/key-count.js} +0 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A union of all single digits in base 10.
|
|
3
|
+
*
|
|
4
|
+
* @category Number
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple type for storing 2D dimensions.
|
|
3
|
+
*
|
|
4
|
+
* @category Number
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export type Dimensions = {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A simple type for storing 3D dimensions.
|
|
14
|
+
*
|
|
15
|
+
* @category Number
|
|
16
|
+
* @category Package : @augment-vir/common
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
*/
|
|
19
|
+
export type Dimensions3d = {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
depth: number;
|
|
23
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @category Number
|
|
3
|
+
* @category Package : @augment-vir/common
|
|
4
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
5
|
+
*/
|
|
6
|
+
export type MinMax = {
|
|
7
|
+
min: number;
|
|
8
|
+
max: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
|
|
12
|
+
* that is not the case, values are swapped.
|
|
13
|
+
*
|
|
14
|
+
* @category Number
|
|
15
|
+
* @category Package : @augment-vir/common
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export declare function ensureMinMax({ min, max }: MinMax): MinMax;
|
|
@@ -0,0 +1,16 @@
|
|
|
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, values are swapped.
|
|
4
|
+
*
|
|
5
|
+
* @category Number
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
9
|
+
export function ensureMinMax({ min, max }) {
|
|
10
|
+
if (min > max) {
|
|
11
|
+
return { min: max, max: min };
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return { min, max };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @returns The converted number or `NaN`.
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
10
|
+
export declare function toNumber(input: unknown): number;
|
|
11
|
+
/**
|
|
12
|
+
* Converts the input into a number and throws an error if the conversion fails.
|
|
13
|
+
*
|
|
14
|
+
* @category Number
|
|
15
|
+
* @category Package : @augment-vir/common
|
|
16
|
+
* @returns The converted number
|
|
17
|
+
* @throws `TypeError` if the conversion resulted in `NaN`
|
|
18
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
19
|
+
*/
|
|
20
|
+
export declare function toEnsuredNumber(input: unknown): number;
|
|
21
|
+
/**
|
|
22
|
+
* Converts the input into a number and returns `undefined` if the conversion fails.
|
|
23
|
+
*
|
|
24
|
+
* @category Number
|
|
25
|
+
* @category Package : @augment-vir/common
|
|
26
|
+
* @returns The converted number or `undefined`.
|
|
27
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
28
|
+
*/
|
|
29
|
+
export declare function toMaybeNumber(input: unknown): number | undefined;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { removeCommas } from '../string/comma.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
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @returns The converted number or `NaN`.
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
11
|
+
export function toNumber(input) {
|
|
12
|
+
if (typeof input === 'number') {
|
|
13
|
+
return input;
|
|
14
|
+
}
|
|
15
|
+
else if (typeof input === 'string') {
|
|
16
|
+
return Number(removeCommas(input));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return Number(input);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Converts the input into a number and throws an error if the conversion fails.
|
|
24
|
+
*
|
|
25
|
+
* @category Number
|
|
26
|
+
* @category Package : @augment-vir/common
|
|
27
|
+
* @returns The converted number
|
|
28
|
+
* @throws `TypeError` if the conversion resulted in `NaN`
|
|
29
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
30
|
+
*/
|
|
31
|
+
export function toEnsuredNumber(input) {
|
|
32
|
+
const maybeNumber = toMaybeNumber(input);
|
|
33
|
+
if (maybeNumber == undefined) {
|
|
34
|
+
throw new TypeError(`Cannot convert to a number: ${String(input)}`);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return maybeNumber;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts the input into a number and returns `undefined` if the conversion fails.
|
|
42
|
+
*
|
|
43
|
+
* @category Number
|
|
44
|
+
* @category Package : @augment-vir/common
|
|
45
|
+
* @returns The converted number or `undefined`.
|
|
46
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
47
|
+
*/
|
|
48
|
+
export function toMaybeNumber(input) {
|
|
49
|
+
const numeric = toNumber(input);
|
|
50
|
+
if (isNaN(numeric)) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return numeric;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Round a value to the given number of decimal digits. If no decimal value is present, no rounding
|
|
3
|
+
* occurs.
|
|
4
|
+
*
|
|
5
|
+
* @category Number
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {round} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* // `result1` is `5.13`
|
|
13
|
+
* const result1 = round(5.125, {digits: 2});
|
|
14
|
+
* // `result2` is `5`
|
|
15
|
+
* const result2 = round(25, {digits: 2});
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
19
|
+
*/
|
|
20
|
+
export declare function round(value: number, { digits }: {
|
|
21
|
+
digits: number;
|
|
22
|
+
}): number;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Round a value to the given number of decimal digits. If no decimal value is present, no rounding
|
|
3
|
+
* occurs.
|
|
4
|
+
*
|
|
5
|
+
* @category Number
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {round} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* // `result1` is `5.13`
|
|
13
|
+
* const result1 = round(5.125, {digits: 2});
|
|
14
|
+
* // `result2` is `5`
|
|
15
|
+
* const result2 = round(25, {digits: 2});
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
19
|
+
*/
|
|
20
|
+
export function round(value, { digits }) {
|
|
21
|
+
const digitFactor = Math.pow(10, digits);
|
|
22
|
+
const multiplied = value * digitFactor;
|
|
23
|
+
return Number((Math.round(multiplied) / digitFactor).toFixed(digits));
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if the given number is so large that it requires scientific notation (`e`) when
|
|
3
|
+
* represented as a string.
|
|
4
|
+
*
|
|
5
|
+
* @category Number
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {requiresScientificNotation} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* requiresScientificNotation(5); // false
|
|
13
|
+
* requiresScientificNotation(999999999999999999999); // true
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export declare function requiresScientificNotation(input: number): boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if the given number is so large that it requires scientific notation (`e`) when
|
|
3
|
+
* represented as a string.
|
|
4
|
+
*
|
|
5
|
+
* @category Number
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {requiresScientificNotation} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* requiresScientificNotation(5); // false
|
|
13
|
+
* requiresScientificNotation(999999999999999999999); // true
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export function requiresScientificNotation(input) {
|
|
19
|
+
return String(input).includes('e');
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The default truncation prefixes for {@link truncateNumber}.
|
|
3
|
+
*
|
|
4
|
+
* @category Number
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export declare const defaultTruncationSuffixes: readonly ["k", "M", "B", "T", "P", "E", "Z", "Y"];
|
|
9
|
+
/**
|
|
10
|
+
* Truncates a number such that is will at a max have 6 (customizable) characters including suffix,
|
|
11
|
+
* decimal point, or comma.
|
|
12
|
+
*
|
|
13
|
+
* Default suffixes are in {@link defaultTruncationSuffixes}:
|
|
14
|
+
*
|
|
15
|
+
* 'k', // thousand
|
|
16
|
+
* 'M', // million
|
|
17
|
+
* 'B', // billion
|
|
18
|
+
* 'T', // trillion
|
|
19
|
+
* 'P', // peta-, quadrillion
|
|
20
|
+
* 'E', // exa- quintillion
|
|
21
|
+
* 'Z', // zetta- sextillion
|
|
22
|
+
* 'Y', // yotta- septillion
|
|
23
|
+
*
|
|
24
|
+
* @category Number
|
|
25
|
+
* @category Package : @augment-vir/common
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import {truncateNumber} from '@augment-vir/common';
|
|
30
|
+
*
|
|
31
|
+
* // `result` will be `'1M'`
|
|
32
|
+
* const result = truncateNumber(1_000_000);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
36
|
+
*/
|
|
37
|
+
export declare function truncateNumber(originalValue: Readonly<unknown>, { customSuffixes, maxLength, }?: Partial<{
|
|
38
|
+
customSuffixes: ReadonlyArray<string> | undefined;
|
|
39
|
+
maxLength: number | undefined;
|
|
40
|
+
}>): string;
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { safeMatch } from '../regexp/match.js';
|
|
2
|
+
import { addCommasToNumber } from '../string/comma.js';
|
|
3
|
+
import { safeSplit } from '../string/split.js';
|
|
4
|
+
import { toNumber } from './number-conversion.js';
|
|
5
|
+
import { requiresScientificNotation } from './scientific.js';
|
|
6
|
+
/**
|
|
7
|
+
* The default truncation prefixes for {@link truncateNumber}.
|
|
8
|
+
*
|
|
9
|
+
* @category Number
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
12
|
+
*/
|
|
13
|
+
export const defaultTruncationSuffixes = [
|
|
5
14
|
'k', // thousand
|
|
6
15
|
'M', // million
|
|
7
16
|
'B', // billion
|
|
@@ -32,23 +41,22 @@ function combineBeforeAndAfterDot({ beforeDot, afterDot = '', maxLength, }) {
|
|
|
32
41
|
return beforeDot;
|
|
33
42
|
}
|
|
34
43
|
function truncateBigNumber(numberAsString, suffixes, maxLength) {
|
|
35
|
-
const [beforeDot, afterDot,] =
|
|
44
|
+
const [beforeDot, afterDot,] = safeSplit(numberAsString, '.');
|
|
36
45
|
const withCommas = addCommasToNumber(beforeDot);
|
|
37
46
|
const truncationDepth = safeMatch(withCommas, /,/g).length;
|
|
38
47
|
const suffix = suffixes[truncationDepth - 1];
|
|
39
|
-
const [beforeComma, afterComma,] =
|
|
48
|
+
const [beforeComma, afterComma,] = safeSplit(withCommas, ',');
|
|
40
49
|
const trailing = [
|
|
41
50
|
afterComma,
|
|
42
51
|
afterDot,
|
|
43
52
|
].join('');
|
|
44
53
|
if (beforeComma.length + 1 > maxLength) {
|
|
45
54
|
// will look like 0.9M
|
|
46
|
-
|
|
55
|
+
return [
|
|
47
56
|
'0.',
|
|
48
57
|
beforeComma[0],
|
|
49
58
|
suffixes[truncationDepth],
|
|
50
59
|
].join('');
|
|
51
|
-
return minimumString;
|
|
52
60
|
}
|
|
53
61
|
else {
|
|
54
62
|
const combined = combineBeforeAndAfterDot({
|
|
@@ -65,15 +73,16 @@ function truncateBigNumber(numberAsString, suffixes, maxLength) {
|
|
|
65
73
|
const minScientificNotationLength = '1e+'.length;
|
|
66
74
|
function truncateScientificNotation({ input, maxLength, }) {
|
|
67
75
|
const valueString = String(input);
|
|
68
|
-
const [beforeExponent, rawExponent,] =
|
|
69
|
-
const exponent = rawExponent.replace(/^[
|
|
76
|
+
const [beforeExponent, rawExponent,] = safeSplit(valueString, 'e');
|
|
77
|
+
const exponent = rawExponent.replace(/^[-+]/, '');
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
70
79
|
const plusOrMinus = rawExponent[0];
|
|
71
80
|
const eSuffix = [
|
|
72
81
|
'e',
|
|
73
82
|
plusOrMinus,
|
|
74
83
|
exponent,
|
|
75
84
|
].join('');
|
|
76
|
-
const [beforeDot, afterDot,] =
|
|
85
|
+
const [beforeDot, afterDot,] = safeSplit(beforeExponent, '.');
|
|
77
86
|
const minLength = exponent.length + minScientificNotationLength;
|
|
78
87
|
if (minLength === maxLength) {
|
|
79
88
|
// this will look like "4e+4" or "5e-234"
|
|
@@ -105,7 +114,7 @@ function truncateScientificNotation({ input, maxLength, }) {
|
|
|
105
114
|
}
|
|
106
115
|
}
|
|
107
116
|
function handleSmallNumbers(numberAsString, maxLength) {
|
|
108
|
-
const [beforeDot, afterDot,] =
|
|
117
|
+
const [beforeDot, afterDot,] = safeSplit(addCommasToNumber(numberAsString), '.');
|
|
109
118
|
if (beforeDot.length <= maxLength) {
|
|
110
119
|
return combineBeforeAndAfterDot({
|
|
111
120
|
beforeDot,
|
|
@@ -117,10 +126,10 @@ function handleSmallNumbers(numberAsString, maxLength) {
|
|
|
117
126
|
return undefined;
|
|
118
127
|
}
|
|
119
128
|
/**
|
|
120
|
-
*
|
|
121
|
-
* point, or comma.
|
|
129
|
+
* Truncates a number such that is will at a max have 6 (customizable) characters including suffix,
|
|
130
|
+
* decimal point, or comma.
|
|
122
131
|
*
|
|
123
|
-
* Default suffixes are:
|
|
132
|
+
* Default suffixes are in {@link defaultTruncationSuffixes}:
|
|
124
133
|
*
|
|
125
134
|
* 'k', // thousand
|
|
126
135
|
* 'M', // million
|
|
@@ -130,15 +139,28 @@ function handleSmallNumbers(numberAsString, maxLength) {
|
|
|
130
139
|
* 'E', // exa- quintillion
|
|
131
140
|
* 'Z', // zetta- sextillion
|
|
132
141
|
* 'Y', // yotta- septillion
|
|
142
|
+
*
|
|
143
|
+
* @category Number
|
|
144
|
+
* @category Package : @augment-vir/common
|
|
145
|
+
* @example
|
|
146
|
+
*
|
|
147
|
+
* ```ts
|
|
148
|
+
* import {truncateNumber} from '@augment-vir/common';
|
|
149
|
+
*
|
|
150
|
+
* // `result` will be `'1M'`
|
|
151
|
+
* const result = truncateNumber(1_000_000);
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
133
155
|
*/
|
|
134
156
|
export function truncateNumber(originalValue, { customSuffixes = defaultTruncationSuffixes, maxLength = 6, } = {}) {
|
|
135
|
-
const inputNumber =
|
|
157
|
+
const inputNumber = toNumber(originalValue);
|
|
136
158
|
// handle edge cases
|
|
137
159
|
if (isNaN(inputNumber) || inputNumber === Infinity) {
|
|
138
160
|
return String(inputNumber);
|
|
139
161
|
}
|
|
140
162
|
// handle too big or too small edge cases
|
|
141
|
-
if (
|
|
163
|
+
if (requiresScientificNotation(inputNumber)) {
|
|
142
164
|
return truncateScientificNotation({ input: inputNumber, maxLength });
|
|
143
165
|
}
|
|
144
166
|
const numberAsString = String(inputNumber);
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
* {@link clamp} function does), this function wraps the value around to the next bound (inclusive).
|
|
5
|
+
*
|
|
6
|
+
* @category Number
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {wrapNumber} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* wrapNumber({min: 0, max: 100, value: 101}); // 0
|
|
14
|
+
* wrapNumber({min: 0, max: 100, value: -1}); // 100
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
*/
|
|
19
|
+
export declare function wrapNumber(value: number, minMax: Readonly<MinMax>): number;
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* {@link clamp} function does), this function wraps the value around to the next bound (inclusive).
|
|
5
|
+
*
|
|
6
|
+
* @category Number
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {wrapNumber} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* wrapNumber({min: 0, max: 100, value: 101}); // 0
|
|
14
|
+
* wrapNumber({min: 0, max: 100, value: -1}); // 100
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
*/
|
|
19
|
+
export function wrapNumber(value, minMax) {
|
|
20
|
+
const { min, max } = ensureMinMax(minMax);
|
|
21
|
+
if (value > max) {
|
|
22
|
+
return min;
|
|
23
|
+
}
|
|
24
|
+
else if (value < min) {
|
|
25
|
+
return max;
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PartialDeep } from 'type-fest';
|
|
2
|
+
/**
|
|
3
|
+
* Extract all nested object keys and values that are different between the two given objects.
|
|
4
|
+
*
|
|
5
|
+
* @category Object
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
8
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
11
|
+
export declare function diffObjects<T0 extends Readonly<Record<PropertyKey, unknown>>, T1 extends Readonly<Record<PropertyKey, unknown>>>(object0: T0, object1: T1): [PartialDeep<T0>, PartialDeep<T1>] | [];
|
|
12
|
+
/**
|
|
13
|
+
* Extract all entries in the given arrays that are not equal.
|
|
14
|
+
*
|
|
15
|
+
* @category Object
|
|
16
|
+
* @category Package : @augment-vir/common
|
|
17
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
18
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
19
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
20
|
+
*/
|
|
21
|
+
export declare function diffArrays<T0, T1>(array0: ReadonlyArray<T0>, array1: ReadonlyArray<T1>): [Array<T0>, Array<T1>] | [];
|
|
22
|
+
/**
|
|
23
|
+
* Callback for checking equality between two values that can be of different types.
|
|
24
|
+
*
|
|
25
|
+
* @category Object
|
|
26
|
+
* @category Package : @augment-vir/common
|
|
27
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
28
|
+
*/
|
|
29
|
+
export type AreEqualCallback<T0, T1> = (value0: T0, value1: T1) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Simple diff check that is useful simply to return the same format as the other diff functions.
|
|
32
|
+
*
|
|
33
|
+
* @category Object
|
|
34
|
+
* @category Package : @augment-vir/common
|
|
35
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
36
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
37
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
38
|
+
*/
|
|
39
|
+
export declare function diffBasic<T0, T1>(value0: T0, value1: T1,
|
|
40
|
+
/** A custom equality checker. Defaults to a strict equality check (`===`). */
|
|
41
|
+
areEqual?: AreEqualCallback<T0, T1>): [T0, T1] | [];
|
|
42
|
+
/**
|
|
43
|
+
* Diff any values. For diffing objects, use `diffObjects` to get better types.
|
|
44
|
+
*
|
|
45
|
+
* @category Object
|
|
46
|
+
* @category Package : @augment-vir/common
|
|
47
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
48
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
49
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
50
|
+
*/
|
|
51
|
+
export declare function diffValues<T0, T1>(value0: T0, value1: T1): [T0, T1] | [];
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
import { getObjectTypedKeys } from '@augment-vir/core';
|
|
3
|
+
/**
|
|
4
|
+
* Extract all nested object keys and values that are different between the two given objects.
|
|
5
|
+
*
|
|
6
|
+
* @category Object
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
9
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
10
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
11
|
+
*/
|
|
12
|
+
export function diffObjects(object0, object1) {
|
|
13
|
+
const allObjectKeys = Array.from(new Set([
|
|
14
|
+
...getObjectTypedKeys(object0),
|
|
15
|
+
...getObjectTypedKeys(object1),
|
|
16
|
+
]));
|
|
17
|
+
const diffOutput = allObjectKeys.reduce((accum, objectKey) => {
|
|
18
|
+
const value0 = object0[objectKey];
|
|
19
|
+
const value1 = object1[objectKey];
|
|
20
|
+
const diffOutput = diffValues(value0, value1);
|
|
21
|
+
if (!diffOutput.length) {
|
|
22
|
+
return accum;
|
|
23
|
+
}
|
|
24
|
+
if (!(objectKey in object0)) {
|
|
25
|
+
accum[1][objectKey] = diffOutput[1];
|
|
26
|
+
}
|
|
27
|
+
else if (objectKey in object1) {
|
|
28
|
+
accum[0][objectKey] = diffOutput[0];
|
|
29
|
+
accum[1][objectKey] = diffOutput[1];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
accum[0][objectKey] = diffOutput[0];
|
|
33
|
+
}
|
|
34
|
+
return accum;
|
|
35
|
+
}, [
|
|
36
|
+
{},
|
|
37
|
+
{},
|
|
38
|
+
]);
|
|
39
|
+
if (!Object.keys(diffOutput[0]).length && !Object.keys(diffOutput[1]).length) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return diffOutput;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Extract all entries in the given arrays that are not equal.
|
|
48
|
+
*
|
|
49
|
+
* @category Object
|
|
50
|
+
* @category Package : @augment-vir/common
|
|
51
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
52
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
53
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
54
|
+
*/
|
|
55
|
+
export function diffArrays(array0, array1) {
|
|
56
|
+
const allArrayIndexes = Array.from(new Set([
|
|
57
|
+
...Object.keys(array0),
|
|
58
|
+
...Object.keys(array1),
|
|
59
|
+
].map((index) => Number(index)))).sort();
|
|
60
|
+
const diffArrays = allArrayIndexes.reduce((accum, arrayIndex) => {
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
62
|
+
const value0 = array0[arrayIndex];
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
64
|
+
const value1 = array1[arrayIndex];
|
|
65
|
+
const diffOutput = diffValues(value0, value1);
|
|
66
|
+
if (diffOutput.length) {
|
|
67
|
+
if (arrayIndex in array0) {
|
|
68
|
+
accum[0].push(diffOutput[0]);
|
|
69
|
+
}
|
|
70
|
+
if (arrayIndex in array1) {
|
|
71
|
+
accum[1].push(diffOutput[1]);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return accum;
|
|
75
|
+
}, [
|
|
76
|
+
[],
|
|
77
|
+
[],
|
|
78
|
+
]);
|
|
79
|
+
if (!diffArrays[0].length && !diffArrays[1].length) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
return diffArrays;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Simple diff check that is useful simply to return the same format as the other diff functions.
|
|
88
|
+
*
|
|
89
|
+
* @category Object
|
|
90
|
+
* @category Package : @augment-vir/common
|
|
91
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
92
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
93
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
94
|
+
*/
|
|
95
|
+
export function diffBasic(value0, value1,
|
|
96
|
+
/** A custom equality checker. Defaults to a strict equality check (`===`). */
|
|
97
|
+
areEqual = (value0, value1) => value0 === value1) {
|
|
98
|
+
if (areEqual(value0, value1)) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
return [
|
|
103
|
+
value0,
|
|
104
|
+
value1,
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const orderedValueDiffs = [
|
|
109
|
+
(value0, value1) => {
|
|
110
|
+
if (!check.isArray(value0) || !check.isArray(value1)) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
return diffArrays(value0, value1);
|
|
114
|
+
},
|
|
115
|
+
(value0, value1) => {
|
|
116
|
+
if (!check.instanceOf(value0, RegExp) || !check.instanceOf(value1, RegExp)) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
/** Special case RegExps because they should be checked for equality as strings. */
|
|
120
|
+
return diffBasic(value0, value1, (a, b) => String(a) === String(b));
|
|
121
|
+
},
|
|
122
|
+
(value0, value1) => {
|
|
123
|
+
if (!check.isObject(value0) || !check.isObject(value1)) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return diffObjects(value0, value1);
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
/**
|
|
130
|
+
* Diff any values. For diffing objects, use `diffObjects` to get better types.
|
|
131
|
+
*
|
|
132
|
+
* @category Object
|
|
133
|
+
* @category Package : @augment-vir/common
|
|
134
|
+
* @returns An empty tuple if the values are equal. Otherwise, the first tuple entry contains the
|
|
135
|
+
* changes in the first value, second entry contains the changes in the second value.
|
|
136
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
137
|
+
*/
|
|
138
|
+
export function diffValues(value0, value1) {
|
|
139
|
+
let diffOutput = undefined;
|
|
140
|
+
orderedValueDiffs.some((differ) => {
|
|
141
|
+
diffOutput = differ(value0, value1);
|
|
142
|
+
return !!diffOutput;
|
|
143
|
+
});
|
|
144
|
+
if (diffOutput) {
|
|
145
|
+
return diffOutput;
|
|
146
|
+
}
|
|
147
|
+
/** Fallback to the basic diff. */
|
|
148
|
+
return diffBasic(value0, value1);
|
|
149
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IsEmptyObject } from 'type-fest';
|
|
2
|
+
/**
|
|
3
|
+
* Excludes empty objects from a union.
|
|
4
|
+
*
|
|
5
|
+
* @category Object
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
9
|
+
export type ExcludeEmpty<T> = IsEmptyObject<T> extends true ? never : T;
|