@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
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @augment-vir/common
|
|
2
|
+
|
|
3
|
+
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
|
|
4
|
+
|
|
5
|
+
- Examples: [`filterObject`](https://electrovir.github.io/augment-vir/functions/filterObject.html), [`wait`](https://electrovir.github.io/augment-vir/functions/wait.html), [`getEnumValues`](https://electrovir.github.io/augment-vir/functions/getEnumValues.html)
|
|
6
|
+
- Includes a colored logger implementation: [`log`](https://electrovir.github.io/augment-vir/variables/log.html)
|
|
7
|
+
- Includes a SQL-select-like runtime implementation of TypeScript's `Pick`: [`selectFrom`](https://electrovir.github.io/augment-vir/functions/selectFrom-1.html)
|
|
8
|
+
- Includes Prisma type helpers.
|
|
9
|
+
- and much more...
|
|
10
|
+
|
|
11
|
+
See all `@augment-vir` docs here: https://electrovir.github.io/augment-vir
|
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
import type { ArrayElement } from '@augment-vir/core';
|
|
2
2
|
import { Writable } from '../type/writable.js';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Performs
|
|
5
|
+
* [`[].map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
|
6
|
+
* on an array but transfers the input tuple's size to the output type.
|
|
7
|
+
*
|
|
8
|
+
* @category Array
|
|
9
|
+
* @category Package : @augment-vir/common
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import {typedMap} from '@augment-vir/common';
|
|
14
|
+
*
|
|
15
|
+
* const result = await typedMap(
|
|
16
|
+
* [
|
|
17
|
+
* 1,
|
|
18
|
+
* 2,
|
|
19
|
+
* 3,
|
|
20
|
+
* 4,
|
|
21
|
+
* 5,
|
|
22
|
+
* ],
|
|
23
|
+
* (value) => {
|
|
24
|
+
* return value + 1;
|
|
25
|
+
* },
|
|
26
|
+
* );
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @returns A new array (does not mutate).
|
|
30
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
31
|
+
*/
|
|
4
32
|
export declare function typedMap<const ArrayGeneric extends ReadonlyArray<any>, const OutputType>(arrayToMap: ArrayGeneric, mapCallback: (value: ArrayElement<NoInfer<ArrayGeneric>>, index: number, array: NoInfer<ArrayGeneric>) => OutputType): Writable<{
|
|
5
33
|
[Index in keyof ArrayGeneric]: OutputType;
|
|
6
34
|
}>;
|
|
@@ -1,4 +1,32 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Performs
|
|
3
|
+
* [`[].map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
|
4
|
+
* on an array but transfers the input tuple's size to the output type.
|
|
5
|
+
*
|
|
6
|
+
* @category Array
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {typedMap} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* const result = await typedMap(
|
|
14
|
+
* [
|
|
15
|
+
* 1,
|
|
16
|
+
* 2,
|
|
17
|
+
* 3,
|
|
18
|
+
* 4,
|
|
19
|
+
* 5,
|
|
20
|
+
* ],
|
|
21
|
+
* (value) => {
|
|
22
|
+
* return value + 1;
|
|
23
|
+
* },
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @returns A new array (does not mutate).
|
|
28
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
29
|
+
*/
|
|
2
30
|
export function typedMap(arrayToMap, mapCallback) {
|
|
3
31
|
return arrayToMap.map(mapCallback);
|
|
4
32
|
}
|
|
@@ -1,17 +1,35 @@
|
|
|
1
|
+
import { MaybePromise } from '@augment-vir/core';
|
|
1
2
|
/**
|
|
2
3
|
* Polyfill for `Object.groupBy`:
|
|
3
|
-
* https://developer.mozilla.org/
|
|
4
|
+
* https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
|
|
4
5
|
*
|
|
5
|
-
* @category
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Like `groupArrayBy` but maps array entries to a single key. Meaning, the resulting object does
|
|
10
|
-
* not have an array of elements (unless the original array itself contains arrays).
|
|
6
|
+
* @category Array
|
|
7
|
+
* @category Object
|
|
8
|
+
* @example
|
|
11
9
|
*
|
|
12
|
-
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {arrayToObject} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* const result = arrayToObject(
|
|
14
|
+
* [
|
|
15
|
+
* 'a',
|
|
16
|
+
* 'b',
|
|
17
|
+
* ],
|
|
18
|
+
* (value) => `key-${value}`,
|
|
19
|
+
* );
|
|
20
|
+
* // result is `{key-a: ['a'], key-b: ['b']}`
|
|
21
|
+
* ```
|
|
13
22
|
*/
|
|
23
|
+
export declare function groupArrayBy<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Partial<Record<NewKey, ElementType[]>>;
|
|
24
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => Promise<{
|
|
25
|
+
key: NewKey;
|
|
26
|
+
value: NewValue;
|
|
27
|
+
} | undefined>): Promise<Partial<Record<NewKey, NewValue>>>;
|
|
14
28
|
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => {
|
|
15
29
|
key: NewKey;
|
|
16
30
|
value: NewValue;
|
|
17
|
-
}): Partial<Record<NewKey, NewValue>>;
|
|
31
|
+
} | undefined): Partial<Record<NewKey, NewValue>>;
|
|
32
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MaybePromise<{
|
|
33
|
+
key: NewKey;
|
|
34
|
+
value: NewValue;
|
|
35
|
+
} | undefined>): MaybePromise<Partial<Record<NewKey, NewValue>>>;
|
|
@@ -1,10 +1,28 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
import { ensureError } from '@augment-vir/core';
|
|
1
3
|
import { getOrSet } from '../object/get-or-set.js';
|
|
2
4
|
import { typedObjectFromEntries } from '../object/object-entries.js';
|
|
5
|
+
import { filterMap } from './filter.js';
|
|
3
6
|
/**
|
|
4
7
|
* Polyfill for `Object.groupBy`:
|
|
5
|
-
* https://developer.mozilla.org/
|
|
8
|
+
* https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
|
|
6
9
|
*
|
|
7
|
-
* @category
|
|
10
|
+
* @category Array
|
|
11
|
+
* @category Object
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import {arrayToObject} from '@augment-vir/common';
|
|
16
|
+
*
|
|
17
|
+
* const result = arrayToObject(
|
|
18
|
+
* [
|
|
19
|
+
* 'a',
|
|
20
|
+
* 'b',
|
|
21
|
+
* ],
|
|
22
|
+
* (value) => `key-${value}`,
|
|
23
|
+
* );
|
|
24
|
+
* // result is `{key-a: ['a'], key-b: ['b']}`
|
|
25
|
+
* ```
|
|
8
26
|
*/
|
|
9
27
|
export function groupArrayBy(inputArray, callback) {
|
|
10
28
|
return inputArray.reduce((accum, entry, index, originalArray) => {
|
|
@@ -15,17 +33,83 @@ export function groupArrayBy(inputArray, callback) {
|
|
|
15
33
|
}, {});
|
|
16
34
|
}
|
|
17
35
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
36
|
+
* Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
|
|
37
|
+
* `value` outputs from the callback. The resulting object does not have an array of elements
|
|
38
|
+
* (unless the original array itself contains arrays). Automatically handles the case where the
|
|
39
|
+
* callback returns a promise.
|
|
40
|
+
*
|
|
41
|
+
* @category Array
|
|
42
|
+
* @category Object
|
|
43
|
+
* @category Package : @augment-vir/common
|
|
44
|
+
* @example
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* import {arrayToObject} from '@augment-vir/common';
|
|
48
|
+
*
|
|
49
|
+
* const result = arrayToObject(
|
|
50
|
+
* [
|
|
51
|
+
* 'a',
|
|
52
|
+
* 'b',
|
|
53
|
+
* ],
|
|
54
|
+
* (value) => {
|
|
55
|
+
* return {key: `key-${value}`, value};
|
|
56
|
+
* },
|
|
57
|
+
* );
|
|
58
|
+
* // result is `{key-a: 'a', key-b: 'b'}`
|
|
59
|
+
* ```
|
|
20
60
|
*
|
|
21
|
-
* @
|
|
61
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
22
62
|
*/
|
|
23
63
|
export function arrayToObject(inputArray, callback) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
64
|
+
try {
|
|
65
|
+
let gotAPromise = false;
|
|
66
|
+
const mappedEntries = inputArray
|
|
67
|
+
.map((entry, index, originalArray) => {
|
|
68
|
+
const output = callback(entry, index, originalArray);
|
|
69
|
+
if (output instanceof Promise) {
|
|
70
|
+
gotAPromise = true;
|
|
71
|
+
return output;
|
|
72
|
+
}
|
|
73
|
+
else if (output) {
|
|
74
|
+
return [
|
|
75
|
+
output.key,
|
|
76
|
+
output.value,
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.filter(check.isTruthy);
|
|
84
|
+
if (gotAPromise) {
|
|
85
|
+
return new Promise(async (resolve, reject) => {
|
|
86
|
+
try {
|
|
87
|
+
const entries = filterMap(await Promise.all(mappedEntries), (entry) => {
|
|
88
|
+
if (!entry) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
else if (Array.isArray(entry)) {
|
|
92
|
+
return entry;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return [
|
|
96
|
+
entry.key,
|
|
97
|
+
entry.value,
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
}, check.isTruthy);
|
|
101
|
+
resolve(typedObjectFromEntries(entries));
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
reject(ensureError(error));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return typedObjectFromEntries(mappedEntries);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
throw ensureError(error);
|
|
114
|
+
}
|
|
31
115
|
}
|
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs
|
|
3
|
+
* [`[].filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
|
|
4
|
+
* on an array but supports an async callback.
|
|
5
|
+
*
|
|
6
|
+
* @category Array
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {awaitedFilter} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* const result = await awaitedFilter(
|
|
14
|
+
* [
|
|
15
|
+
* 1,
|
|
16
|
+
* 2,
|
|
17
|
+
* 3,
|
|
18
|
+
* 4,
|
|
19
|
+
* 5,
|
|
20
|
+
* ],
|
|
21
|
+
* async (value) => {
|
|
22
|
+
* return await Promise.resolve(value > 2);
|
|
23
|
+
* },
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @returns A new array (does not mutate).
|
|
28
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
29
|
+
*/
|
|
1
30
|
export declare function awaitedFilter<OriginalGeneric>(arrayInput: ReadonlyArray<OriginalGeneric>, filterCallback: (arrayElement: OriginalGeneric, index: number, wholeArray: ReadonlyArray<OriginalGeneric>) => Promise<unknown>, options?: {
|
|
2
31
|
/**
|
|
3
32
|
* Each call to the filter callback is blocking, meaning the next one won't start until the
|
|
@@ -1,4 +1,33 @@
|
|
|
1
1
|
import { awaitedBlockingMap } from './awaited-map.js';
|
|
2
|
+
/**
|
|
3
|
+
* Performs
|
|
4
|
+
* [`[].filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
|
|
5
|
+
* on an array but supports an async callback.
|
|
6
|
+
*
|
|
7
|
+
* @category Array
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {awaitedFilter} from '@augment-vir/common';
|
|
13
|
+
*
|
|
14
|
+
* const result = await awaitedFilter(
|
|
15
|
+
* [
|
|
16
|
+
* 1,
|
|
17
|
+
* 2,
|
|
18
|
+
* 3,
|
|
19
|
+
* 4,
|
|
20
|
+
* 5,
|
|
21
|
+
* ],
|
|
22
|
+
* async (value) => {
|
|
23
|
+
* return await Promise.resolve(value > 2);
|
|
24
|
+
* },
|
|
25
|
+
* );
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @returns A new array (does not mutate).
|
|
29
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
30
|
+
*/
|
|
2
31
|
export async function awaitedFilter(arrayInput, filterCallback, options) {
|
|
3
32
|
const callbackResults = options?.blocking
|
|
4
33
|
? await awaitedBlockingMap(arrayInput, filterCallback)
|
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Performs
|
|
3
|
+
* [`[].forEach()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
|
|
4
|
+
* on an array but supports an async callback. The async callback is blocking. Meaning,
|
|
5
|
+
* `awaitedForEach` will wait for a callback on array element 1 to finish before moving on to array
|
|
6
|
+
* element 2.
|
|
7
|
+
*
|
|
8
|
+
* @category Array
|
|
9
|
+
* @category Package : @augment-vir/common
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import {awaitedForEach} from '@augment-vir/common';
|
|
14
|
+
*
|
|
15
|
+
* await awaitedForEach(
|
|
16
|
+
* [
|
|
17
|
+
* 1,
|
|
18
|
+
* 2,
|
|
19
|
+
* 3,
|
|
20
|
+
* 4,
|
|
21
|
+
* 5,
|
|
22
|
+
* ],
|
|
23
|
+
* async (value) => {
|
|
24
|
+
* await Promise.resolve(value);
|
|
25
|
+
* },
|
|
26
|
+
* );
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
5
30
|
*/
|
|
6
31
|
export declare function awaitedForEach<OriginalGeneric>(input: ReadonlyArray<OriginalGeneric>, callback: (arrayElement: OriginalGeneric, index: number, wholeArray: ReadonlyArray<OriginalGeneric>) => void | PromiseLike<void>): Promise<void>;
|
|
@@ -1,8 +1,33 @@
|
|
|
1
1
|
import { awaitedBlockingMap } from './awaited-map.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Performs
|
|
4
|
+
* [`[].forEach()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
|
|
5
|
+
* on an array but supports an async callback. The async callback is blocking. Meaning,
|
|
6
|
+
* `awaitedForEach` will wait for a callback on array element 1 to finish before moving on to array
|
|
7
|
+
* element 2.
|
|
8
|
+
*
|
|
9
|
+
* @category Array
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {awaitedForEach} from '@augment-vir/common';
|
|
15
|
+
*
|
|
16
|
+
* await awaitedForEach(
|
|
17
|
+
* [
|
|
18
|
+
* 1,
|
|
19
|
+
* 2,
|
|
20
|
+
* 3,
|
|
21
|
+
* 4,
|
|
22
|
+
* 5,
|
|
23
|
+
* ],
|
|
24
|
+
* async (value) => {
|
|
25
|
+
* await Promise.resolve(value);
|
|
26
|
+
* },
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
6
31
|
*/
|
|
7
32
|
export async function awaitedForEach(input, callback) {
|
|
8
33
|
await awaitedBlockingMap(input, callback);
|
|
@@ -1 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs
|
|
3
|
+
* [`[].map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
|
4
|
+
* on an array but supports an async callback. The async callback is blocking. Meaning,
|
|
5
|
+
* `awaitedForEach` will wait for a callback on array element 1 to finish before moving on to array
|
|
6
|
+
* element 2. Compare to `await Promise.all([].map(async () => {}))` which is _not_ blocking (all
|
|
7
|
+
* callbacks are called in parallel).
|
|
8
|
+
*
|
|
9
|
+
* @category Array
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {awaitedBlockingMap} from '@augment-vir/common';
|
|
15
|
+
*
|
|
16
|
+
* const result = await awaitedBlockingMap(
|
|
17
|
+
* [
|
|
18
|
+
* 1,
|
|
19
|
+
* 2,
|
|
20
|
+
* 3,
|
|
21
|
+
* 4,
|
|
22
|
+
* 5,
|
|
23
|
+
* ],
|
|
24
|
+
* async (value) => {
|
|
25
|
+
* return await Promise.resolve(value);
|
|
26
|
+
* },
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @returns A new array (does not mutate).
|
|
31
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
32
|
+
*/
|
|
1
33
|
export declare function awaitedBlockingMap<OriginalGeneric, MappedGeneric>(input: ReadonlyArray<OriginalGeneric>, callback: (arrayElement: OriginalGeneric, index: number, wholeArray: ReadonlyArray<OriginalGeneric>) => MappedGeneric | PromiseLike<MappedGeneric>): Promise<Awaited<MappedGeneric>[]>;
|
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs
|
|
3
|
+
* [`[].map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
|
4
|
+
* on an array but supports an async callback. The async callback is blocking. Meaning,
|
|
5
|
+
* `awaitedForEach` will wait for a callback on array element 1 to finish before moving on to array
|
|
6
|
+
* element 2. Compare to `await Promise.all([].map(async () => {}))` which is _not_ blocking (all
|
|
7
|
+
* callbacks are called in parallel).
|
|
8
|
+
*
|
|
9
|
+
* @category Array
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {awaitedBlockingMap} from '@augment-vir/common';
|
|
15
|
+
*
|
|
16
|
+
* const result = await awaitedBlockingMap(
|
|
17
|
+
* [
|
|
18
|
+
* 1,
|
|
19
|
+
* 2,
|
|
20
|
+
* 3,
|
|
21
|
+
* 4,
|
|
22
|
+
* 5,
|
|
23
|
+
* ],
|
|
24
|
+
* async (value) => {
|
|
25
|
+
* return await Promise.resolve(value);
|
|
26
|
+
* },
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @returns A new array (does not mutate).
|
|
31
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
32
|
+
*/
|
|
1
33
|
export async function awaitedBlockingMap(input, callback) {
|
|
2
34
|
return await input.reduce(async (accumPromise, currentElement, index, wholeArray) => {
|
|
3
35
|
const accum = await accumPromise;
|
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes all given indexes from the given array.
|
|
3
|
+
*
|
|
4
|
+
* @category Array
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {filterOutIndexes} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* const result = filterOutIndexes(
|
|
12
|
+
* [
|
|
13
|
+
* 'a',
|
|
14
|
+
* 'b',
|
|
15
|
+
* '',
|
|
16
|
+
* ],
|
|
17
|
+
* [
|
|
18
|
+
* 0,
|
|
19
|
+
* 2,
|
|
20
|
+
* ],
|
|
21
|
+
* );
|
|
22
|
+
* // result is `['b']`
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @returns A new array (does not mutate).
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
1
28
|
export declare function filterOutIndexes<T>(array: ReadonlyArray<T>, indexes: ReadonlyArray<number>): T[];
|
|
29
|
+
/** Performs `filterMap` with a type guard filter. */
|
|
2
30
|
export declare function filterMap<ElementType, MappedEntry, TypeGuarded extends MappedEntry>(inputArray: ReadonlyArray<ElementType>, mapCallback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MappedEntry, filterCallback: (mappedOutput: MappedEntry, originalEntry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => mappedOutput is TypeGuarded): TypeGuarded[];
|
|
31
|
+
/** Performs a regular `filterMap`. */
|
|
3
32
|
export declare function filterMap<ElementType, MappedEntry>(inputArray: ReadonlyArray<ElementType>, mapCallback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MappedEntry, filterCallback: (mappedOutput: MappedEntry, originalEntry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => boolean): MappedEntry[];
|
|
@@ -1,6 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes all given indexes from the given array.
|
|
3
|
+
*
|
|
4
|
+
* @category Array
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {filterOutIndexes} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* const result = filterOutIndexes(
|
|
12
|
+
* [
|
|
13
|
+
* 'a',
|
|
14
|
+
* 'b',
|
|
15
|
+
* '',
|
|
16
|
+
* ],
|
|
17
|
+
* [
|
|
18
|
+
* 0,
|
|
19
|
+
* 2,
|
|
20
|
+
* ],
|
|
21
|
+
* );
|
|
22
|
+
* // result is `['b']`
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @returns A new array (does not mutate).
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
1
28
|
export function filterOutIndexes(array, indexes) {
|
|
2
29
|
return array.filter((_, index) => !indexes.includes(index));
|
|
3
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Performs
|
|
33
|
+
* [`[].map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
|
34
|
+
* and
|
|
35
|
+
* [`[].filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
|
|
36
|
+
* (in that order) on an array with a single iteration.
|
|
37
|
+
*
|
|
38
|
+
* @category Array
|
|
39
|
+
* @category Package : @augment-vir/common
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* import {filterMap} from '@augment-vir/common';
|
|
44
|
+
*
|
|
45
|
+
* const result = filterMap(
|
|
46
|
+
* [
|
|
47
|
+
* 'a',
|
|
48
|
+
* 'b',
|
|
49
|
+
* '',
|
|
50
|
+
* ],
|
|
51
|
+
* // map callback
|
|
52
|
+
* (value) => {
|
|
53
|
+
* return `value-${value}`;
|
|
54
|
+
* },
|
|
55
|
+
* // filter callback
|
|
56
|
+
* (mappedValue, originalValue) => {
|
|
57
|
+
* return !!originalValue;
|
|
58
|
+
* },
|
|
59
|
+
* );
|
|
60
|
+
* // result is `['value-a', 'value-b']`
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
64
|
+
*/
|
|
4
65
|
export function filterMap(inputArray, mapCallback, filterCallback) {
|
|
5
66
|
return inputArray.reduce((accum, entry, index, originalArray) => {
|
|
6
67
|
const mapOutput = mapCallback(entry, index, originalArray);
|
|
@@ -1 +1,60 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Removes duplicates from an array. Optionally provide a callback for calculating a unique id for
|
|
3
|
+
* entries.
|
|
4
|
+
*
|
|
5
|
+
* @category Array
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example No callback
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {removeDuplicates} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* const result = removeDuplicates([
|
|
13
|
+
* 1,
|
|
14
|
+
* 1,
|
|
15
|
+
* 1,
|
|
16
|
+
* 1,
|
|
17
|
+
* 1,
|
|
18
|
+
* 2,
|
|
19
|
+
* 4,
|
|
20
|
+
* ]);
|
|
21
|
+
* // result is `[1, 2, 4]`
|
|
22
|
+
*
|
|
23
|
+
* const exampleEntry = {id: 5};
|
|
24
|
+
*
|
|
25
|
+
* const result2 = removeDuplicates([
|
|
26
|
+
* {id: 1},
|
|
27
|
+
* // this entry will not get filtered out because it's a new object reference
|
|
28
|
+
* {id: 1},
|
|
29
|
+
* exampleEntry,
|
|
30
|
+
* // this `exampleEntry` will get filtered out because it's the same reference as the one above
|
|
31
|
+
* exampleEntry,
|
|
32
|
+
* {id: 4},
|
|
33
|
+
* ]);
|
|
34
|
+
* // result2 is `[{id: 1}, {id: 1}, exampleEntry, {id: 4}]`
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example With callback
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* import {removeDuplicates} from '@augment-vir/common';
|
|
41
|
+
*
|
|
42
|
+
* const exampleEntry = {id: 5};
|
|
43
|
+
*
|
|
44
|
+
* const result2 = removeDuplicates(
|
|
45
|
+
* [
|
|
46
|
+
* {id: 1},
|
|
47
|
+
* {id: 1},
|
|
48
|
+
* exampleEntry,
|
|
49
|
+
* exampleEntry,
|
|
50
|
+
* {id: 4},
|
|
51
|
+
* ],
|
|
52
|
+
* (entry) => entry.id,
|
|
53
|
+
* );
|
|
54
|
+
* // result2 is `[{id: 1}, exampleEntry, {id: 4}]`
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @returns A new array (does not mutate).
|
|
58
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
59
|
+
*/
|
|
60
|
+
export declare function removeDuplicates<const Entry>(originalArray: ReadonlyArray<Entry>, calculateUniqueId?: (entry: Readonly<Entry>) => unknown): Entry[];
|