@alextheman/utility 5.17.0 → 5.18.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/index.cjs +94 -88
- package/dist/index.d.cts +8 -3
- package/dist/index.d.ts +8 -3
- package/dist/index.js +94 -88
- package/dist/internal/index.cjs +94 -49
- package/dist/internal/index.d.cts +2 -2
- package/dist/internal/index.d.ts +2 -2
- package/dist/internal/index.js +94 -49
- package/dist/node/index.cjs +47 -2
- package/dist/node/index.js +47 -2
- package/dist/v6/index.cjs +47 -2
- package/dist/v6/index.d.cts +4 -4
- package/dist/v6/index.d.ts +4 -4
- package/dist/v6/index.js +47 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -83,55 +83,6 @@ var APIError = class APIError extends Error {
|
|
|
83
83
|
}
|
|
84
84
|
};
|
|
85
85
|
//#endregion
|
|
86
|
-
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
87
|
-
/**
|
|
88
|
-
* Creates a new array where each element is the result of the provided callback.
|
|
89
|
-
*
|
|
90
|
-
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
91
|
-
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
92
|
-
*
|
|
93
|
-
* @category Array Helpers
|
|
94
|
-
*
|
|
95
|
-
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
96
|
-
*
|
|
97
|
-
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
98
|
-
* @param length - The desired length of the resulting array.
|
|
99
|
-
*
|
|
100
|
-
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
101
|
-
*/
|
|
102
|
-
function fillArray(callback, length = 1) {
|
|
103
|
-
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
104
|
-
return callback(index);
|
|
105
|
-
});
|
|
106
|
-
if (outputArray.some((item) => {
|
|
107
|
-
return item instanceof Promise;
|
|
108
|
-
})) return Promise.all(outputArray);
|
|
109
|
-
return outputArray;
|
|
110
|
-
}
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
113
|
-
/**
|
|
114
|
-
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
115
|
-
*
|
|
116
|
-
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
117
|
-
* will be `undefined`. Iteration always uses the length of the first array.
|
|
118
|
-
*
|
|
119
|
-
* @category Array Helpers
|
|
120
|
-
*
|
|
121
|
-
* @template FirstArrayItem
|
|
122
|
-
* @template SecondArrayItem
|
|
123
|
-
*
|
|
124
|
-
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
125
|
-
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
126
|
-
*
|
|
127
|
-
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
128
|
-
*/
|
|
129
|
-
function paralleliseArrays(firstArray, secondArray) {
|
|
130
|
-
const outputArray = [];
|
|
131
|
-
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
132
|
-
return outputArray;
|
|
133
|
-
}
|
|
134
|
-
//#endregion
|
|
135
86
|
//#region src/v6/CodeError.ts
|
|
136
87
|
/**
|
|
137
88
|
* Represents errors that can be described using a standardised error code, and a human-readable error message.
|
|
@@ -335,6 +286,100 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
335
286
|
}
|
|
336
287
|
};
|
|
337
288
|
//#endregion
|
|
289
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
290
|
+
/**
|
|
291
|
+
* Creates an array of numbers within a given range.
|
|
292
|
+
*
|
|
293
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
294
|
+
* - The sign of `step` must match the direction of the range.
|
|
295
|
+
*
|
|
296
|
+
* @category Array Helpers
|
|
297
|
+
*
|
|
298
|
+
* @param start - The number to start at (inclusive).
|
|
299
|
+
* @param stop - The number to stop at (exclusive).
|
|
300
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
301
|
+
*
|
|
302
|
+
* @throws {DataError} If `step` is `0`.
|
|
303
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
304
|
+
*
|
|
305
|
+
* @returns An array of numbers satisfying the range provided.
|
|
306
|
+
*/
|
|
307
|
+
function range(start, stop, step = 1) {
|
|
308
|
+
const numbers = [];
|
|
309
|
+
if (step === 0) throw new DataError$1({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
310
|
+
else if (step > 0) {
|
|
311
|
+
if (start > stop) throw new DataError$1({
|
|
312
|
+
start,
|
|
313
|
+
stop,
|
|
314
|
+
step
|
|
315
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
316
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
317
|
+
} else if (step < 0) {
|
|
318
|
+
if (start < stop) throw new DataError$1({
|
|
319
|
+
start,
|
|
320
|
+
stop,
|
|
321
|
+
step
|
|
322
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
323
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
324
|
+
}
|
|
325
|
+
return numbers;
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
329
|
+
/**
|
|
330
|
+
* Creates a new array where each element is the result of the provided callback.
|
|
331
|
+
*
|
|
332
|
+
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
333
|
+
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
334
|
+
*
|
|
335
|
+
* @category Array Helpers
|
|
336
|
+
*
|
|
337
|
+
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
338
|
+
*
|
|
339
|
+
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
340
|
+
* @param length - The desired length of the resulting array.
|
|
341
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
342
|
+
*
|
|
343
|
+
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
344
|
+
*/
|
|
345
|
+
function fillArray(callback, length = 1, options) {
|
|
346
|
+
if (options?.sequential) return (async () => {
|
|
347
|
+
const resolvedArray = [];
|
|
348
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
349
|
+
return resolvedArray;
|
|
350
|
+
})();
|
|
351
|
+
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
352
|
+
return callback(index);
|
|
353
|
+
});
|
|
354
|
+
if (outputArray.some((item) => {
|
|
355
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
356
|
+
})) return Promise.all(outputArray);
|
|
357
|
+
return outputArray;
|
|
358
|
+
}
|
|
359
|
+
//#endregion
|
|
360
|
+
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
361
|
+
/**
|
|
362
|
+
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
363
|
+
*
|
|
364
|
+
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
365
|
+
* will be `undefined`. Iteration always uses the length of the first array.
|
|
366
|
+
*
|
|
367
|
+
* @category Array Helpers
|
|
368
|
+
*
|
|
369
|
+
* @template FirstArrayItem
|
|
370
|
+
* @template SecondArrayItem
|
|
371
|
+
*
|
|
372
|
+
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
373
|
+
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
374
|
+
*
|
|
375
|
+
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
376
|
+
*/
|
|
377
|
+
function paralleliseArrays(firstArray, secondArray) {
|
|
378
|
+
const outputArray = [];
|
|
379
|
+
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
380
|
+
return outputArray;
|
|
381
|
+
}
|
|
382
|
+
//#endregion
|
|
338
383
|
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
339
384
|
/**
|
|
340
385
|
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
@@ -405,45 +450,6 @@ function randomiseArray(array) {
|
|
|
405
450
|
return outputArray;
|
|
406
451
|
}
|
|
407
452
|
//#endregion
|
|
408
|
-
//#region src/root/functions/arrayHelpers/range.ts
|
|
409
|
-
/**
|
|
410
|
-
* Creates an array of numbers within a given range.
|
|
411
|
-
*
|
|
412
|
-
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
413
|
-
* - The sign of `step` must match the direction of the range.
|
|
414
|
-
*
|
|
415
|
-
* @category Array Helpers
|
|
416
|
-
*
|
|
417
|
-
* @param start - The number to start at (inclusive).
|
|
418
|
-
* @param stop - The number to stop at (exclusive).
|
|
419
|
-
* @param step - The step size between numbers, defaulting to 1.
|
|
420
|
-
*
|
|
421
|
-
* @throws {DataError} If `step` is `0`.
|
|
422
|
-
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
423
|
-
*
|
|
424
|
-
* @returns An array of numbers satisfying the range provided.
|
|
425
|
-
*/
|
|
426
|
-
function range(start, stop, step = 1) {
|
|
427
|
-
const numbers = [];
|
|
428
|
-
if (step === 0) throw new DataError$1({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
429
|
-
else if (step > 0) {
|
|
430
|
-
if (start > stop) throw new DataError$1({
|
|
431
|
-
start,
|
|
432
|
-
stop,
|
|
433
|
-
step
|
|
434
|
-
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
435
|
-
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
436
|
-
} else if (step < 0) {
|
|
437
|
-
if (start < stop) throw new DataError$1({
|
|
438
|
-
start,
|
|
439
|
-
stop,
|
|
440
|
-
step
|
|
441
|
-
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
442
|
-
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
443
|
-
}
|
|
444
|
-
return numbers;
|
|
445
|
-
}
|
|
446
|
-
//#endregion
|
|
447
453
|
//#region src/root/functions/arrayHelpers/removeDuplicates.ts
|
|
448
454
|
/**
|
|
449
455
|
* Removes duplicate values from an array.
|
package/dist/index.d.cts
CHANGED
|
@@ -113,6 +113,10 @@ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<P
|
|
|
113
113
|
type RecordKey = string | number | symbol;
|
|
114
114
|
//#endregion
|
|
115
115
|
//#region src/root/functions/arrayHelpers/fillArray.d.ts
|
|
116
|
+
interface FillArrayAsyncOptions {
|
|
117
|
+
/** Resolve each array item sequentially rather than in parallel (defaults to parallel). */
|
|
118
|
+
sequential?: boolean;
|
|
119
|
+
}
|
|
116
120
|
/**
|
|
117
121
|
* Creates a new array where each element is the resolved result of the provided asynchronous callback.
|
|
118
122
|
*
|
|
@@ -123,10 +127,11 @@ type RecordKey = string | number | symbol;
|
|
|
123
127
|
*
|
|
124
128
|
* @param callback - An asynchronous function invoked with the current index.
|
|
125
129
|
* @param length - The desired length of the resulting array.
|
|
130
|
+
* @param options - Extra options to apply.
|
|
126
131
|
*
|
|
127
132
|
* @returns A Promise resolving to an array of the callback results.
|
|
128
133
|
*/
|
|
129
|
-
declare function fillArray<ItemType>(callback: (index: number) => Promise<ItemType>, length?: number): Promise<Array<ItemType>>;
|
|
134
|
+
declare function fillArray<ItemType>(callback: (index: number) => Promise<ItemType>, length?: number, options?: FillArrayAsyncOptions): Promise<Array<ItemType>>;
|
|
130
135
|
/**
|
|
131
136
|
* Creates a new array where each element is the result of the provided synchronous callback.
|
|
132
137
|
*
|
|
@@ -1288,7 +1293,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1288
1293
|
*
|
|
1289
1294
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1290
1295
|
*/
|
|
1291
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
1296
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
1292
1297
|
/**
|
|
1293
1298
|
* Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
|
|
1294
1299
|
*
|
|
@@ -1300,7 +1305,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1300
1305
|
*
|
|
1301
1306
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1302
1307
|
*/
|
|
1303
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
1308
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
1304
1309
|
/**
|
|
1305
1310
|
* Converts the `DataError` instance to a serialised JSON payload.
|
|
1306
1311
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -113,6 +113,10 @@ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<P
|
|
|
113
113
|
type RecordKey = string | number | symbol;
|
|
114
114
|
//#endregion
|
|
115
115
|
//#region src/root/functions/arrayHelpers/fillArray.d.ts
|
|
116
|
+
interface FillArrayAsyncOptions {
|
|
117
|
+
/** Resolve each array item sequentially rather than in parallel (defaults to parallel). */
|
|
118
|
+
sequential?: boolean;
|
|
119
|
+
}
|
|
116
120
|
/**
|
|
117
121
|
* Creates a new array where each element is the resolved result of the provided asynchronous callback.
|
|
118
122
|
*
|
|
@@ -123,10 +127,11 @@ type RecordKey = string | number | symbol;
|
|
|
123
127
|
*
|
|
124
128
|
* @param callback - An asynchronous function invoked with the current index.
|
|
125
129
|
* @param length - The desired length of the resulting array.
|
|
130
|
+
* @param options - Extra options to apply.
|
|
126
131
|
*
|
|
127
132
|
* @returns A Promise resolving to an array of the callback results.
|
|
128
133
|
*/
|
|
129
|
-
declare function fillArray<ItemType>(callback: (index: number) => Promise<ItemType>, length?: number): Promise<Array<ItemType>>;
|
|
134
|
+
declare function fillArray<ItemType>(callback: (index: number) => Promise<ItemType>, length?: number, options?: FillArrayAsyncOptions): Promise<Array<ItemType>>;
|
|
130
135
|
/**
|
|
131
136
|
* Creates a new array where each element is the result of the provided synchronous callback.
|
|
132
137
|
*
|
|
@@ -1288,7 +1293,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1288
1293
|
*
|
|
1289
1294
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1290
1295
|
*/
|
|
1291
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
1296
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
1292
1297
|
/**
|
|
1293
1298
|
* Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
|
|
1294
1299
|
*
|
|
@@ -1300,7 +1305,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1300
1305
|
*
|
|
1301
1306
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1302
1307
|
*/
|
|
1303
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
1308
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
1304
1309
|
/**
|
|
1305
1310
|
* Converts the `DataError` instance to a serialised JSON payload.
|
|
1306
1311
|
*
|
package/dist/index.js
CHANGED
|
@@ -59,55 +59,6 @@ var APIError = class APIError extends Error {
|
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
//#endregion
|
|
62
|
-
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
63
|
-
/**
|
|
64
|
-
* Creates a new array where each element is the result of the provided callback.
|
|
65
|
-
*
|
|
66
|
-
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
67
|
-
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
68
|
-
*
|
|
69
|
-
* @category Array Helpers
|
|
70
|
-
*
|
|
71
|
-
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
72
|
-
*
|
|
73
|
-
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
74
|
-
* @param length - The desired length of the resulting array.
|
|
75
|
-
*
|
|
76
|
-
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
77
|
-
*/
|
|
78
|
-
function fillArray(callback, length = 1) {
|
|
79
|
-
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
80
|
-
return callback(index);
|
|
81
|
-
});
|
|
82
|
-
if (outputArray.some((item) => {
|
|
83
|
-
return item instanceof Promise;
|
|
84
|
-
})) return Promise.all(outputArray);
|
|
85
|
-
return outputArray;
|
|
86
|
-
}
|
|
87
|
-
//#endregion
|
|
88
|
-
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
89
|
-
/**
|
|
90
|
-
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
91
|
-
*
|
|
92
|
-
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
93
|
-
* will be `undefined`. Iteration always uses the length of the first array.
|
|
94
|
-
*
|
|
95
|
-
* @category Array Helpers
|
|
96
|
-
*
|
|
97
|
-
* @template FirstArrayItem
|
|
98
|
-
* @template SecondArrayItem
|
|
99
|
-
*
|
|
100
|
-
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
101
|
-
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
102
|
-
*
|
|
103
|
-
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
104
|
-
*/
|
|
105
|
-
function paralleliseArrays(firstArray, secondArray) {
|
|
106
|
-
const outputArray = [];
|
|
107
|
-
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
108
|
-
return outputArray;
|
|
109
|
-
}
|
|
110
|
-
//#endregion
|
|
111
62
|
//#region src/v6/CodeError.ts
|
|
112
63
|
/**
|
|
113
64
|
* Represents errors that can be described using a standardised error code, and a human-readable error message.
|
|
@@ -311,6 +262,100 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
311
262
|
}
|
|
312
263
|
};
|
|
313
264
|
//#endregion
|
|
265
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
266
|
+
/**
|
|
267
|
+
* Creates an array of numbers within a given range.
|
|
268
|
+
*
|
|
269
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
270
|
+
* - The sign of `step` must match the direction of the range.
|
|
271
|
+
*
|
|
272
|
+
* @category Array Helpers
|
|
273
|
+
*
|
|
274
|
+
* @param start - The number to start at (inclusive).
|
|
275
|
+
* @param stop - The number to stop at (exclusive).
|
|
276
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
277
|
+
*
|
|
278
|
+
* @throws {DataError} If `step` is `0`.
|
|
279
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
280
|
+
*
|
|
281
|
+
* @returns An array of numbers satisfying the range provided.
|
|
282
|
+
*/
|
|
283
|
+
function range(start, stop, step = 1) {
|
|
284
|
+
const numbers = [];
|
|
285
|
+
if (step === 0) throw new DataError$1({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
286
|
+
else if (step > 0) {
|
|
287
|
+
if (start > stop) throw new DataError$1({
|
|
288
|
+
start,
|
|
289
|
+
stop,
|
|
290
|
+
step
|
|
291
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
292
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
293
|
+
} else if (step < 0) {
|
|
294
|
+
if (start < stop) throw new DataError$1({
|
|
295
|
+
start,
|
|
296
|
+
stop,
|
|
297
|
+
step
|
|
298
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
299
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
300
|
+
}
|
|
301
|
+
return numbers;
|
|
302
|
+
}
|
|
303
|
+
//#endregion
|
|
304
|
+
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
305
|
+
/**
|
|
306
|
+
* Creates a new array where each element is the result of the provided callback.
|
|
307
|
+
*
|
|
308
|
+
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
309
|
+
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
310
|
+
*
|
|
311
|
+
* @category Array Helpers
|
|
312
|
+
*
|
|
313
|
+
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
314
|
+
*
|
|
315
|
+
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
316
|
+
* @param length - The desired length of the resulting array.
|
|
317
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
318
|
+
*
|
|
319
|
+
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
320
|
+
*/
|
|
321
|
+
function fillArray(callback, length = 1, options) {
|
|
322
|
+
if (options?.sequential) return (async () => {
|
|
323
|
+
const resolvedArray = [];
|
|
324
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
325
|
+
return resolvedArray;
|
|
326
|
+
})();
|
|
327
|
+
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
328
|
+
return callback(index);
|
|
329
|
+
});
|
|
330
|
+
if (outputArray.some((item) => {
|
|
331
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
332
|
+
})) return Promise.all(outputArray);
|
|
333
|
+
return outputArray;
|
|
334
|
+
}
|
|
335
|
+
//#endregion
|
|
336
|
+
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
337
|
+
/**
|
|
338
|
+
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
339
|
+
*
|
|
340
|
+
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
341
|
+
* will be `undefined`. Iteration always uses the length of the first array.
|
|
342
|
+
*
|
|
343
|
+
* @category Array Helpers
|
|
344
|
+
*
|
|
345
|
+
* @template FirstArrayItem
|
|
346
|
+
* @template SecondArrayItem
|
|
347
|
+
*
|
|
348
|
+
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
349
|
+
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
350
|
+
*
|
|
351
|
+
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
352
|
+
*/
|
|
353
|
+
function paralleliseArrays(firstArray, secondArray) {
|
|
354
|
+
const outputArray = [];
|
|
355
|
+
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
356
|
+
return outputArray;
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
314
359
|
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
315
360
|
/**
|
|
316
361
|
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
@@ -381,45 +426,6 @@ function randomiseArray(array) {
|
|
|
381
426
|
return outputArray;
|
|
382
427
|
}
|
|
383
428
|
//#endregion
|
|
384
|
-
//#region src/root/functions/arrayHelpers/range.ts
|
|
385
|
-
/**
|
|
386
|
-
* Creates an array of numbers within a given range.
|
|
387
|
-
*
|
|
388
|
-
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
389
|
-
* - The sign of `step` must match the direction of the range.
|
|
390
|
-
*
|
|
391
|
-
* @category Array Helpers
|
|
392
|
-
*
|
|
393
|
-
* @param start - The number to start at (inclusive).
|
|
394
|
-
* @param stop - The number to stop at (exclusive).
|
|
395
|
-
* @param step - The step size between numbers, defaulting to 1.
|
|
396
|
-
*
|
|
397
|
-
* @throws {DataError} If `step` is `0`.
|
|
398
|
-
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
399
|
-
*
|
|
400
|
-
* @returns An array of numbers satisfying the range provided.
|
|
401
|
-
*/
|
|
402
|
-
function range(start, stop, step = 1) {
|
|
403
|
-
const numbers = [];
|
|
404
|
-
if (step === 0) throw new DataError$1({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
405
|
-
else if (step > 0) {
|
|
406
|
-
if (start > stop) throw new DataError$1({
|
|
407
|
-
start,
|
|
408
|
-
stop,
|
|
409
|
-
step
|
|
410
|
-
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
411
|
-
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
412
|
-
} else if (step < 0) {
|
|
413
|
-
if (start < stop) throw new DataError$1({
|
|
414
|
-
start,
|
|
415
|
-
stop,
|
|
416
|
-
step
|
|
417
|
-
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
418
|
-
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
419
|
-
}
|
|
420
|
-
return numbers;
|
|
421
|
-
}
|
|
422
|
-
//#endregion
|
|
423
429
|
//#region src/root/functions/arrayHelpers/removeDuplicates.ts
|
|
424
430
|
/**
|
|
425
431
|
* Removes duplicate values from an array.
|
package/dist/internal/index.cjs
CHANGED
|
@@ -37,55 +37,6 @@ const DependencyGroup = {
|
|
|
37
37
|
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
38
38
|
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
39
39
|
//#endregion
|
|
40
|
-
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
41
|
-
/**
|
|
42
|
-
* Creates a new array where each element is the result of the provided callback.
|
|
43
|
-
*
|
|
44
|
-
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
45
|
-
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
46
|
-
*
|
|
47
|
-
* @category Array Helpers
|
|
48
|
-
*
|
|
49
|
-
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
50
|
-
*
|
|
51
|
-
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
52
|
-
* @param length - The desired length of the resulting array.
|
|
53
|
-
*
|
|
54
|
-
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
55
|
-
*/
|
|
56
|
-
function fillArray(callback, length = 1) {
|
|
57
|
-
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
58
|
-
return callback(index);
|
|
59
|
-
});
|
|
60
|
-
if (outputArray.some((item) => {
|
|
61
|
-
return item instanceof Promise;
|
|
62
|
-
})) return Promise.all(outputArray);
|
|
63
|
-
return outputArray;
|
|
64
|
-
}
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
67
|
-
/**
|
|
68
|
-
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
69
|
-
*
|
|
70
|
-
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
71
|
-
* will be `undefined`. Iteration always uses the length of the first array.
|
|
72
|
-
*
|
|
73
|
-
* @category Array Helpers
|
|
74
|
-
*
|
|
75
|
-
* @template FirstArrayItem
|
|
76
|
-
* @template SecondArrayItem
|
|
77
|
-
*
|
|
78
|
-
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
79
|
-
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
80
|
-
*
|
|
81
|
-
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
82
|
-
*/
|
|
83
|
-
function paralleliseArrays(firstArray, secondArray) {
|
|
84
|
-
const outputArray = [];
|
|
85
|
-
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
86
|
-
return outputArray;
|
|
87
|
-
}
|
|
88
|
-
//#endregion
|
|
89
40
|
//#region src/v6/CodeError.ts
|
|
90
41
|
/**
|
|
91
42
|
* Represents errors that can be described using a standardised error code, and a human-readable error message.
|
|
@@ -289,6 +240,100 @@ var DataError = class DataError extends CodeError {
|
|
|
289
240
|
}
|
|
290
241
|
};
|
|
291
242
|
//#endregion
|
|
243
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
244
|
+
/**
|
|
245
|
+
* Creates an array of numbers within a given range.
|
|
246
|
+
*
|
|
247
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
248
|
+
* - The sign of `step` must match the direction of the range.
|
|
249
|
+
*
|
|
250
|
+
* @category Array Helpers
|
|
251
|
+
*
|
|
252
|
+
* @param start - The number to start at (inclusive).
|
|
253
|
+
* @param stop - The number to stop at (exclusive).
|
|
254
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
255
|
+
*
|
|
256
|
+
* @throws {DataError} If `step` is `0`.
|
|
257
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
258
|
+
*
|
|
259
|
+
* @returns An array of numbers satisfying the range provided.
|
|
260
|
+
*/
|
|
261
|
+
function range(start, stop, step = 1) {
|
|
262
|
+
const numbers = [];
|
|
263
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
264
|
+
else if (step > 0) {
|
|
265
|
+
if (start > stop) throw new DataError({
|
|
266
|
+
start,
|
|
267
|
+
stop,
|
|
268
|
+
step
|
|
269
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
270
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
271
|
+
} else if (step < 0) {
|
|
272
|
+
if (start < stop) throw new DataError({
|
|
273
|
+
start,
|
|
274
|
+
stop,
|
|
275
|
+
step
|
|
276
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
277
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
278
|
+
}
|
|
279
|
+
return numbers;
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
283
|
+
/**
|
|
284
|
+
* Creates a new array where each element is the result of the provided callback.
|
|
285
|
+
*
|
|
286
|
+
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
287
|
+
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
288
|
+
*
|
|
289
|
+
* @category Array Helpers
|
|
290
|
+
*
|
|
291
|
+
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
292
|
+
*
|
|
293
|
+
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
294
|
+
* @param length - The desired length of the resulting array.
|
|
295
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
296
|
+
*
|
|
297
|
+
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
298
|
+
*/
|
|
299
|
+
function fillArray(callback, length = 1, options) {
|
|
300
|
+
if (options?.sequential) return (async () => {
|
|
301
|
+
const resolvedArray = [];
|
|
302
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
303
|
+
return resolvedArray;
|
|
304
|
+
})();
|
|
305
|
+
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
306
|
+
return callback(index);
|
|
307
|
+
});
|
|
308
|
+
if (outputArray.some((item) => {
|
|
309
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
310
|
+
})) return Promise.all(outputArray);
|
|
311
|
+
return outputArray;
|
|
312
|
+
}
|
|
313
|
+
//#endregion
|
|
314
|
+
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
315
|
+
/**
|
|
316
|
+
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
317
|
+
*
|
|
318
|
+
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
319
|
+
* will be `undefined`. Iteration always uses the length of the first array.
|
|
320
|
+
*
|
|
321
|
+
* @category Array Helpers
|
|
322
|
+
*
|
|
323
|
+
* @template FirstArrayItem
|
|
324
|
+
* @template SecondArrayItem
|
|
325
|
+
*
|
|
326
|
+
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
327
|
+
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
328
|
+
*
|
|
329
|
+
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
330
|
+
*/
|
|
331
|
+
function paralleliseArrays(firstArray, secondArray) {
|
|
332
|
+
const outputArray = [];
|
|
333
|
+
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
334
|
+
return outputArray;
|
|
335
|
+
}
|
|
336
|
+
//#endregion
|
|
292
337
|
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
293
338
|
/**
|
|
294
339
|
* Converts a string to an integer and throws an error if it cannot be converted.
|
|
@@ -147,7 +147,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
147
147
|
*
|
|
148
148
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
149
149
|
*/
|
|
150
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
150
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
151
151
|
/**
|
|
152
152
|
* Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
|
|
153
153
|
*
|
|
@@ -159,7 +159,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
159
159
|
*
|
|
160
160
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
161
161
|
*/
|
|
162
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
162
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
163
163
|
/**
|
|
164
164
|
* Converts the `DataError` instance to a serialised JSON payload.
|
|
165
165
|
*
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -147,7 +147,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
147
147
|
*
|
|
148
148
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
149
149
|
*/
|
|
150
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
150
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
151
151
|
/**
|
|
152
152
|
* Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
|
|
153
153
|
*
|
|
@@ -159,7 +159,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
159
159
|
*
|
|
160
160
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
161
161
|
*/
|
|
162
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
162
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
163
163
|
/**
|
|
164
164
|
* Converts the `DataError` instance to a serialised JSON payload.
|
|
165
165
|
*
|
package/dist/internal/index.js
CHANGED
|
@@ -12,55 +12,6 @@ const DependencyGroup = {
|
|
|
12
12
|
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
13
13
|
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
14
14
|
//#endregion
|
|
15
|
-
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new array where each element is the result of the provided callback.
|
|
18
|
-
*
|
|
19
|
-
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
20
|
-
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
21
|
-
*
|
|
22
|
-
* @category Array Helpers
|
|
23
|
-
*
|
|
24
|
-
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
25
|
-
*
|
|
26
|
-
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
27
|
-
* @param length - The desired length of the resulting array.
|
|
28
|
-
*
|
|
29
|
-
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
30
|
-
*/
|
|
31
|
-
function fillArray(callback, length = 1) {
|
|
32
|
-
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
33
|
-
return callback(index);
|
|
34
|
-
});
|
|
35
|
-
if (outputArray.some((item) => {
|
|
36
|
-
return item instanceof Promise;
|
|
37
|
-
})) return Promise.all(outputArray);
|
|
38
|
-
return outputArray;
|
|
39
|
-
}
|
|
40
|
-
//#endregion
|
|
41
|
-
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
42
|
-
/**
|
|
43
|
-
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
44
|
-
*
|
|
45
|
-
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
46
|
-
* will be `undefined`. Iteration always uses the length of the first array.
|
|
47
|
-
*
|
|
48
|
-
* @category Array Helpers
|
|
49
|
-
*
|
|
50
|
-
* @template FirstArrayItem
|
|
51
|
-
* @template SecondArrayItem
|
|
52
|
-
*
|
|
53
|
-
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
54
|
-
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
55
|
-
*
|
|
56
|
-
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
57
|
-
*/
|
|
58
|
-
function paralleliseArrays(firstArray, secondArray) {
|
|
59
|
-
const outputArray = [];
|
|
60
|
-
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
61
|
-
return outputArray;
|
|
62
|
-
}
|
|
63
|
-
//#endregion
|
|
64
15
|
//#region src/v6/CodeError.ts
|
|
65
16
|
/**
|
|
66
17
|
* Represents errors that can be described using a standardised error code, and a human-readable error message.
|
|
@@ -264,6 +215,100 @@ var DataError = class DataError extends CodeError {
|
|
|
264
215
|
}
|
|
265
216
|
};
|
|
266
217
|
//#endregion
|
|
218
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
219
|
+
/**
|
|
220
|
+
* Creates an array of numbers within a given range.
|
|
221
|
+
*
|
|
222
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
223
|
+
* - The sign of `step` must match the direction of the range.
|
|
224
|
+
*
|
|
225
|
+
* @category Array Helpers
|
|
226
|
+
*
|
|
227
|
+
* @param start - The number to start at (inclusive).
|
|
228
|
+
* @param stop - The number to stop at (exclusive).
|
|
229
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
230
|
+
*
|
|
231
|
+
* @throws {DataError} If `step` is `0`.
|
|
232
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
233
|
+
*
|
|
234
|
+
* @returns An array of numbers satisfying the range provided.
|
|
235
|
+
*/
|
|
236
|
+
function range(start, stop, step = 1) {
|
|
237
|
+
const numbers = [];
|
|
238
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
239
|
+
else if (step > 0) {
|
|
240
|
+
if (start > stop) throw new DataError({
|
|
241
|
+
start,
|
|
242
|
+
stop,
|
|
243
|
+
step
|
|
244
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
245
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
246
|
+
} else if (step < 0) {
|
|
247
|
+
if (start < stop) throw new DataError({
|
|
248
|
+
start,
|
|
249
|
+
stop,
|
|
250
|
+
step
|
|
251
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
252
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
253
|
+
}
|
|
254
|
+
return numbers;
|
|
255
|
+
}
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
258
|
+
/**
|
|
259
|
+
* Creates a new array where each element is the result of the provided callback.
|
|
260
|
+
*
|
|
261
|
+
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
262
|
+
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
263
|
+
*
|
|
264
|
+
* @category Array Helpers
|
|
265
|
+
*
|
|
266
|
+
* @template ItemType - The return type of the callback (awaited if any items are a Promise) that becomes the type of the array items.
|
|
267
|
+
*
|
|
268
|
+
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
269
|
+
* @param length - The desired length of the resulting array.
|
|
270
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
271
|
+
*
|
|
272
|
+
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
273
|
+
*/
|
|
274
|
+
function fillArray(callback, length = 1, options) {
|
|
275
|
+
if (options?.sequential) return (async () => {
|
|
276
|
+
const resolvedArray = [];
|
|
277
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
278
|
+
return resolvedArray;
|
|
279
|
+
})();
|
|
280
|
+
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
281
|
+
return callback(index);
|
|
282
|
+
});
|
|
283
|
+
if (outputArray.some((item) => {
|
|
284
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
285
|
+
})) return Promise.all(outputArray);
|
|
286
|
+
return outputArray;
|
|
287
|
+
}
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region src/root/functions/arrayHelpers/paralleliseArrays.ts
|
|
290
|
+
/**
|
|
291
|
+
* Creates a new array of tuples, each containing the item at the given index from both arrays.
|
|
292
|
+
*
|
|
293
|
+
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
294
|
+
* will be `undefined`. Iteration always uses the length of the first array.
|
|
295
|
+
*
|
|
296
|
+
* @category Array Helpers
|
|
297
|
+
*
|
|
298
|
+
* @template FirstArrayItem
|
|
299
|
+
* @template SecondArrayItem
|
|
300
|
+
*
|
|
301
|
+
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
302
|
+
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
303
|
+
*
|
|
304
|
+
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
305
|
+
*/
|
|
306
|
+
function paralleliseArrays(firstArray, secondArray) {
|
|
307
|
+
const outputArray = [];
|
|
308
|
+
for (let i = 0; i < firstArray.length; i++) outputArray.push([firstArray[i], secondArray[i]]);
|
|
309
|
+
return outputArray;
|
|
310
|
+
}
|
|
311
|
+
//#endregion
|
|
267
312
|
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
268
313
|
/**
|
|
269
314
|
* Converts a string to an integer and throws an error if it cannot be converted.
|
package/dist/node/index.cjs
CHANGED
|
@@ -63,6 +63,45 @@ const normaliseImportPath = normalizeImportPath;
|
|
|
63
63
|
const FILE_PATH_PATTERN = String.raw`(?<directory>.+)[\/\\](?<base>[^\/\\]+)`;
|
|
64
64
|
const FILE_PATH_REGEX = RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
65
65
|
//#endregion
|
|
66
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
67
|
+
/**
|
|
68
|
+
* Creates an array of numbers within a given range.
|
|
69
|
+
*
|
|
70
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
71
|
+
* - The sign of `step` must match the direction of the range.
|
|
72
|
+
*
|
|
73
|
+
* @category Array Helpers
|
|
74
|
+
*
|
|
75
|
+
* @param start - The number to start at (inclusive).
|
|
76
|
+
* @param stop - The number to stop at (exclusive).
|
|
77
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
78
|
+
*
|
|
79
|
+
* @throws {DataError} If `step` is `0`.
|
|
80
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
81
|
+
*
|
|
82
|
+
* @returns An array of numbers satisfying the range provided.
|
|
83
|
+
*/
|
|
84
|
+
function range(start, stop, step = 1) {
|
|
85
|
+
const numbers = [];
|
|
86
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
87
|
+
else if (step > 0) {
|
|
88
|
+
if (start > stop) throw new DataError({
|
|
89
|
+
start,
|
|
90
|
+
stop,
|
|
91
|
+
step
|
|
92
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
93
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
94
|
+
} else if (step < 0) {
|
|
95
|
+
if (start < stop) throw new DataError({
|
|
96
|
+
start,
|
|
97
|
+
stop,
|
|
98
|
+
step
|
|
99
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
100
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
101
|
+
}
|
|
102
|
+
return numbers;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
66
105
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
67
106
|
/**
|
|
68
107
|
* Creates a new array where each element is the result of the provided callback.
|
|
@@ -76,15 +115,21 @@ const FILE_PATH_REGEX = RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
|
76
115
|
*
|
|
77
116
|
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
78
117
|
* @param length - The desired length of the resulting array.
|
|
118
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
79
119
|
*
|
|
80
120
|
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
81
121
|
*/
|
|
82
|
-
function fillArray(callback, length = 1) {
|
|
122
|
+
function fillArray(callback, length = 1, options) {
|
|
123
|
+
if (options?.sequential) return (async () => {
|
|
124
|
+
const resolvedArray = [];
|
|
125
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
126
|
+
return resolvedArray;
|
|
127
|
+
})();
|
|
83
128
|
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
84
129
|
return callback(index);
|
|
85
130
|
});
|
|
86
131
|
if (outputArray.some((item) => {
|
|
87
|
-
return item instanceof Promise;
|
|
132
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
88
133
|
})) return Promise.all(outputArray);
|
|
89
134
|
return outputArray;
|
|
90
135
|
}
|
package/dist/node/index.js
CHANGED
|
@@ -39,6 +39,45 @@ const normaliseImportPath = normalizeImportPath;
|
|
|
39
39
|
const FILE_PATH_PATTERN = String.raw`(?<directory>.+)[\/\\](?<base>[^\/\\]+)`;
|
|
40
40
|
const FILE_PATH_REGEX = RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
41
41
|
//#endregion
|
|
42
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
43
|
+
/**
|
|
44
|
+
* Creates an array of numbers within a given range.
|
|
45
|
+
*
|
|
46
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
47
|
+
* - The sign of `step` must match the direction of the range.
|
|
48
|
+
*
|
|
49
|
+
* @category Array Helpers
|
|
50
|
+
*
|
|
51
|
+
* @param start - The number to start at (inclusive).
|
|
52
|
+
* @param stop - The number to stop at (exclusive).
|
|
53
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
54
|
+
*
|
|
55
|
+
* @throws {DataError} If `step` is `0`.
|
|
56
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
57
|
+
*
|
|
58
|
+
* @returns An array of numbers satisfying the range provided.
|
|
59
|
+
*/
|
|
60
|
+
function range(start, stop, step = 1) {
|
|
61
|
+
const numbers = [];
|
|
62
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
63
|
+
else if (step > 0) {
|
|
64
|
+
if (start > stop) throw new DataError({
|
|
65
|
+
start,
|
|
66
|
+
stop,
|
|
67
|
+
step
|
|
68
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
69
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
70
|
+
} else if (step < 0) {
|
|
71
|
+
if (start < stop) throw new DataError({
|
|
72
|
+
start,
|
|
73
|
+
stop,
|
|
74
|
+
step
|
|
75
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
76
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
77
|
+
}
|
|
78
|
+
return numbers;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
42
81
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
43
82
|
/**
|
|
44
83
|
* Creates a new array where each element is the result of the provided callback.
|
|
@@ -52,15 +91,21 @@ const FILE_PATH_REGEX = RegExp(`^${FILE_PATH_PATTERN}$`);
|
|
|
52
91
|
*
|
|
53
92
|
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
54
93
|
* @param length - The desired length of the resulting array.
|
|
94
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
55
95
|
*
|
|
56
96
|
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
57
97
|
*/
|
|
58
|
-
function fillArray(callback, length = 1) {
|
|
98
|
+
function fillArray(callback, length = 1, options) {
|
|
99
|
+
if (options?.sequential) return (async () => {
|
|
100
|
+
const resolvedArray = [];
|
|
101
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
102
|
+
return resolvedArray;
|
|
103
|
+
})();
|
|
59
104
|
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
60
105
|
return callback(index);
|
|
61
106
|
});
|
|
62
107
|
if (outputArray.some((item) => {
|
|
63
|
-
return item instanceof Promise;
|
|
108
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
64
109
|
})) return Promise.all(outputArray);
|
|
65
110
|
return outputArray;
|
|
66
111
|
}
|
package/dist/v6/index.cjs
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates an array of numbers within a given range.
|
|
5
|
+
*
|
|
6
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
7
|
+
* - The sign of `step` must match the direction of the range.
|
|
8
|
+
*
|
|
9
|
+
* @category Array Helpers
|
|
10
|
+
*
|
|
11
|
+
* @param start - The number to start at (inclusive).
|
|
12
|
+
* @param stop - The number to stop at (exclusive).
|
|
13
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
14
|
+
*
|
|
15
|
+
* @throws {DataError} If `step` is `0`.
|
|
16
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
17
|
+
*
|
|
18
|
+
* @returns An array of numbers satisfying the range provided.
|
|
19
|
+
*/
|
|
20
|
+
function range(start, stop, step = 1) {
|
|
21
|
+
const numbers = [];
|
|
22
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
23
|
+
else if (step > 0) {
|
|
24
|
+
if (start > stop) throw new DataError({
|
|
25
|
+
start,
|
|
26
|
+
stop,
|
|
27
|
+
step
|
|
28
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
29
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
30
|
+
} else if (step < 0) {
|
|
31
|
+
if (start < stop) throw new DataError({
|
|
32
|
+
start,
|
|
33
|
+
stop,
|
|
34
|
+
step
|
|
35
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
36
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
37
|
+
}
|
|
38
|
+
return numbers;
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
2
41
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
3
42
|
/**
|
|
4
43
|
* Creates a new array where each element is the result of the provided callback.
|
|
@@ -12,15 +51,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
12
51
|
*
|
|
13
52
|
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
14
53
|
* @param length - The desired length of the resulting array.
|
|
54
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
15
55
|
*
|
|
16
56
|
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
17
57
|
*/
|
|
18
|
-
function fillArray(callback, length = 1) {
|
|
58
|
+
function fillArray(callback, length = 1, options) {
|
|
59
|
+
if (options?.sequential) return (async () => {
|
|
60
|
+
const resolvedArray = [];
|
|
61
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
62
|
+
return resolvedArray;
|
|
63
|
+
})();
|
|
19
64
|
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
20
65
|
return callback(index);
|
|
21
66
|
});
|
|
22
67
|
if (outputArray.some((item) => {
|
|
23
|
-
return item instanceof Promise;
|
|
68
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
24
69
|
})) return Promise.all(outputArray);
|
|
25
70
|
return outputArray;
|
|
26
71
|
}
|
package/dist/v6/index.d.cts
CHANGED
|
@@ -146,7 +146,7 @@ declare class APIError<DataType extends object = Record<PropertyKey, unknown>, E
|
|
|
146
146
|
*
|
|
147
147
|
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
148
148
|
*/
|
|
149
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): APIError<DataType, ErrorCode>;
|
|
149
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): APIError<DataType, ErrorCode>;
|
|
150
150
|
/**
|
|
151
151
|
* Gets the thrown `APIError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `APIError` if no error thrown.
|
|
152
152
|
*
|
|
@@ -158,7 +158,7 @@ declare class APIError<DataType extends object = Record<PropertyKey, unknown>, E
|
|
|
158
158
|
*
|
|
159
159
|
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
160
160
|
*/
|
|
161
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<APIError<DataType, ErrorCode>>;
|
|
161
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<APIError<DataType, ErrorCode>>;
|
|
162
162
|
/**
|
|
163
163
|
* Create a new `APIError` with the error code derived from the status directly.
|
|
164
164
|
*
|
|
@@ -228,7 +228,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
228
228
|
*
|
|
229
229
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
230
230
|
*/
|
|
231
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
231
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
232
232
|
/**
|
|
233
233
|
* Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
|
|
234
234
|
*
|
|
@@ -240,7 +240,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
240
240
|
*
|
|
241
241
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
242
242
|
*/
|
|
243
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
243
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
244
244
|
/**
|
|
245
245
|
* Converts the `DataError` instance to a serialised JSON payload.
|
|
246
246
|
*
|
package/dist/v6/index.d.ts
CHANGED
|
@@ -146,7 +146,7 @@ declare class APIError<DataType extends object = Record<PropertyKey, unknown>, E
|
|
|
146
146
|
*
|
|
147
147
|
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
148
148
|
*/
|
|
149
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): APIError<DataType, ErrorCode>;
|
|
149
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): APIError<DataType, ErrorCode>;
|
|
150
150
|
/**
|
|
151
151
|
* Gets the thrown `APIError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `APIError` if no error thrown.
|
|
152
152
|
*
|
|
@@ -158,7 +158,7 @@ declare class APIError<DataType extends object = Record<PropertyKey, unknown>, E
|
|
|
158
158
|
*
|
|
159
159
|
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
160
160
|
*/
|
|
161
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<APIError<DataType, ErrorCode>>;
|
|
161
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<APIError<DataType, ErrorCode>>;
|
|
162
162
|
/**
|
|
163
163
|
* Create a new `APIError` with the error code derived from the status directly.
|
|
164
164
|
*
|
|
@@ -228,7 +228,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
228
228
|
*
|
|
229
229
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
230
230
|
*/
|
|
231
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
231
|
+
static expectError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
|
|
232
232
|
/**
|
|
233
233
|
* Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
|
|
234
234
|
*
|
|
@@ -240,7 +240,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
240
240
|
*
|
|
241
241
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
242
242
|
*/
|
|
243
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
243
|
+
static expectErrorAsync<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
244
244
|
/**
|
|
245
245
|
* Converts the `DataError` instance to a serialised JSON payload.
|
|
246
246
|
*
|
package/dist/v6/index.js
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
+
//#region src/root/functions/arrayHelpers/range.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates an array of numbers within a given range.
|
|
4
|
+
*
|
|
5
|
+
* - The range is inclusive of `start` and exclusive of `stop`.
|
|
6
|
+
* - The sign of `step` must match the direction of the range.
|
|
7
|
+
*
|
|
8
|
+
* @category Array Helpers
|
|
9
|
+
*
|
|
10
|
+
* @param start - The number to start at (inclusive).
|
|
11
|
+
* @param stop - The number to stop at (exclusive).
|
|
12
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
13
|
+
*
|
|
14
|
+
* @throws {DataError} If `step` is `0`.
|
|
15
|
+
* @throws {DataError} If `step` direction does not match the order of `start` and `stop`.
|
|
16
|
+
*
|
|
17
|
+
* @returns An array of numbers satisfying the range provided.
|
|
18
|
+
*/
|
|
19
|
+
function range(start, stop, step = 1) {
|
|
20
|
+
const numbers = [];
|
|
21
|
+
if (step === 0) throw new DataError({ step }, "ZERO_STEP_SIZE", "Step size cannot be zero.");
|
|
22
|
+
else if (step > 0) {
|
|
23
|
+
if (start > stop) throw new DataError({
|
|
24
|
+
start,
|
|
25
|
+
stop,
|
|
26
|
+
step
|
|
27
|
+
}, "INVALID_BOUNDARIES", "The starting value cannot be bigger than the final value if step is positive");
|
|
28
|
+
for (let i = start; i < stop; i += step) numbers.push(i);
|
|
29
|
+
} else if (step < 0) {
|
|
30
|
+
if (start < stop) throw new DataError({
|
|
31
|
+
start,
|
|
32
|
+
stop,
|
|
33
|
+
step
|
|
34
|
+
}, "INVALID_BOUNDARIES", "The final value cannot be bigger than the starting value if step is negative");
|
|
35
|
+
for (let i = start; i > stop; i += step) numbers.push(i);
|
|
36
|
+
}
|
|
37
|
+
return numbers;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
1
40
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
2
41
|
/**
|
|
3
42
|
* Creates a new array where each element is the result of the provided callback.
|
|
@@ -11,15 +50,21 @@
|
|
|
11
50
|
*
|
|
12
51
|
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
13
52
|
* @param length - The desired length of the resulting array.
|
|
53
|
+
* @param options - Extra options to apply if any item is asynchronous.
|
|
14
54
|
*
|
|
15
55
|
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
16
56
|
*/
|
|
17
|
-
function fillArray(callback, length = 1) {
|
|
57
|
+
function fillArray(callback, length = 1, options) {
|
|
58
|
+
if (options?.sequential) return (async () => {
|
|
59
|
+
const resolvedArray = [];
|
|
60
|
+
for (const index of range(0, length)) resolvedArray.push(await callback(index));
|
|
61
|
+
return resolvedArray;
|
|
62
|
+
})();
|
|
18
63
|
const outputArray = new Array(length).fill(null).map((_, index) => {
|
|
19
64
|
return callback(index);
|
|
20
65
|
});
|
|
21
66
|
if (outputArray.some((item) => {
|
|
22
|
-
return item instanceof Promise;
|
|
67
|
+
return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
|
|
23
68
|
})) return Promise.all(outputArray);
|
|
24
69
|
return outputArray;
|
|
25
70
|
}
|