@augment-vir/common 31.60.0 → 31.62.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/function/execute-count.d.ts +17 -0
- package/dist/augments/function/execute-count.js +40 -0
- package/dist/augments/object/flatten-object.d.ts +15 -0
- package/dist/augments/object/flatten-object.js +32 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +3 -3
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type MaybePromise, type Tuple } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Executes the given callback a given count of times.
|
|
4
|
+
*
|
|
5
|
+
* @category Function
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {executeCount} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* executeCount(5, (count) => console.log(count)); // will log: 1,2,3,4,5
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
16
|
+
*/
|
|
17
|
+
export declare function executeCount<N extends number, T = void | undefined>(count: N, callback: (currentCount: number, totalCount: number) => T): Extract<T, Promise<any>> extends never ? Tuple<T, N> : Exclude<T, Promise<any>> extends never ? Promise<Tuple<Awaited<T>, N>> : MaybePromise<Tuple<Awaited<T>, N>>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createArray } from '../array/create-array.js';
|
|
2
|
+
/**
|
|
3
|
+
* Executes the given callback a given count of times.
|
|
4
|
+
*
|
|
5
|
+
* @category Function
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {executeCount} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* executeCount(5, (count) => console.log(count)); // will log: 1,2,3,4,5
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
16
|
+
*/
|
|
17
|
+
export function executeCount(count, callback) {
|
|
18
|
+
return createArray(count, (index) => index + 1).reduce((accum, currentCount) => {
|
|
19
|
+
if (accum instanceof Promise) {
|
|
20
|
+
return accum.then(async (awaitedAccum) => {
|
|
21
|
+
const result = await callback(currentCount, count);
|
|
22
|
+
awaitedAccum.push(result);
|
|
23
|
+
return awaitedAccum;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const result = callback(currentCount, count);
|
|
28
|
+
if (result instanceof Promise) {
|
|
29
|
+
return result.then((awaitedResult) => {
|
|
30
|
+
accum.push(awaitedResult);
|
|
31
|
+
return accum;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
accum.push(result);
|
|
36
|
+
return accum;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type AnyObject, type UnknownObject } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* Flattens a nested object into a single-level object.
|
|
4
|
+
*
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {flattenObject} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* flattenObject({a: 'hello', b: {c: 42, d: {e: true, a: 'bye'}}});
|
|
12
|
+
* // {a: 'bye', 'c': 42, 'e': true}
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function flattenObject(originalObject: Readonly<AnyObject>): UnknownObject;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
/**
|
|
3
|
+
* Flattens a nested object into a single-level object.
|
|
4
|
+
*
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {flattenObject} from '@augment-vir/common';
|
|
10
|
+
*
|
|
11
|
+
* flattenObject({a: 'hello', b: {c: 42, d: {e: true, a: 'bye'}}});
|
|
12
|
+
* // {a: 'bye', 'c': 42, 'e': true}
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function flattenObject(originalObject) {
|
|
16
|
+
return Object.fromEntries(flattenObjectToEntries(originalObject));
|
|
17
|
+
}
|
|
18
|
+
function flattenObjectToEntries(originalObject) {
|
|
19
|
+
return Object.entries(originalObject).flatMap(([key, value,]) => {
|
|
20
|
+
if (check.isObject(value)) {
|
|
21
|
+
return flattenObjectToEntries(value);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return [
|
|
25
|
+
[
|
|
26
|
+
key,
|
|
27
|
+
value,
|
|
28
|
+
],
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export * from './augments/enum/enum-value-check.js';
|
|
|
19
19
|
export * from './augments/error/combine-errors.js';
|
|
20
20
|
export * from './augments/function/call-asynchronously.js';
|
|
21
21
|
export * from './augments/function/debounce.js';
|
|
22
|
+
export * from './augments/function/execute-count.js';
|
|
22
23
|
export * from './augments/function/execution-duration.js';
|
|
23
24
|
export * from './augments/function/if-truthy.js';
|
|
24
25
|
export * from './augments/function/retry.js';
|
|
@@ -47,6 +48,7 @@ export * from './augments/object/deep-copy.js';
|
|
|
47
48
|
export * from './augments/object/deep-value.js';
|
|
48
49
|
export * from './augments/object/diff.js';
|
|
49
50
|
export * from './augments/object/empty.js';
|
|
51
|
+
export * from './augments/object/flatten-object.js';
|
|
50
52
|
export * from './augments/object/get-or-set.js';
|
|
51
53
|
export * from './augments/object/key-count.js';
|
|
52
54
|
export * from './augments/object/map-entries.js';
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ export * from './augments/enum/enum-value-check.js';
|
|
|
19
19
|
export * from './augments/error/combine-errors.js';
|
|
20
20
|
export * from './augments/function/call-asynchronously.js';
|
|
21
21
|
export * from './augments/function/debounce.js';
|
|
22
|
+
export * from './augments/function/execute-count.js';
|
|
22
23
|
export * from './augments/function/execution-duration.js';
|
|
23
24
|
export * from './augments/function/if-truthy.js';
|
|
24
25
|
export * from './augments/function/retry.js';
|
|
@@ -47,6 +48,7 @@ export * from './augments/object/deep-copy.js';
|
|
|
47
48
|
export * from './augments/object/deep-value.js';
|
|
48
49
|
export * from './augments/object/diff.js';
|
|
49
50
|
export * from './augments/object/empty.js';
|
|
51
|
+
export * from './augments/object/flatten-object.js';
|
|
50
52
|
export * from './augments/object/get-or-set.js';
|
|
51
53
|
export * from './augments/object/key-count.js';
|
|
52
54
|
export * from './augments/object/map-entries.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.62.0",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"test:web": "virmator --no-deps test web"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@augment-vir/assert": "^31.
|
|
44
|
-
"@augment-vir/core": "^31.
|
|
43
|
+
"@augment-vir/assert": "^31.62.0",
|
|
44
|
+
"@augment-vir/core": "^31.62.0",
|
|
45
45
|
"@date-vir/duration": "^8.1.0",
|
|
46
46
|
"ansi-styles": "^6.2.3",
|
|
47
47
|
"deepcopy-esm": "^2.1.1",
|