@cognite/underscore 1.0.1 → 1.48535.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/README.md +2 -0
- package/dist/arrays.d.ts +31 -31
- package/dist/arrays.js +53 -18
- package/dist/arrays.js.map +1 -0
- package/dist/assert.d.ts +6 -6
- package/dist/assert.js +12 -5
- package/dist/assert.js.map +1 -0
- package/dist/brand.d.ts +41 -38
- package/dist/brand.js +39 -0
- package/dist/brand.js.map +1 -0
- package/dist/chunk-and-process-in-parallel.d.ts +112 -106
- package/dist/chunk-and-process-in-parallel.js +138 -29
- package/dist/chunk-and-process-in-parallel.js.map +1 -0
- package/dist/chunk-array.d.ts +25 -25
- package/dist/chunk-array.js +34 -6
- package/dist/chunk-array.js.map +1 -0
- package/dist/class.d.ts +8 -8
- package/dist/class.js +11 -5
- package/dist/class.js.map +1 -0
- package/dist/exhaustive-guard.d.ts +15 -15
- package/dist/exhaustive-guard.js +19 -5
- package/dist/exhaustive-guard.js.map +1 -0
- package/dist/exponential-backoff-with-jitter.d.ts +13 -13
- package/dist/exponential-backoff-with-jitter.js +23 -7
- package/dist/exponential-backoff-with-jitter.js.map +1 -0
- package/dist/generate-unique-name.d.ts +17 -17
- package/dist/generate-unique-name.js +46 -10
- package/dist/generate-unique-name.js.map +1 -0
- package/dist/get-numbers-between.d.ts +15 -15
- package/dist/get-numbers-between.js +21 -5
- package/dist/get-numbers-between.js.map +1 -0
- package/dist/humanize-value.d.ts +3 -3
- package/dist/humanize-value.js +33 -15
- package/dist/humanize-value.js.map +1 -0
- package/dist/index.d.ts +23 -23
- package/dist/index.js +23 -23
- package/dist/index.js.map +1 -0
- package/dist/is-function.d.ts +5 -5
- package/dist/is-function.js +9 -5
- package/dist/is-function.js.map +1 -0
- package/dist/is-primitive.d.ts +7 -7
- package/dist/is-primitive.js +16 -6
- package/dist/is-primitive.js.map +1 -0
- package/dist/is-valid-url.d.ts +11 -14
- package/dist/is-valid-url.js +39 -16
- package/dist/is-valid-url.js.map +1 -0
- package/dist/iterators.d.ts +7 -7
- package/dist/iterators.js +16 -6
- package/dist/iterators.js.map +1 -0
- package/dist/math.d.ts +10 -10
- package/dist/math.js +16 -7
- package/dist/math.js.map +1 -0
- package/dist/random.d.ts +24 -24
- package/dist/random.js +52 -18
- package/dist/random.js.map +1 -0
- package/dist/react/use-debounce.d.ts +21 -21
- package/dist/react/use-debounce.js +39 -14
- package/dist/react/use-debounce.js.map +1 -0
- package/dist/react/use-derived-promise.d.ts +28 -29
- package/dist/react/use-derived-promise.js +70 -33
- package/dist/react/use-derived-promise.js.map +1 -0
- package/dist/react/use-stable-sync-external-store.d.ts +25 -25
- package/dist/react/use-stable-sync-external-store.js +52 -11
- package/dist/react/use-stable-sync-external-store.js.map +1 -0
- package/dist/react.d.ts +3 -3
- package/dist/react.js +5 -4
- package/dist/react.js.map +1 -0
- package/dist/repeat.d.ts +5 -5
- package/dist/repeat.js +11 -5
- package/dist/repeat.js.map +1 -0
- package/dist/retry-async.d.ts +87 -87
- package/dist/retry-async.js +88 -17
- package/dist/retry-async.js.map +1 -0
- package/dist/sleep.d.ts +5 -5
- package/dist/sleep.js +9 -5
- package/dist/sleep.js.map +1 -0
- package/dist/string.d.ts +35 -35
- package/dist/string.js +48 -9
- package/dist/string.js.map +1 -0
- package/dist/structMap/index.d.ts +3 -3
- package/dist/structMap/index.js +4 -0
- package/dist/structMap/index.js.map +1 -0
- package/dist/structMap/stableStringify.d.ts +21 -21
- package/dist/structMap/stableStringify.js +69 -22
- package/dist/structMap/stableStringify.js.map +1 -0
- package/dist/structMap/structMap.d.ts +111 -108
- package/dist/structMap/structMap.js +131 -28
- package/dist/structMap/structMap.js.map +1 -0
- package/dist/structMap/testUtils.d.ts +8 -8
- package/dist/structMap/testUtils.js +26 -0
- package/dist/structMap/testUtils.js.map +1 -0
- package/dist/structMap/types.d.ts +4 -4
- package/dist/structMap/types.js +5 -0
- package/dist/structMap/types.js.map +1 -0
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -15,11 +15,13 @@ See the collection of functions [here](./docs/modules.md).
|
|
|
15
15
|
## Importing
|
|
16
16
|
|
|
17
17
|
The library has two modules. Non-React functions is imported the following way:
|
|
18
|
+
|
|
18
19
|
```ts
|
|
19
20
|
import { someFunction } from '@cognite/underscore';
|
|
20
21
|
```
|
|
21
22
|
|
|
22
23
|
But React functions are imported the following way:
|
|
24
|
+
|
|
23
25
|
```ts
|
|
24
26
|
import { someFunction } from '@cognite/underscore/react';
|
|
25
27
|
```
|
package/dist/arrays.d.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
* Inserts an element at a specific index in an array.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of elements in the array.
|
|
5
|
+
* @param {T[]} array - The array.
|
|
6
|
+
* @param {number} index - The index at which to insert the element.
|
|
7
|
+
* @param {T} item - The element to insert.
|
|
8
|
+
*/
|
|
9
9
|
export declare function insertAt<T>(array: T[], index: number, item: T): void;
|
|
10
10
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
* Removes an element from an array, but only removed the first occurrence.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The type of elements in the array.
|
|
14
|
+
* @param {T[]} array - The array.
|
|
15
|
+
* @param {T} item - The element to remove.
|
|
16
|
+
*/
|
|
17
17
|
export declare function remove<T>(array: T[], item: T): void;
|
|
18
18
|
/**
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
* Removes an element by the index from an array.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The type of elements in the array.
|
|
22
|
+
* @param {T[]} array - The array.
|
|
23
|
+
* @param {number} index - The index of the element to remove.
|
|
24
|
+
*/
|
|
25
25
|
export declare function removeAt<T>(array: T[], index: number): void;
|
|
26
26
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
* Swaps two elements in an array at index `i` and `j`.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The type of elements in the array.
|
|
30
|
+
* @param {T[]} array - The array.
|
|
31
|
+
* @param {number} i - The index of the first element.
|
|
32
|
+
* @param {number} j - The index of the second element.
|
|
33
|
+
*/
|
|
34
34
|
export declare function swap<T>(array: T[], i: number, j: number): void;
|
|
35
35
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
* Shuffles the elements of the given array using the Fisher-Yates algorithm.
|
|
37
|
+
*
|
|
38
|
+
* @template T - The type of elements in the array.
|
|
39
|
+
* @param {T[]} array - The array.
|
|
40
|
+
*/
|
|
41
41
|
export declare function shuffle<T>(array: T[]): void;
|
package/dist/arrays.js
CHANGED
|
@@ -1,23 +1,58 @@
|
|
|
1
|
-
import { randomInteger
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { randomInteger } from "./random.js";
|
|
2
|
+
/**
|
|
3
|
+
* Inserts an element at a specific index in an array.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of elements in the array.
|
|
6
|
+
* @param {T[]} array - The array.
|
|
7
|
+
* @param {number} index - The index at which to insert the element.
|
|
8
|
+
* @param {T} item - The element to insert.
|
|
9
|
+
*/ export function insertAt(array, index, item) {
|
|
10
|
+
array.splice(index, 0, item);
|
|
5
11
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Removes an element from an array, but only removed the first occurrence.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of elements in the array.
|
|
16
|
+
* @param {T[]} array - The array.
|
|
17
|
+
* @param {T} item - The element to remove.
|
|
18
|
+
*/ export function remove(array, item) {
|
|
19
|
+
const index = array.indexOf(item);
|
|
20
|
+
if (index !== -1) {
|
|
21
|
+
removeAt(array, index);
|
|
22
|
+
}
|
|
9
23
|
}
|
|
10
|
-
|
|
11
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Removes an element by the index from an array.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The type of elements in the array.
|
|
28
|
+
* @param {T[]} array - The array.
|
|
29
|
+
* @param {number} index - The index of the element to remove.
|
|
30
|
+
*/ export function removeAt(array, index) {
|
|
31
|
+
array.splice(index, 1);
|
|
12
32
|
}
|
|
13
|
-
|
|
14
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Swaps two elements in an array at index `i` and `j`.
|
|
35
|
+
*
|
|
36
|
+
* @template T - The type of elements in the array.
|
|
37
|
+
* @param {T[]} array - The array.
|
|
38
|
+
* @param {number} i - The index of the first element.
|
|
39
|
+
* @param {number} j - The index of the second element.
|
|
40
|
+
*/ export function swap(array, i, j) {
|
|
41
|
+
[array[i], array[j]] = [
|
|
42
|
+
array[j],
|
|
43
|
+
array[i]
|
|
44
|
+
];
|
|
15
45
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Shuffles the elements of the given array using the Fisher-Yates algorithm.
|
|
48
|
+
*
|
|
49
|
+
* @template T - The type of elements in the array.
|
|
50
|
+
* @param {T[]} array - The array.
|
|
51
|
+
*/ export function shuffle(array) {
|
|
52
|
+
for(let i = array.length - 1; i >= 0; i--){
|
|
53
|
+
const j = randomInteger(0, i + 1);
|
|
54
|
+
swap(array, i, j);
|
|
55
|
+
}
|
|
21
56
|
}
|
|
22
|
-
|
|
23
|
-
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=arrays.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/arrays.ts"],"sourcesContent":["import { randomInteger } from './random';\n\n/**\n * Inserts an element at a specific index in an array.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} array - The array.\n * @param {number} index - The index at which to insert the element.\n * @param {T} item - The element to insert.\n */\nexport function insertAt<T>(array: T[], index: number, item: T): void {\n array.splice(index, 0, item);\n}\n\n/**\n * Removes an element from an array, but only removed the first occurrence.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} array - The array.\n * @param {T} item - The element to remove.\n */\nexport function remove<T>(array: T[], item: T): void {\n const index = array.indexOf(item);\n if (index !== -1) {\n removeAt(array, index);\n }\n}\n\n/**\n * Removes an element by the index from an array.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} array - The array.\n * @param {number} index - The index of the element to remove.\n */\nexport function removeAt<T>(array: T[], index: number): void {\n array.splice(index, 1);\n}\n\n/**\n * Swaps two elements in an array at index `i` and `j`.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} array - The array.\n * @param {number} i - The index of the first element.\n * @param {number} j - The index of the second element.\n */\nexport function swap<T>(array: T[], i: number, j: number): void {\n [array[i], array[j]] = [array[j], array[i]];\n}\n\n/**\n * Shuffles the elements of the given array using the Fisher-Yates algorithm.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} array - The array.\n */\nexport function shuffle<T>(array: T[]): void {\n for (let i = array.length - 1; i >= 0; i--) {\n const j = randomInteger(0, i + 1);\n swap(array, i, j);\n }\n}\n"],"names":["randomInteger","insertAt","array","index","item","splice","remove","indexOf","removeAt","swap","i","j","shuffle","length"],"mappings":"AAAA,SAASA,aAAa,QAAQ,cAAW;AAEzC;;;;;;;CAOC,GACD,OAAO,SAASC,SAAYC,KAAU,EAAEC,KAAa,EAAEC,IAAO;IAC5DF,MAAMG,MAAM,CAACF,OAAO,GAAGC;AACzB;AAEA;;;;;;CAMC,GACD,OAAO,SAASE,OAAUJ,KAAU,EAAEE,IAAO;IAC3C,MAAMD,QAAQD,MAAMK,OAAO,CAACH;IAC5B,IAAID,UAAU,CAAC,GAAG;QAChBK,SAASN,OAAOC;IAClB;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASK,SAAYN,KAAU,EAAEC,KAAa;IACnDD,MAAMG,MAAM,CAACF,OAAO;AACtB;AAEA;;;;;;;CAOC,GACD,OAAO,SAASM,KAAQP,KAAU,EAAEQ,CAAS,EAAEC,CAAS;IACtD,CAACT,KAAK,CAACQ,EAAE,EAAER,KAAK,CAACS,EAAE,CAAC,GAAG;QAACT,KAAK,CAACS,EAAE;QAAET,KAAK,CAACQ,EAAE;KAAC;AAC7C;AAEA;;;;;CAKC,GACD,OAAO,SAASE,QAAWV,KAAU;IACnC,IAAK,IAAIQ,IAAIR,MAAMW,MAAM,GAAG,GAAGH,KAAK,GAAGA,IAAK;QAC1C,MAAMC,IAAIX,cAAc,GAAGU,IAAI;QAC/BD,KAAKP,OAAOQ,GAAGC;IACjB;AACF"}
|
package/dist/assert.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
* Whatever gets passed into the condition parameter must be true if the assert returns. That means that for the rest of the scope, that condition must be truthy from the perspective of the type checker.
|
|
3
|
+
* Adapted from {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions}
|
|
4
|
+
*
|
|
5
|
+
* @param condition - A condition which is guaranteed to be true for the rest of the calling scope after this function returns.
|
|
6
|
+
* @param msg - The message which should be included in the error given that the condition fails.
|
|
7
|
+
*/
|
|
8
8
|
export declare function assert(condition: unknown, msg?: string): asserts condition;
|
package/dist/assert.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Whatever gets passed into the condition parameter must be true if the assert returns. That means that for the rest of the scope, that condition must be truthy from the perspective of the type checker.
|
|
3
|
+
* Adapted from {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions}
|
|
4
|
+
*
|
|
5
|
+
* @param condition - A condition which is guaranteed to be true for the rest of the calling scope after this function returns.
|
|
6
|
+
* @param msg - The message which should be included in the error given that the condition fails.
|
|
7
|
+
*/ export function assert(condition, msg) {
|
|
8
|
+
if (!Boolean(condition)) {
|
|
9
|
+
throw new Error(msg);
|
|
10
|
+
}
|
|
4
11
|
}
|
|
5
|
-
|
|
6
|
-
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assert.ts"],"sourcesContent":["/**\n * Whatever gets passed into the condition parameter must be true if the assert returns. That means that for the rest of the scope, that condition must be truthy from the perspective of the type checker.\n * Adapted from {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions}\n *\n * @param condition - A condition which is guaranteed to be true for the rest of the calling scope after this function returns.\n * @param msg - The message which should be included in the error given that the condition fails.\n */\nexport function assert(condition: unknown, msg?: string): asserts condition {\n if (!Boolean(condition)) {\n throw new Error(msg);\n }\n}\n"],"names":["assert","condition","msg","Boolean","Error"],"mappings":"AAAA;;;;;;CAMC,GACD,OAAO,SAASA,OAAOC,SAAkB,EAAEC,GAAY;IACrD,IAAI,CAACC,QAAQF,YAAY;QACvB,MAAM,IAAIG,MAAMF;IAClB;AACF"}
|
package/dist/brand.d.ts
CHANGED
|
@@ -1,42 +1,45 @@
|
|
|
1
1
|
declare const brand: unique symbol;
|
|
2
2
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
export type Brand<
|
|
40
|
-
|
|
3
|
+
* Adds a compile-time-only "brand" to a base type, producing a nominal type.
|
|
4
|
+
* TypeScript's structural typing normally treats any two types with the same shape as
|
|
5
|
+
* interchangeable, so a `UserId` and an `OrderId` that are both `string` can be passed to each
|
|
6
|
+
* other's functions by mistake.
|
|
7
|
+
* `Brand` closes that hole by attaching a unique, unused property to the type, so values of
|
|
8
|
+
* different brands are no longer assignable to one another even though they share a base type.
|
|
9
|
+
* The branded property does not exist at runtime — it is erased during compilation and only
|
|
10
|
+
* affects type checking.
|
|
11
|
+
*
|
|
12
|
+
* Declare a custom branded type by aliasing `Brand` with a base type and a unique name:
|
|
13
|
+
* ```ts
|
|
14
|
+
* type UserId = Brand<string, 'UserId'>;
|
|
15
|
+
* type OrderId = Brand<string, 'OrderId'>;
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Because there is no runtime representation, values must be converted into a branded type with
|
|
19
|
+
* a type assertion, typically inside a small constructor function that documents and centralizes
|
|
20
|
+
* the conversion:
|
|
21
|
+
* ```ts
|
|
22
|
+
* function toUserId(id: string): UserId {
|
|
23
|
+
* return id as UserId;
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Once created, a branded value can still be used wherever the base type is expected...
|
|
28
|
+
* ```ts
|
|
29
|
+
* function logId(id: string) {}
|
|
30
|
+
* logId(toUserId('abc')); // OK, UserId is a string
|
|
31
|
+
* ```
|
|
32
|
+
* ...but the reverse requires an explicit conversion, and brands are not interchangeable:
|
|
33
|
+
* ```ts
|
|
34
|
+
* function getUser(id: UserId) {}
|
|
35
|
+
* getUser('abc'); // Error, plain string is not a UserId
|
|
36
|
+
* getUser(toOrderId('abc')); // Error, OrderId is not a UserId
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export type Brand<
|
|
40
|
+
Base,
|
|
41
|
+
Name extends string
|
|
42
|
+
> = Base & {
|
|
43
|
+
readonly [brand]: Name;
|
|
41
44
|
};
|
|
42
45
|
export {};
|
package/dist/brand.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds a compile-time-only "brand" to a base type, producing a nominal type.
|
|
3
|
+
* TypeScript's structural typing normally treats any two types with the same shape as
|
|
4
|
+
* interchangeable, so a `UserId` and an `OrderId` that are both `string` can be passed to each
|
|
5
|
+
* other's functions by mistake.
|
|
6
|
+
* `Brand` closes that hole by attaching a unique, unused property to the type, so values of
|
|
7
|
+
* different brands are no longer assignable to one another even though they share a base type.
|
|
8
|
+
* The branded property does not exist at runtime — it is erased during compilation and only
|
|
9
|
+
* affects type checking.
|
|
10
|
+
*
|
|
11
|
+
* Declare a custom branded type by aliasing `Brand` with a base type and a unique name:
|
|
12
|
+
* ```ts
|
|
13
|
+
* type UserId = Brand<string, 'UserId'>;
|
|
14
|
+
* type OrderId = Brand<string, 'OrderId'>;
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Because there is no runtime representation, values must be converted into a branded type with
|
|
18
|
+
* a type assertion, typically inside a small constructor function that documents and centralizes
|
|
19
|
+
* the conversion:
|
|
20
|
+
* ```ts
|
|
21
|
+
* function toUserId(id: string): UserId {
|
|
22
|
+
* return id as UserId;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Once created, a branded value can still be used wherever the base type is expected...
|
|
27
|
+
* ```ts
|
|
28
|
+
* function logId(id: string) {}
|
|
29
|
+
* logId(toUserId('abc')); // OK, UserId is a string
|
|
30
|
+
* ```
|
|
31
|
+
* ...but the reverse requires an explicit conversion, and brands are not interchangeable:
|
|
32
|
+
* ```ts
|
|
33
|
+
* function getUser(id: UserId) {}
|
|
34
|
+
* getUser('abc'); // Error, plain string is not a UserId
|
|
35
|
+
* getUser(toOrderId('abc')); // Error, OrderId is not a UserId
|
|
36
|
+
* ```
|
|
37
|
+
*/ export { };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=brand.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/brand.ts"],"sourcesContent":["declare const brand: unique symbol;\n\n/**\n * Adds a compile-time-only \"brand\" to a base type, producing a nominal type.\n * TypeScript's structural typing normally treats any two types with the same shape as\n * interchangeable, so a `UserId` and an `OrderId` that are both `string` can be passed to each\n * other's functions by mistake.\n * `Brand` closes that hole by attaching a unique, unused property to the type, so values of\n * different brands are no longer assignable to one another even though they share a base type.\n * The branded property does not exist at runtime — it is erased during compilation and only\n * affects type checking.\n *\n * Declare a custom branded type by aliasing `Brand` with a base type and a unique name:\n * ```ts\n * type UserId = Brand<string, 'UserId'>;\n * type OrderId = Brand<string, 'OrderId'>;\n * ```\n *\n * Because there is no runtime representation, values must be converted into a branded type with\n * a type assertion, typically inside a small constructor function that documents and centralizes\n * the conversion:\n * ```ts\n * function toUserId(id: string): UserId {\n * return id as UserId;\n * }\n * ```\n *\n * Once created, a branded value can still be used wherever the base type is expected...\n * ```ts\n * function logId(id: string) {}\n * logId(toUserId('abc')); // OK, UserId is a string\n * ```\n * ...but the reverse requires an explicit conversion, and brands are not interchangeable:\n * ```ts\n * function getUser(id: UserId) {}\n * getUser('abc'); // Error, plain string is not a UserId\n * getUser(toOrderId('abc')); // Error, OrderId is not a UserId\n * ```\n */\nexport type Brand<Base, Name extends string> = Base & {\n readonly [brand]: Name;\n};\n"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoCC,GACD,WAEE"}
|
|
@@ -1,112 +1,118 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
* Options for {@link chunkAndProcessInParallel}.
|
|
3
|
+
*/
|
|
4
4
|
export interface ChunkAndProcessOptions {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of chunks to process in parallel.
|
|
7
|
+
* @default 10
|
|
8
|
+
*/
|
|
9
|
+
numWorkers?: number;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export declare class PartialExecutionError<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
12
|
+
* Error thrown when {@link chunkAndProcessInParallel} fails partway through processing.
|
|
13
|
+
*
|
|
14
|
+
* This error provides detailed information about which items were successfully processed,
|
|
15
|
+
* which items failed, and which items were never attempted.
|
|
16
|
+
*
|
|
17
|
+
* @template T - The type of input elements.
|
|
18
|
+
*/
|
|
19
|
+
export declare class PartialExecutionError<
|
|
20
|
+
T,
|
|
21
|
+
U
|
|
22
|
+
> extends Error {
|
|
23
|
+
/**
|
|
24
|
+
* The underlying error that caused the failure.
|
|
25
|
+
*/
|
|
26
|
+
readonly reason: Error;
|
|
27
|
+
/**
|
|
28
|
+
* Input items from chunks that were successfully processed before the error occurred.
|
|
29
|
+
*/
|
|
30
|
+
readonly succeeded: T[];
|
|
31
|
+
/**
|
|
32
|
+
* Results from the chunks that were successfully processed before the error occurred.
|
|
33
|
+
*/
|
|
34
|
+
readonly succeededResults: U[];
|
|
35
|
+
/**
|
|
36
|
+
* Input items from the chunk that failed and caused the error.
|
|
37
|
+
*/
|
|
38
|
+
readonly failed: T[];
|
|
39
|
+
/**
|
|
40
|
+
* Input items from chunks that were never processed due to the error.
|
|
41
|
+
* This includes both chunks that were never started and chunks that were
|
|
42
|
+
* in-progress when the error occurred.
|
|
43
|
+
*/
|
|
44
|
+
readonly unprocessed: T[];
|
|
45
|
+
constructor(reason: Error, succeeded: T[], succeededResults: U[], failed: T[], unprocessed: T[]);
|
|
43
46
|
}
|
|
44
47
|
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
export declare function chunkAndProcessInParallel<
|
|
48
|
+
* Splits an array into chunks and processes each chunk in parallel using a callback function.
|
|
49
|
+
*
|
|
50
|
+
* This function is useful for batch processing large datasets where you want to:
|
|
51
|
+
* - Limit the number of items processed at once (e.g., API limits)
|
|
52
|
+
* - Process multiple chunks simultaneously for better performance
|
|
53
|
+
* - Control the degree of parallelism to avoid overwhelming resources
|
|
54
|
+
*
|
|
55
|
+
* The function maintains a saturated queue up to `numWorkers` concurrent operations.
|
|
56
|
+
* When one chunk completes, the next chunk starts immediately, ensuring optimal throughput.
|
|
57
|
+
*
|
|
58
|
+
* All chunks are processed with controlled parallelism, and the results are flattened into
|
|
59
|
+
* a single array. If any chunk fails, the entire operation fails.
|
|
60
|
+
* The order of the result is maintained regardless of the completion order of the
|
|
61
|
+
* chunks, but not that there is no requirement that the callback must return one
|
|
62
|
+
* result per input element (i.e. the number of result elements might differ from the
|
|
63
|
+
* number of input elements).
|
|
64
|
+
*
|
|
65
|
+
* @template T - The type of elements in the input array.
|
|
66
|
+
* @template U - The type of elements in the output array.
|
|
67
|
+
* @param workItems - The array of items to process.
|
|
68
|
+
* @param chunkSize - The maximum size of each chunk. Must be a positive integer.
|
|
69
|
+
* @param performWorkCallback - An async function that processes a chunk and returns results.
|
|
70
|
+
* @param options - Optional configuration for parallelism control.
|
|
71
|
+
* @returns A promise that resolves to a flattened array of all results.
|
|
72
|
+
* @throws {Error} If `chunkSize` is not a positive integer or `numWorkers` is not a positive integer.
|
|
73
|
+
* @throws {PartialExecutionError} If `performWorkCallback` throws an error for any chunk, providing details about succeeded, failed, and unprocessed items.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // Batch retrieve assets from CDF with controlled parallelism
|
|
78
|
+
* const assetIds = [1, 2, 3, ..., 10000];
|
|
79
|
+
* const assets = await chunkAndProcessInParallel(
|
|
80
|
+
* assetIds,
|
|
81
|
+
* 1000, // Retrieve 1000 assets per request
|
|
82
|
+
* async (chunk) => {
|
|
83
|
+
* const response = await client.assets.retrieve(
|
|
84
|
+
* chunk.map(id => ({ id }))
|
|
85
|
+
* );
|
|
86
|
+
* return response;
|
|
87
|
+
* },
|
|
88
|
+
* { numWorkers: 5 } // Process at most 5 chunks concurrently
|
|
89
|
+
* );
|
|
90
|
+
* // Result: All assets from all chunks combined
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* // Handle partial execution errors
|
|
96
|
+
* try {
|
|
97
|
+
* const results = await chunkAndProcessInParallel(
|
|
98
|
+
* largeDataset,
|
|
99
|
+
* 100,
|
|
100
|
+
* async (chunk) => processChunk(chunk),
|
|
101
|
+
* { numWorkers: 3 }
|
|
102
|
+
* );
|
|
103
|
+
* } catch (error) {
|
|
104
|
+
* if (error instanceof PartialExecutionError) {
|
|
105
|
+
* console.log(`Successfully processed ${error.succeeded.length} items`);
|
|
106
|
+
* console.log(`Failed to process ${error.failed.length} items`);
|
|
107
|
+
* console.log(`Did not attempt ${error.unprocessed.length} items`);
|
|
108
|
+
* console.error('Failure reason:', error.reason);
|
|
109
|
+
* // Optionally retry only the failed and unprocessed items
|
|
110
|
+
* }
|
|
111
|
+
* throw error;
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export declare function chunkAndProcessInParallel<
|
|
116
|
+
T,
|
|
117
|
+
U
|
|
118
|
+
>(workItems: T[], chunkSize: number, performWorkCallback: (workItemsChunk: T[]) => Promise<U[]>, options?: ChunkAndProcessOptions): Promise<U[]>;
|