@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,27 @@
|
|
|
1
|
+
import { getObjectTypedEntries, typedObjectFromEntries } from './object-entries.js';
|
|
2
|
+
/**
|
|
3
|
+
* Filters an object. Like
|
|
4
|
+
* [`[].filter`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
|
|
5
|
+
* but for objects.
|
|
6
|
+
*
|
|
7
|
+
* @category Object
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {filterObject} from '@augment-vir';
|
|
13
|
+
*
|
|
14
|
+
* filterObject({a: 1, b: 2, c: 3}, (key, value) => {
|
|
15
|
+
* return value >= 2;
|
|
16
|
+
* });
|
|
17
|
+
* // output is `{b: 2, c: 3}`
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
21
|
+
*/
|
|
22
|
+
export function filterObject(inputObject, callback) {
|
|
23
|
+
const filteredEntries = getObjectTypedEntries(inputObject).filter(([key, value,]) => {
|
|
24
|
+
return callback(key, value, inputObject);
|
|
25
|
+
});
|
|
26
|
+
return typedObjectFromEntries(filteredEntries);
|
|
27
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Same as the TypeScript built-in type `Omit` except that it works on actual runtime values.
|
|
3
|
+
*
|
|
4
|
+
* @category Object
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {omitObjectKeys} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* omitObjectKeys({a: 'a', b: 'b'}, ['a']);
|
|
12
|
+
* // output is `{b: 'b'}`
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
16
|
+
*/
|
|
17
|
+
export declare function omitObjectKeys<const ObjectGeneric, const KeyGeneric extends keyof ObjectGeneric>(inputObject: Readonly<ObjectGeneric>, omitTheseKeys: ReadonlyArray<KeyGeneric>): Omit<ObjectGeneric, KeyGeneric>;
|
|
18
|
+
/**
|
|
19
|
+
* Same as the TypeScript built-in type `Pick` except that it works on actual runtime values.
|
|
20
|
+
*
|
|
21
|
+
* @category Object
|
|
22
|
+
* @category Package : @augment-vir/common
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import {pickObjectKeys} from '@augment-vir/common';
|
|
27
|
+
*
|
|
28
|
+
* pickObjectKeys({a: 'a', b: 'b'}, ['a']);
|
|
29
|
+
* // output is `{a: 'a'}`
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
33
|
+
*/
|
|
34
|
+
export declare function pickObjectKeys<const ObjectGeneric, const KeyGeneric extends keyof ObjectGeneric>(inputObject: Readonly<ObjectGeneric>, pickTheseKeys: ReadonlyArray<KeyGeneric>): Pick<ObjectGeneric, KeyGeneric>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { filterObject } from './object-filter.js';
|
|
2
|
+
/**
|
|
3
|
+
* Same as the TypeScript built-in type `Omit` except that it works on actual runtime values.
|
|
4
|
+
*
|
|
5
|
+
* @category Object
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {omitObjectKeys} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* omitObjectKeys({a: 'a', b: 'b'}, ['a']);
|
|
13
|
+
* // output is `{b: 'b'}`
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export function omitObjectKeys(inputObject, omitTheseKeys) {
|
|
19
|
+
return filterObject(inputObject, (currentKey) => {
|
|
20
|
+
return !omitTheseKeys.includes(currentKey);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Same as the TypeScript built-in type `Pick` except that it works on actual runtime values.
|
|
25
|
+
*
|
|
26
|
+
* @category Object
|
|
27
|
+
* @category Package : @augment-vir/common
|
|
28
|
+
* @example
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import {pickObjectKeys} from '@augment-vir/common';
|
|
32
|
+
*
|
|
33
|
+
* pickObjectKeys({a: 'a', b: 'b'}, ['a']);
|
|
34
|
+
* // output is `{a: 'a'}`
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
38
|
+
*/
|
|
39
|
+
export function pickObjectKeys(inputObject, pickTheseKeys) {
|
|
40
|
+
return filterObject(inputObject, (currentKey) => {
|
|
41
|
+
return pickTheseKeys.includes(currentKey);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CompleteRequire } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Gets an object's values. This is the same as
|
|
4
|
+
* [`Object.values`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/values)
|
|
5
|
+
* except that it has better TypeScript types.
|
|
6
|
+
*
|
|
7
|
+
* @category Object
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
11
|
+
export declare function getObjectTypedValues<ObjectGeneric>(input: ObjectGeneric): CompleteRequire<ObjectGeneric>[keyof CompleteRequire<ObjectGeneric>][];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { getObjectTypedKeys } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Gets an object's values. This is the same as
|
|
4
|
+
* [`Object.values`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/values)
|
|
5
|
+
* except that it has better TypeScript types.
|
|
6
|
+
*
|
|
7
|
+
* @category Object
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
11
|
+
export function getObjectTypedValues(input) {
|
|
12
|
+
return getObjectTypedKeys(input).map((key) => input[key]);
|
|
13
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates the equivalent of CJS's
|
|
3
|
+
* [`__dirname`](https://nodejs.org/docs/latest/api/globals.html#__dirname) and
|
|
4
|
+
* [`__filename`](https://nodejs.org/docs/latest/api/globals.html#__filename) for ESM modules.
|
|
5
|
+
*
|
|
6
|
+
* This is the equivalent of
|
|
7
|
+
* [`import.meta.dirname`](https://nodejs.org/api/esm.html#importmetadirname) and
|
|
8
|
+
* [`import.meta.filename`](https://nodejs.org/api/esm.html#importmetafilename) added to Node.js
|
|
9
|
+
* v20.11.0 but is compatible with older versions of Node.js as well as browsers.
|
|
10
|
+
*
|
|
11
|
+
* @category Path : Common
|
|
12
|
+
* @category Package : @augment-vir/common
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {getEsmPath} from '@augment-vir/common';
|
|
17
|
+
*
|
|
18
|
+
* const {filePath, dirPath} = getEsmPath(import.meta);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
22
|
+
*/
|
|
23
|
+
export declare function getEsmPath(importMeta: ImportMeta): {
|
|
24
|
+
filePath: string;
|
|
25
|
+
dirPath: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { removeSuffix } from '../string/suffix.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates the equivalent of CJS's
|
|
4
|
+
* [`__dirname`](https://nodejs.org/docs/latest/api/globals.html#__dirname) and
|
|
5
|
+
* [`__filename`](https://nodejs.org/docs/latest/api/globals.html#__filename) for ESM modules.
|
|
6
|
+
*
|
|
7
|
+
* This is the equivalent of
|
|
8
|
+
* [`import.meta.dirname`](https://nodejs.org/api/esm.html#importmetadirname) and
|
|
9
|
+
* [`import.meta.filename`](https://nodejs.org/api/esm.html#importmetafilename) added to Node.js
|
|
10
|
+
* v20.11.0 but is compatible with older versions of Node.js as well as browsers.
|
|
11
|
+
*
|
|
12
|
+
* @category Path : Common
|
|
13
|
+
* @category Package : @augment-vir/common
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import {getEsmPath} from '@augment-vir/common';
|
|
18
|
+
*
|
|
19
|
+
* const {filePath, dirPath} = getEsmPath(import.meta);
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
23
|
+
*/
|
|
24
|
+
export function getEsmPath(importMeta) {
|
|
25
|
+
const filePath = new URL('', importMeta.url).pathname;
|
|
26
|
+
const dirPath = removeSuffix({ value: new URL('.', importMeta.url).pathname, suffix: '/' });
|
|
27
|
+
return {
|
|
28
|
+
filePath,
|
|
29
|
+
dirPath,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { AnyFunction, AnyObject } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts all model names from a generated `Prisma` object.
|
|
4
|
+
*
|
|
5
|
+
* @category Prisma : Common
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type {Prisma} from '@prisma/client';
|
|
11
|
+
* import type {ModelNameFromPrismaTypeMap} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* function doThing(modelName: ModelNameFromPrismaTypeMap<Prisma.TypeMap>) {}
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export type ModelNameFromPrismaTypeMap<PrismaTypeMap extends BasePrismaTypeMap> = PrismaTypeMap['meta']['modelProps'];
|
|
19
|
+
/**
|
|
20
|
+
* Extracts all model names from a generated `PrismaClient`.
|
|
21
|
+
*
|
|
22
|
+
* @category Prisma : Common
|
|
23
|
+
* @category Package : @augment-vir/common
|
|
24
|
+
* @example
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
28
|
+
* import type {ModelNameFromPrismaClient} from '@augment-vir/common';
|
|
29
|
+
*
|
|
30
|
+
* function doThing(modelName: ModelNameFromPrismaClient<PrismaClient>) {}
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
34
|
+
*/
|
|
35
|
+
export type ModelNameFromPrismaClient<PrismaClient extends AnyObject> = keyof {
|
|
36
|
+
[Model in Extract<keyof PrismaClient, string> as PrismaClient[Model] extends {
|
|
37
|
+
findFirst: AnyFunction;
|
|
38
|
+
} ? Model : never]: boolean;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Extracts the creation data for a model from the given `PrismaClient` type.
|
|
42
|
+
*
|
|
43
|
+
* @category Prisma : Common
|
|
44
|
+
* @category Package : @augment-vir/common
|
|
45
|
+
* @example
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
49
|
+
* import type {ModelCreationEntry} from '@augment-vir/common';
|
|
50
|
+
*
|
|
51
|
+
* function doThing(entry: ModelCreationEntry<PrismaClient, 'User'>) {}
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
55
|
+
*/
|
|
56
|
+
export type PrismaModelCreationEntry<PrismaClient extends AnyObject, Model extends ModelNameFromPrismaClient<PrismaClient>> = NonNullable<Parameters<PrismaClient[Model]['create']>[0]> extends {
|
|
57
|
+
data?: infer Data;
|
|
58
|
+
} ? NonNullable<Data> : `ERROR: failed to infer creation entry for model '${Model}'`;
|
|
59
|
+
/**
|
|
60
|
+
* A base type for `Prisma.TypeMap` because Prisma doesn't give us one. This currently only includes
|
|
61
|
+
* the properties that are used within `@augment-vir/common`.
|
|
62
|
+
*
|
|
63
|
+
* @category Prisma : Common
|
|
64
|
+
* @category Package : @augment-vir/common
|
|
65
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
66
|
+
*/
|
|
67
|
+
export type BasePrismaTypeMap = {
|
|
68
|
+
meta: {
|
|
69
|
+
modelProps: string;
|
|
70
|
+
};
|
|
71
|
+
model: Record<string, {
|
|
72
|
+
payload: BasePrismaPayload;
|
|
73
|
+
}>;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* A base type for Prisma model payloads because Prisma doesn't give us one. This currently only
|
|
77
|
+
* includes the properties that are used within this package.
|
|
78
|
+
*
|
|
79
|
+
* Note: this omits the `composites` property because I don't have any examples of what those
|
|
80
|
+
* actually are.
|
|
81
|
+
*
|
|
82
|
+
* @category Prisma : Common
|
|
83
|
+
* @category Package : @augment-vir/common
|
|
84
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
85
|
+
*/
|
|
86
|
+
export type BasePrismaPayload = {
|
|
87
|
+
name: string;
|
|
88
|
+
objects: Record<string, BasePrismaPayload | BasePrismaPayload[] | null>;
|
|
89
|
+
scalars: AnyObject;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* A full model entry with all relations from the given Prisma type map and model name.
|
|
93
|
+
*
|
|
94
|
+
* @category Prisma : Common
|
|
95
|
+
* @category Package : @augment-vir/common
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* import type {Prisma} from '@prisma/client';
|
|
100
|
+
* import type {FullPrismaModel} from '@augment-vir/common';
|
|
101
|
+
*
|
|
102
|
+
* function doThing(fullModel: FullModel<Prisma.TypeMap, 'User'>) {}
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
106
|
+
*/
|
|
107
|
+
export type FullPrismaModel<PrismaTypeMap extends BasePrismaTypeMap, Model extends ModelNameFromPrismaTypeMap<PrismaTypeMap>> = ExpandPrismaTypeMapPayload<PrismaTypeMap['model'][Model]['payload']>;
|
|
108
|
+
/**
|
|
109
|
+
* A base model entry with only its immediate properties from the given Prisma type map and model
|
|
110
|
+
* name.
|
|
111
|
+
*
|
|
112
|
+
* @category Prisma : Common
|
|
113
|
+
* @category Package : @augment-vir/common
|
|
114
|
+
* @example
|
|
115
|
+
*
|
|
116
|
+
* ```ts
|
|
117
|
+
* import type {Prisma} from '@prisma/client';
|
|
118
|
+
* import type {BasePrismaModel} from '@augment-vir/common';
|
|
119
|
+
*
|
|
120
|
+
* function doThing(fullModel: BaseModel<Prisma.TypeMap, 'User'>) {}
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
124
|
+
*/
|
|
125
|
+
export type BasePrismaModel<PrismaTypeMap extends BasePrismaTypeMap, Model extends ModelNameFromPrismaTypeMap<PrismaTypeMap>> = PrismaTypeMap['model'][Model]['payload']['scalars'];
|
|
126
|
+
/**
|
|
127
|
+
* Expand a Prisma payload into its scalars and recursive relations.
|
|
128
|
+
*
|
|
129
|
+
* @category Prisma : Common : Util
|
|
130
|
+
* @category Package : @augment-vir/common
|
|
131
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
132
|
+
*/
|
|
133
|
+
export type ExpandPrismaTypeMapPayload<Payload extends BasePrismaPayload> = Payload['scalars'] & {
|
|
134
|
+
[Key in keyof Payload['objects']]: ExpandPotentialArrayPrismaTypeMapPayload<NonNullable<Payload['objects'][Key]>> | (null extends Payload['objects'][Key] ? null : never);
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Expand a payload that might be inside of an array, keeping it inside of an array if it is.
|
|
138
|
+
*
|
|
139
|
+
* @category Prisma : Common : Util
|
|
140
|
+
* @category Package : @augment-vir/common
|
|
141
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
142
|
+
*/
|
|
143
|
+
export type ExpandPotentialArrayPrismaTypeMapPayload<PayloadWrapper extends BasePrismaPayload | BasePrismaPayload[]> = PayloadWrapper extends (infer ActualPayload extends BasePrismaPayload)[] ? ExpandPrismaTypeMapPayload<ActualPayload>[] : PayloadWrapper extends BasePrismaPayload ? ExpandPrismaTypeMapPayload<PayloadWrapper> : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AnyDuration } from '@date-vir/duration';
|
|
2
|
+
/**
|
|
3
|
+
* An error thrown by {@link wrapPromiseInTimeout} when the timeout is reached.
|
|
4
|
+
*
|
|
5
|
+
* @category Promise
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
9
|
+
export declare class PromiseTimeoutError extends Error {
|
|
10
|
+
readonly duration: AnyDuration;
|
|
11
|
+
readonly name = "PromiseTimeoutError";
|
|
12
|
+
constructor(duration: AnyDuration, failureMessage?: string | undefined);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Wraps an already-created Promise in a timeout, causing a rejection if the original Promise isn't
|
|
16
|
+
* resolved by then.
|
|
17
|
+
*
|
|
18
|
+
* @category Promise
|
|
19
|
+
* @category Package : @augment-vir/common
|
|
20
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
21
|
+
*/
|
|
22
|
+
export declare function wrapPromiseInTimeout<T>(duration: Readonly<AnyDuration>, originalPromise: PromiseLike<T>, failureMessage?: string | undefined): Promise<T>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
import { ensureError } from '@augment-vir/core';
|
|
3
|
+
import { convertDuration, DurationUnit } from '@date-vir/duration';
|
|
4
|
+
/**
|
|
5
|
+
* An error thrown by {@link wrapPromiseInTimeout} when the timeout is reached.
|
|
6
|
+
*
|
|
7
|
+
* @category Promise
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
11
|
+
export class PromiseTimeoutError extends Error {
|
|
12
|
+
duration;
|
|
13
|
+
name = 'PromiseTimeoutError';
|
|
14
|
+
constructor(duration, failureMessage) {
|
|
15
|
+
super([
|
|
16
|
+
failureMessage,
|
|
17
|
+
`Promised timed out after ${convertDuration(duration, DurationUnit.Milliseconds).milliseconds} ms.`,
|
|
18
|
+
]
|
|
19
|
+
.filter(check.isTruthy)
|
|
20
|
+
.join(': '));
|
|
21
|
+
this.duration = duration;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Wraps an already-created Promise in a timeout, causing a rejection if the original Promise isn't
|
|
26
|
+
* resolved by then.
|
|
27
|
+
*
|
|
28
|
+
* @category Promise
|
|
29
|
+
* @category Package : @augment-vir/common
|
|
30
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
31
|
+
*/
|
|
32
|
+
export function wrapPromiseInTimeout(duration, originalPromise, failureMessage) {
|
|
33
|
+
const milliseconds = convertDuration(duration, DurationUnit.Milliseconds).milliseconds;
|
|
34
|
+
return new Promise(async (resolve, reject) => {
|
|
35
|
+
const timeoutId = milliseconds === Infinity
|
|
36
|
+
? undefined
|
|
37
|
+
: setTimeout(() => {
|
|
38
|
+
reject(new PromiseTimeoutError(duration, failureMessage));
|
|
39
|
+
}, milliseconds);
|
|
40
|
+
try {
|
|
41
|
+
const result = await originalPromise;
|
|
42
|
+
resolve(result);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
reject(ensureError(error));
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true at rate of the percentLikelyToBeTrue input. Inputs should be whole numbers which
|
|
3
|
+
* will be treated like percents. Anything outside of 0-100 inclusively will be clamped. An input 0
|
|
4
|
+
* will always return true. An input of 100 will always return true. Decimals on the input will be
|
|
5
|
+
* chopped off, use whole numbers.
|
|
6
|
+
*
|
|
7
|
+
* This function uses cryptographically secure randomness.
|
|
8
|
+
*
|
|
9
|
+
* @category Random
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {randomBoolean} from '@augment-vir/common';
|
|
15
|
+
*
|
|
16
|
+
* randomBoolean(50); // 50% chance to return true
|
|
17
|
+
* randomBoolean(0); // always false, 0% chance of being true
|
|
18
|
+
* randomBoolean(100); // always true, 100% chance of being true
|
|
19
|
+
* randomBoolean(59.67); // 59% chance of being true
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
23
|
+
*/
|
|
24
|
+
export declare function randomBoolean(percentLikelyToBeTrue?: number): boolean;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { clamp } from '../number/clamp.js';
|
|
2
|
+
import { randomInteger } from './random-integer.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns true at rate of the percentLikelyToBeTrue input. Inputs should be whole numbers which
|
|
5
|
+
* will be treated like percents. Anything outside of 0-100 inclusively will be clamped. An input 0
|
|
6
|
+
* will always return true. An input of 100 will always return true. Decimals on the input will be
|
|
7
|
+
* chopped off, use whole numbers.
|
|
8
|
+
*
|
|
9
|
+
* This function uses cryptographically secure randomness.
|
|
10
|
+
*
|
|
11
|
+
* @category Random
|
|
12
|
+
* @category Package : @augment-vir/common
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {randomBoolean} from '@augment-vir/common';
|
|
17
|
+
*
|
|
18
|
+
* randomBoolean(50); // 50% chance to return true
|
|
19
|
+
* randomBoolean(0); // always false, 0% chance of being true
|
|
20
|
+
* randomBoolean(100); // always true, 100% chance of being true
|
|
21
|
+
* randomBoolean(59.67); // 59% chance of being true
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
25
|
+
*/
|
|
26
|
+
export function randomBoolean(percentLikelyToBeTrue = 50) {
|
|
27
|
+
return (randomInteger({ min: 0, max: 99 }) <
|
|
28
|
+
clamp(Math.floor(percentLikelyToBeTrue), {
|
|
29
|
+
min: 0,
|
|
30
|
+
max: 100,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a random integer (no decimal points are included) between the given min and max values
|
|
3
|
+
* (inclusive).
|
|
4
|
+
*
|
|
5
|
+
* This function uses cryptographically secure randomness.
|
|
6
|
+
*
|
|
7
|
+
* @category Random
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
11
|
+
export declare function randomInteger({ min: rawMin, max: rawMax }: {
|
|
12
|
+
min: number;
|
|
13
|
+
max: number;
|
|
14
|
+
}): number;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ensureMinMax } from '../number/min-max.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a random integer (no decimal points are included) between the given min and max values
|
|
4
|
+
* (inclusive).
|
|
5
|
+
*
|
|
6
|
+
* This function uses cryptographically secure randomness.
|
|
7
|
+
*
|
|
8
|
+
* @category Random
|
|
9
|
+
* @category Package : @augment-vir/common
|
|
10
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
11
|
+
*/
|
|
12
|
+
export function randomInteger({ min: rawMin, max: rawMax }) {
|
|
13
|
+
const { min, max } = ensureMinMax({ min: Math.floor(rawMin), max: Math.floor(rawMax) });
|
|
14
|
+
const range = max - min + 1;
|
|
15
|
+
const bitsNeeded = Math.ceil(Math.log2(range));
|
|
16
|
+
const neededBytes = Math.ceil(bitsNeeded / 8);
|
|
17
|
+
/**
|
|
18
|
+
* Testing on my system maxes out at 65,536 (Node) or 65,537 (Safari / Chrome / Firefox) bytes.
|
|
19
|
+
* I don't know why, and that may be system dependent, I don't know.
|
|
20
|
+
*/
|
|
21
|
+
if (neededBytes > 65_000) {
|
|
22
|
+
throw new RangeError(`Cannot create a random integer so large. ({min: ${min}, max: ${max}})`);
|
|
23
|
+
}
|
|
24
|
+
const cutoff = Math.floor(256 ** neededBytes / range) * range;
|
|
25
|
+
const currentBytes = new Uint8Array(neededBytes);
|
|
26
|
+
let value;
|
|
27
|
+
do {
|
|
28
|
+
crypto.getRandomValues(currentBytes);
|
|
29
|
+
value = currentBytes.reduce((accum, byte, index) => {
|
|
30
|
+
return accum + byte * 256 ** index;
|
|
31
|
+
}, 0);
|
|
32
|
+
} while (value >= cutoff);
|
|
33
|
+
return min + (value % range);
|
|
34
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All letters allowed in {@link randomString}.
|
|
3
|
+
*
|
|
4
|
+
* @category Random : Util
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export declare const allowedRandomStringLetters: ReadonlyArray<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a random string (including letters and numbers) of a given length.
|
|
11
|
+
*
|
|
12
|
+
* This function uses cryptographically secure randomness.
|
|
13
|
+
*
|
|
14
|
+
* @category Random
|
|
15
|
+
* @category Package : @augment-vir/common
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export declare function randomString(generatedStringLength?: number): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { randomInteger } from './random-integer.js';
|
|
2
|
+
/**
|
|
3
|
+
* All letters allowed in {@link randomString}.
|
|
4
|
+
*
|
|
5
|
+
* @category Random : Util
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
9
|
+
export const allowedRandomStringLetters = [
|
|
10
|
+
'a',
|
|
11
|
+
'b',
|
|
12
|
+
'c',
|
|
13
|
+
'd',
|
|
14
|
+
'e',
|
|
15
|
+
'f',
|
|
16
|
+
'g',
|
|
17
|
+
'h',
|
|
18
|
+
'i',
|
|
19
|
+
'j',
|
|
20
|
+
'k',
|
|
21
|
+
'l',
|
|
22
|
+
'm',
|
|
23
|
+
'n',
|
|
24
|
+
'o',
|
|
25
|
+
'p',
|
|
26
|
+
'q',
|
|
27
|
+
'r',
|
|
28
|
+
's',
|
|
29
|
+
't',
|
|
30
|
+
'u',
|
|
31
|
+
'v',
|
|
32
|
+
'w',
|
|
33
|
+
'x',
|
|
34
|
+
'y',
|
|
35
|
+
'z',
|
|
36
|
+
'0',
|
|
37
|
+
'1',
|
|
38
|
+
'2',
|
|
39
|
+
'3',
|
|
40
|
+
'4',
|
|
41
|
+
'5',
|
|
42
|
+
'6',
|
|
43
|
+
'7',
|
|
44
|
+
'8',
|
|
45
|
+
'9',
|
|
46
|
+
];
|
|
47
|
+
/**
|
|
48
|
+
* Creates a random string (including letters and numbers) of a given length.
|
|
49
|
+
*
|
|
50
|
+
* This function uses cryptographically secure randomness.
|
|
51
|
+
*
|
|
52
|
+
* @category Random
|
|
53
|
+
* @category Package : @augment-vir/common
|
|
54
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
55
|
+
*/
|
|
56
|
+
export function randomString(generatedStringLength = 16) {
|
|
57
|
+
let stringBuilder = '';
|
|
58
|
+
for (let i = 0; i < generatedStringLength; i++) {
|
|
59
|
+
const index = randomInteger({
|
|
60
|
+
min: 0,
|
|
61
|
+
max: allowedRandomStringLetters.length - 1,
|
|
62
|
+
});
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
64
|
+
stringBuilder += allowedRandomStringLetters[index];
|
|
65
|
+
}
|
|
66
|
+
return stringBuilder;
|
|
67
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs
|
|
3
|
+
* [`''.match`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/match)
|
|
4
|
+
* but falls back to an empty array if no match was found.
|
|
5
|
+
*
|
|
6
|
+
* @category RegExp
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
10
|
+
export declare function safeMatch(input: string, regExp: RegExp): string[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs
|
|
3
|
+
* [`''.match`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/match)
|
|
4
|
+
* but falls back to an empty array if no match was found.
|
|
5
|
+
*
|
|
6
|
+
* @category RegExp
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
10
|
+
export function safeMatch(input, regExp) {
|
|
11
|
+
const match = input.match(regExp);
|
|
12
|
+
return match ?? [];
|
|
13
|
+
}
|