@augment-vir/common 31.25.0 → 31.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/augments/array/array-to-object.d.ts +120 -9
- package/dist/augments/array/array-to-object.js +12 -4
- package/dist/augments/array/extract-duplicates.d.ts +25 -0
- package/dist/augments/array/extract-duplicates.js +43 -0
- package/dist/augments/object/object-sort.d.ts +8 -1
- package/dist/augments/object/object-sort.js +7 -0
- package/dist/augments/promise/timed-promise.js +4 -1
- package/dist/augments/type/boolean.d.ts +9 -0
- package/dist/augments/type/boolean.js +1 -0
- package/dist/augments/type/partial.d.ts +8 -0
- package/dist/augments/type/partial.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/package.json +5 -5
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { type MaybePromise } from '@augment-vir/core';
|
|
2
|
+
import { type InverseBoolean } from '../type/boolean.js';
|
|
3
|
+
import { type MakePartial } from '../type/partial.js';
|
|
2
4
|
/**
|
|
3
5
|
* Polyfill for `Object.groupBy`:
|
|
4
6
|
* https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
|
|
5
7
|
*
|
|
6
8
|
* @category Array
|
|
7
9
|
* @category Object
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
8
11
|
* @example
|
|
9
12
|
*
|
|
10
13
|
* ```ts
|
|
11
|
-
* import {
|
|
14
|
+
* import {groupArrayBy} from '@augment-vir/common';
|
|
12
15
|
*
|
|
13
|
-
* const result =
|
|
16
|
+
* const result = groupArrayBy(
|
|
14
17
|
* [
|
|
15
18
|
* 'a',
|
|
16
19
|
* 'b',
|
|
@@ -19,17 +22,125 @@ import { type MaybePromise } from '@augment-vir/core';
|
|
|
19
22
|
* );
|
|
20
23
|
* // result is `{key-a: ['a'], key-b: ['b']}`
|
|
21
24
|
* ```
|
|
25
|
+
*
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
28
|
+
export declare function groupArrayBy<ElementType, NewKey extends PropertyKey, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey, options?: ArrayToObjectOptions<UseRequired>): MakePartial<Record<NewKey, ElementType[]>, InverseBoolean<UseRequired>>;
|
|
29
|
+
/**
|
|
30
|
+
* Options for {@link groupArrayBy} and {@link arrayToObject}.
|
|
31
|
+
*
|
|
32
|
+
* @category Array
|
|
33
|
+
* @category Object
|
|
34
|
+
* @category Package : @augment-vir/common
|
|
35
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
22
36
|
*/
|
|
23
|
-
export
|
|
24
|
-
|
|
37
|
+
export type ArrayToObjectOptions<UseRequired extends boolean | undefined> = Partial<{
|
|
38
|
+
/**
|
|
39
|
+
* Set to `true` to make the object return type not use `Partial<>`.
|
|
40
|
+
*
|
|
41
|
+
* @default false
|
|
42
|
+
*/
|
|
43
|
+
useRequired: UseRequired;
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
|
|
47
|
+
* `value` outputs from the callback. The resulting object does not have an array of elements
|
|
48
|
+
* (unless the original array itself contains arrays). Automatically handles the case where the
|
|
49
|
+
* callback returns a promise.
|
|
50
|
+
*
|
|
51
|
+
* @category Array
|
|
52
|
+
* @category Object
|
|
53
|
+
* @category Package : @augment-vir/common
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* import {arrayToObject} from '@augment-vir/common';
|
|
58
|
+
*
|
|
59
|
+
* const result = arrayToObject(
|
|
60
|
+
* [
|
|
61
|
+
* 'a',
|
|
62
|
+
* 'b',
|
|
63
|
+
* ],
|
|
64
|
+
* (value) => {
|
|
65
|
+
* return {key: `key-${value}`, value};
|
|
66
|
+
* },
|
|
67
|
+
* );
|
|
68
|
+
* // result is `{key-a: 'a', key-b: 'b'}`
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
72
|
+
*/
|
|
73
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => Promise<{
|
|
25
74
|
key: NewKey;
|
|
26
75
|
value: NewValue;
|
|
27
|
-
} | undefined
|
|
28
|
-
|
|
76
|
+
} | undefined>,
|
|
77
|
+
/** Optional. */
|
|
78
|
+
options?: ArrayToObjectOptions<UseRequired>): Promise<MakePartial<Record<NewKey, NewValue>, InverseBoolean<UseRequired>>>;
|
|
79
|
+
/**
|
|
80
|
+
* Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
|
|
81
|
+
* `value` outputs from the callback. The resulting object does not have an array of elements
|
|
82
|
+
* (unless the original array itself contains arrays). Automatically handles the case where the
|
|
83
|
+
* callback returns a promise.
|
|
84
|
+
*
|
|
85
|
+
* @category Array
|
|
86
|
+
* @category Object
|
|
87
|
+
* @category Package : @augment-vir/common
|
|
88
|
+
* @example
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* import {arrayToObject} from '@augment-vir/common';
|
|
92
|
+
*
|
|
93
|
+
* const result = arrayToObject(
|
|
94
|
+
* [
|
|
95
|
+
* 'a',
|
|
96
|
+
* 'b',
|
|
97
|
+
* ],
|
|
98
|
+
* (value) => {
|
|
99
|
+
* return {key: `key-${value}`, value};
|
|
100
|
+
* },
|
|
101
|
+
* );
|
|
102
|
+
* // result is `{key-a: 'a', key-b: 'b'}`
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
106
|
+
*/
|
|
107
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => {
|
|
29
108
|
key: NewKey;
|
|
30
109
|
value: NewValue;
|
|
31
|
-
} | undefined
|
|
32
|
-
|
|
110
|
+
} | undefined,
|
|
111
|
+
/** Optional. */
|
|
112
|
+
options?: ArrayToObjectOptions<UseRequired>): MakePartial<Record<NewKey, NewValue>, InverseBoolean<UseRequired>>;
|
|
113
|
+
/**
|
|
114
|
+
* Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
|
|
115
|
+
* `value` outputs from the callback. The resulting object does not have an array of elements
|
|
116
|
+
* (unless the original array itself contains arrays). Automatically handles the case where the
|
|
117
|
+
* callback returns a promise.
|
|
118
|
+
*
|
|
119
|
+
* @category Array
|
|
120
|
+
* @category Object
|
|
121
|
+
* @category Package : @augment-vir/common
|
|
122
|
+
* @example
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* import {arrayToObject} from '@augment-vir/common';
|
|
126
|
+
*
|
|
127
|
+
* const result = arrayToObject(
|
|
128
|
+
* [
|
|
129
|
+
* 'a',
|
|
130
|
+
* 'b',
|
|
131
|
+
* ],
|
|
132
|
+
* (value) => {
|
|
133
|
+
* return {key: `key-${value}`, value};
|
|
134
|
+
* },
|
|
135
|
+
* );
|
|
136
|
+
* // result is `{key-a: 'a', key-b: 'b'}`
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
140
|
+
*/
|
|
141
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MaybePromise<{
|
|
33
142
|
key: NewKey;
|
|
34
143
|
value: NewValue;
|
|
35
|
-
} | undefined
|
|
144
|
+
} | undefined>,
|
|
145
|
+
/** Optional. */
|
|
146
|
+
options?: ArrayToObjectOptions<UseRequired>): MaybePromise<MakePartial<Record<NewKey, NewValue>, InverseBoolean<UseRequired>>>;
|
|
@@ -9,12 +9,13 @@ import { filterMap } from './filter.js';
|
|
|
9
9
|
*
|
|
10
10
|
* @category Array
|
|
11
11
|
* @category Object
|
|
12
|
+
* @category Package : @augment-vir/common
|
|
12
13
|
* @example
|
|
13
14
|
*
|
|
14
15
|
* ```ts
|
|
15
|
-
* import {
|
|
16
|
+
* import {groupArrayBy} from '@augment-vir/common';
|
|
16
17
|
*
|
|
17
|
-
* const result =
|
|
18
|
+
* const result = groupArrayBy(
|
|
18
19
|
* [
|
|
19
20
|
* 'a',
|
|
20
21
|
* 'b',
|
|
@@ -23,8 +24,12 @@ import { filterMap } from './filter.js';
|
|
|
23
24
|
* );
|
|
24
25
|
* // result is `{key-a: ['a'], key-b: ['b']}`
|
|
25
26
|
* ```
|
|
27
|
+
*
|
|
28
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
26
29
|
*/
|
|
27
|
-
export function groupArrayBy(inputArray, callback
|
|
30
|
+
export function groupArrayBy(inputArray, callback,
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
32
|
+
options = {}) {
|
|
28
33
|
return inputArray.reduce((accum, entry, index, originalArray) => {
|
|
29
34
|
const key = callback(entry, index, originalArray);
|
|
30
35
|
const entryArray = getOrSet(accum, key, () => []);
|
|
@@ -60,7 +65,10 @@ export function groupArrayBy(inputArray, callback) {
|
|
|
60
65
|
*
|
|
61
66
|
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
62
67
|
*/
|
|
63
|
-
export function arrayToObject(inputArray, callback
|
|
68
|
+
export function arrayToObject(inputArray, callback,
|
|
69
|
+
/** Optional. */
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
71
|
+
options = {}) {
|
|
64
72
|
try {
|
|
65
73
|
let gotAPromise = false;
|
|
66
74
|
const mappedEntries = inputArray
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds all duplicates.
|
|
3
|
+
*
|
|
4
|
+
* @category Array
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {extractDuplicates} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* const result = extractDuplicates([
|
|
12
|
+
* 'a',
|
|
13
|
+
* 'b',
|
|
14
|
+
* '',
|
|
15
|
+
* 'a',
|
|
16
|
+
* ]);
|
|
17
|
+
* // result is `{duplicates: ['a'], uniques: ['b', '']}`
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
21
|
+
*/
|
|
22
|
+
export declare function extractDuplicates<T>(items: ReadonlyArray<T>, comparator?: (a: T, b: T) => boolean): {
|
|
23
|
+
duplicates: T[];
|
|
24
|
+
uniques: T[];
|
|
25
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
/**
|
|
3
|
+
* Finds all duplicates.
|
|
4
|
+
*
|
|
5
|
+
* @category Array
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {extractDuplicates} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* const result = extractDuplicates([
|
|
13
|
+
* 'a',
|
|
14
|
+
* 'b',
|
|
15
|
+
* '',
|
|
16
|
+
* 'a',
|
|
17
|
+
* ]);
|
|
18
|
+
* // result is `{duplicates: ['a'], uniques: ['b', '']}`
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
22
|
+
*/
|
|
23
|
+
export function extractDuplicates(items, comparator = check.strictEquals) {
|
|
24
|
+
const duplicates = [];
|
|
25
|
+
const uniques = [];
|
|
26
|
+
items.forEach((item) => {
|
|
27
|
+
const duplicatesIndex = duplicates.findIndex((duplicateEntry) => comparator(duplicateEntry, item));
|
|
28
|
+
if (duplicatesIndex < 0) {
|
|
29
|
+
const uniquesIndex = uniques.findIndex((uniqueEntry) => comparator(uniqueEntry, item));
|
|
30
|
+
if (uniquesIndex < 0) {
|
|
31
|
+
uniques.push(item);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
uniques.splice(uniquesIndex, 1);
|
|
35
|
+
duplicates.push(item);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
duplicates,
|
|
41
|
+
uniques,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import { type AnyObject } from '@augment-vir/core';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates as new sorted object copied from the the original given object.
|
|
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 declare function sortObject<const T extends AnyObject>(original: Readonly<T>): T;
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates as new sorted object copied from the the original given object.
|
|
3
|
+
*
|
|
4
|
+
* @category Object
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export function sortObject(original) {
|
|
2
9
|
return Object.fromEntries(Object.entries(original).sort((a, b) => a[0].localeCompare(b[0])));
|
|
3
10
|
}
|
|
@@ -30,7 +30,10 @@ export class PromiseTimeoutError extends Error {
|
|
|
30
30
|
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
31
31
|
*/
|
|
32
32
|
export function wrapPromiseInTimeout(duration, originalPromise, failureMessage) {
|
|
33
|
-
const
|
|
33
|
+
const isInfinity = Object.values(duration).some((value) => value && check.isInfinite(value));
|
|
34
|
+
const milliseconds = isInfinity
|
|
35
|
+
? Infinity
|
|
36
|
+
: convertDuration(duration, { milliseconds: true }).milliseconds;
|
|
34
37
|
return new Promise(async (resolve, reject) => {
|
|
35
38
|
const timeoutId = milliseconds === Infinity
|
|
36
39
|
? undefined
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reverses a boolean (optionally `undefined`). `true` becomes `false`. `false` and `undefined`
|
|
3
|
+
* become `true`.
|
|
4
|
+
*
|
|
5
|
+
* @category Type
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
9
|
+
export type InverseBoolean<Input extends boolean | undefined> = Input extends true ? false : true;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets `T` as partial if `ShouldBePartial` is `true`. Otherwise, `T` is left alone.
|
|
3
|
+
*
|
|
4
|
+
* @category Type
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export type MakePartial<T extends object, ShouldBePartial extends boolean> = ShouldBePartial extends true ? Partial<T> : T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './augments/array/awaited/awaited-filter.js';
|
|
|
5
5
|
export * from './augments/array/awaited/awaited-for-each.js';
|
|
6
6
|
export * from './augments/array/awaited/awaited-map.js';
|
|
7
7
|
export * from './augments/array/create-array.js';
|
|
8
|
+
export * from './augments/array/extract-duplicates.js';
|
|
8
9
|
export * from './augments/array/filter.js';
|
|
9
10
|
export * from './augments/array/remove-duplicates.js';
|
|
10
11
|
export * from './augments/array/repeat-array.js';
|
|
@@ -84,7 +85,9 @@ export * from './augments/string/substring-index.js';
|
|
|
84
85
|
export * from './augments/string/suffix.js';
|
|
85
86
|
export * from './augments/string/white-space.js';
|
|
86
87
|
export * from './augments/string/wrap-string.js';
|
|
88
|
+
export * from './augments/type/boolean.js';
|
|
87
89
|
export * from './augments/type/ensure-type.js';
|
|
90
|
+
export * from './augments/type/partial.js';
|
|
88
91
|
export * from './augments/type/readonly.js';
|
|
89
92
|
export * from './augments/type/type-recursion.js';
|
|
90
93
|
export * from './augments/type/union.js';
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from './augments/array/awaited/awaited-filter.js';
|
|
|
5
5
|
export * from './augments/array/awaited/awaited-for-each.js';
|
|
6
6
|
export * from './augments/array/awaited/awaited-map.js';
|
|
7
7
|
export * from './augments/array/create-array.js';
|
|
8
|
+
export * from './augments/array/extract-duplicates.js';
|
|
8
9
|
export * from './augments/array/filter.js';
|
|
9
10
|
export * from './augments/array/remove-duplicates.js';
|
|
10
11
|
export * from './augments/array/repeat-array.js';
|
|
@@ -84,7 +85,9 @@ export * from './augments/string/substring-index.js';
|
|
|
84
85
|
export * from './augments/string/suffix.js';
|
|
85
86
|
export * from './augments/string/white-space.js';
|
|
86
87
|
export * from './augments/string/wrap-string.js';
|
|
88
|
+
export * from './augments/type/boolean.js';
|
|
87
89
|
export * from './augments/type/ensure-type.js';
|
|
90
|
+
export * from './augments/type/partial.js';
|
|
88
91
|
export * from './augments/type/readonly.js';
|
|
89
92
|
export * from './augments/type/type-recursion.js';
|
|
90
93
|
export * from './augments/type/union.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.27.0",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"test:web": "virmator --no-deps test web"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@augment-vir/assert": "^31.
|
|
44
|
-
"@augment-vir/core": "^31.
|
|
45
|
-
"@date-vir/duration": "^7.3.
|
|
43
|
+
"@augment-vir/assert": "^31.27.0",
|
|
44
|
+
"@augment-vir/core": "^31.27.0",
|
|
45
|
+
"@date-vir/duration": "^7.3.2",
|
|
46
46
|
"ansi-styles": "^6.2.1",
|
|
47
47
|
"deepcopy-esm": "^2.1.1",
|
|
48
48
|
"json5": "^2.2.3",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"@web/test-runner-commands": "^0.9.0",
|
|
56
56
|
"@web/test-runner-playwright": "^0.11.1",
|
|
57
57
|
"@web/test-runner-visual-regression": "^0.10.0",
|
|
58
|
-
"concurrently": "^9.
|
|
58
|
+
"concurrently": "^9.2.0",
|
|
59
59
|
"execute-in-browser": "^1.0.7",
|
|
60
60
|
"istanbul-smart-text-reporter": "^1.1.5",
|
|
61
61
|
"typescript": "^5.8.3"
|