@react-hive/honey-utils 1.6.0 โ 1.7.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 +190 -109
- package/dist/README.md +190 -109
- package/dist/array.d.ts +77 -15
- package/dist/guards.d.ts +29 -21
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.dev.cjs +99 -42
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/README.md
CHANGED
|
@@ -11,8 +11,6 @@ A lightweight TypeScript utility library providing a collection of helper functi
|
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
---
|
|
15
|
-
|
|
16
14
|
- ๐ **Type Guards** - Functions for runtime type checking
|
|
17
15
|
- ๐งต **String Utilities** - String manipulation and transformation
|
|
18
16
|
- ๐ข **Array Utilities** - Array filtering and manipulation
|
|
@@ -24,8 +22,6 @@ A lightweight TypeScript utility library providing a collection of helper functi
|
|
|
24
22
|
|
|
25
23
|
## Installation
|
|
26
24
|
|
|
27
|
-
---
|
|
28
|
-
|
|
29
25
|
```bash
|
|
30
26
|
# Using npm
|
|
31
27
|
npm install @react-hive/honey-utils
|
|
@@ -39,8 +35,6 @@ pnpm add @react-hive/honey-utils
|
|
|
39
35
|
|
|
40
36
|
## Usage
|
|
41
37
|
|
|
42
|
-
---
|
|
43
|
-
|
|
44
38
|
### Importing
|
|
45
39
|
|
|
46
40
|
```ts
|
|
@@ -56,16 +50,24 @@ import * as HoneyUtils from '@react-hive/honey-utils';
|
|
|
56
50
|
```ts
|
|
57
51
|
import { toKebabCase, camelToDashCase, splitStringIntoWords, hashString } from '@react-hive/honey-utils';
|
|
58
52
|
|
|
59
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Convert string to kebab-case
|
|
55
|
+
*/
|
|
60
56
|
toKebabCase('helloWorld'); // 'hello-world'
|
|
61
57
|
|
|
62
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Convert camelCase to dash-case
|
|
60
|
+
*/
|
|
63
61
|
camelToDashCase('helloWorld'); // 'hello-world'
|
|
64
62
|
|
|
65
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Split string into words
|
|
65
|
+
*/
|
|
66
66
|
splitStringIntoWords('hello world'); // ['hello', 'world']
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Generate a hash from a string
|
|
70
|
+
*/
|
|
69
71
|
const hash = hashString('background-color: red;'); // 'e4k1z0x'
|
|
70
72
|
```
|
|
71
73
|
|
|
@@ -73,95 +75,129 @@ const hash = hashString('background-color: red;'); // 'e4k1z0x'
|
|
|
73
75
|
|
|
74
76
|
```ts
|
|
75
77
|
import {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
78
|
+
boolFilter,
|
|
79
|
+
unique,
|
|
80
|
+
chunk,
|
|
81
|
+
intersection,
|
|
82
|
+
difference,
|
|
83
|
+
mapAsync,
|
|
84
|
+
forAsync,
|
|
85
|
+
reduceAsync,
|
|
86
|
+
filterAsync,
|
|
87
|
+
someAsync,
|
|
88
|
+
everyAsync,
|
|
89
|
+
findAsync,
|
|
90
|
+
pipe,
|
|
91
|
+
compose,
|
|
88
92
|
} from '@react-hive/honey-utils';
|
|
89
93
|
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Filter out falsy values from an array
|
|
96
|
+
*/
|
|
97
|
+
boolFilter([0, 1, false, 2, '', 3, null, undefined, true]);
|
|
92
98
|
// โ [1, 2, 3, true]
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Remove duplicate values from an array
|
|
102
|
+
*/
|
|
103
|
+
unique([1, 2, 2, 3, 1, 4]);
|
|
96
104
|
// โ [1, 2, 3, 4]
|
|
97
105
|
|
|
98
|
-
|
|
99
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Split an array into chunks of specified size
|
|
108
|
+
*/
|
|
109
|
+
chunk([1, 2, 3, 4, 5], 2);
|
|
100
110
|
// โ [[1, 2], [3, 4], [5]]
|
|
101
111
|
|
|
102
|
-
|
|
103
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Find common elements between arrays
|
|
114
|
+
*/
|
|
115
|
+
intersection([1, 2, 3], [2, 3, 4]);
|
|
104
116
|
// โ [2, 3]
|
|
105
117
|
|
|
106
|
-
|
|
107
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Find elements in one array not in another
|
|
120
|
+
*/
|
|
121
|
+
difference([1, 2, 3, 4], [2, 4]);
|
|
108
122
|
// โ [1, 3]
|
|
109
123
|
|
|
110
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Run async operations in parallel and collect results
|
|
126
|
+
*/
|
|
111
127
|
await mapAsync([1, 2, 3], async (n) => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return n * 2;
|
|
128
|
+
await delay(100);
|
|
129
|
+
return n * 2;
|
|
115
130
|
});
|
|
116
131
|
// โ [2, 4, 6]
|
|
117
132
|
|
|
118
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Run async operations sequentially and collect results
|
|
135
|
+
*/
|
|
119
136
|
await forAsync([1, 2, 3], async (n, i) => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return n * i;
|
|
137
|
+
await delay(100);
|
|
138
|
+
return n * i;
|
|
123
139
|
});
|
|
124
140
|
// โ [0, 2, 6]
|
|
125
141
|
|
|
126
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Reduce array asynchronously
|
|
144
|
+
*/
|
|
127
145
|
await reduceAsync([1, 2, 3], async (acc, n) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return acc + n;
|
|
146
|
+
await delay(50);
|
|
147
|
+
return acc + n;
|
|
131
148
|
}, 0);
|
|
132
149
|
// โ 6
|
|
133
150
|
|
|
134
|
-
|
|
151
|
+
/**
|
|
152
|
+
* Filter array asynchronously
|
|
153
|
+
*/
|
|
135
154
|
await filterAsync([1, 2, 3, 4], async (n) => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return n % 2 === 0;
|
|
155
|
+
await delay(30);
|
|
156
|
+
return n % 2 === 0;
|
|
139
157
|
});
|
|
140
158
|
// โ [2, 4]
|
|
141
159
|
|
|
142
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Check if some items match condition asynchronously
|
|
162
|
+
*/
|
|
143
163
|
await someAsync([1, 3, 5], async (n) => {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return n % 2 === 0;
|
|
164
|
+
await delay(10);
|
|
165
|
+
return n % 2 === 0;
|
|
147
166
|
});
|
|
148
167
|
// โ false
|
|
149
168
|
|
|
150
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Check if all items match condition asynchronously
|
|
171
|
+
*/
|
|
151
172
|
await everyAsync([2, 4, 6], async (n) => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return n % 2 === 0;
|
|
173
|
+
await delay(10);
|
|
174
|
+
return n % 2 === 0;
|
|
155
175
|
});
|
|
156
176
|
// โ true
|
|
157
177
|
|
|
158
|
-
|
|
178
|
+
/**
|
|
179
|
+
* Find first matching item asynchronously
|
|
180
|
+
*/
|
|
159
181
|
await findAsync([1, 3, 4, 5], async (n) => {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return n % 2 === 0;
|
|
182
|
+
await delay(20);
|
|
183
|
+
return n % 2 === 0;
|
|
163
184
|
});
|
|
164
185
|
// โ 4
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Compose functions from left to right
|
|
189
|
+
*/
|
|
190
|
+
const double = (n: number) => n * 2;
|
|
191
|
+
const increment = (n: number) => n + 1;
|
|
192
|
+
|
|
193
|
+
pipe(double, increment)(3);
|
|
194
|
+
// โ 7 โ increment(double(3)) โ increment(6)
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Compose functions from right to left
|
|
198
|
+
*/
|
|
199
|
+
compose(increment, double)(3);
|
|
200
|
+
// โ 7 โ increment(double(3)) โ increment(6)
|
|
165
201
|
```
|
|
166
202
|
|
|
167
203
|
### Function Utilities
|
|
@@ -169,19 +205,27 @@ await findAsync([1, 3, 4, 5], async (n) => {
|
|
|
169
205
|
```ts
|
|
170
206
|
import { noop, invokeIfFunction, delay, retry } from '@react-hive/honey-utils';
|
|
171
207
|
|
|
172
|
-
|
|
173
|
-
|
|
208
|
+
/**
|
|
209
|
+
* No-operation function. Does nothing
|
|
210
|
+
*/
|
|
211
|
+
noop();
|
|
174
212
|
|
|
175
|
-
|
|
213
|
+
/**
|
|
214
|
+
* Invoke if function, otherwise return value
|
|
215
|
+
*/
|
|
176
216
|
const fn = (x: number) => x * 2;
|
|
177
217
|
|
|
178
218
|
invokeIfFunction(fn, 5); // 10
|
|
179
219
|
invokeIfFunction('not a function', 5); // 'not a function'
|
|
180
220
|
|
|
181
|
-
|
|
182
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Waits for 1 second before continuing
|
|
223
|
+
*/
|
|
224
|
+
await delay(1000);
|
|
183
225
|
|
|
184
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Retry an async function with configurable options
|
|
228
|
+
*/
|
|
185
229
|
async function fetchData() {
|
|
186
230
|
const response = await fetch('/api/data');
|
|
187
231
|
|
|
@@ -230,65 +274,95 @@ import {
|
|
|
230
274
|
isSet
|
|
231
275
|
} from '@react-hive/honey-utils';
|
|
232
276
|
|
|
233
|
-
|
|
277
|
+
/**
|
|
278
|
+
* Check if value is a string
|
|
279
|
+
*/
|
|
234
280
|
isString('hello'); // true
|
|
235
281
|
isString(123); // false
|
|
236
282
|
|
|
237
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Check if value is a number
|
|
285
|
+
*/
|
|
238
286
|
isNumber(123); // true
|
|
239
287
|
isNumber('123'); // false
|
|
240
288
|
|
|
241
|
-
|
|
289
|
+
/**
|
|
290
|
+
* Check if value is a boolean
|
|
291
|
+
*/
|
|
242
292
|
isBool(true); // true
|
|
243
293
|
isBool('true'); // false
|
|
244
294
|
|
|
245
|
-
|
|
295
|
+
/**
|
|
296
|
+
* Check if value is an object
|
|
297
|
+
*/
|
|
246
298
|
isObject({}); // true
|
|
247
299
|
isObject('object'); // false
|
|
248
300
|
|
|
249
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Check if value is a function
|
|
303
|
+
*/
|
|
250
304
|
isFunction(() => {}); // true
|
|
251
305
|
isFunction({}); // false
|
|
252
306
|
|
|
253
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Check if value is a Promise
|
|
309
|
+
*/
|
|
254
310
|
isPromise(Promise.resolve()); // true
|
|
255
311
|
isPromise({}); // false
|
|
256
312
|
|
|
257
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Check if value is null or undefined
|
|
315
|
+
*/
|
|
258
316
|
isNil(null); // true
|
|
259
317
|
isNil(undefined); // true
|
|
260
318
|
isNil(''); // false
|
|
261
319
|
|
|
262
|
-
|
|
320
|
+
/**
|
|
321
|
+
* Check if value is null, undefined, or empty string
|
|
322
|
+
*/
|
|
263
323
|
isNilOrEmptyString(''); // true
|
|
264
324
|
isNilOrEmptyString(null); // true
|
|
265
325
|
isNilOrEmptyString('hello'); // false
|
|
266
326
|
|
|
267
|
-
|
|
327
|
+
/**
|
|
328
|
+
* Check if value is an array
|
|
329
|
+
*/
|
|
268
330
|
isArray([1, 2, 3]); // true
|
|
269
331
|
isArray({}); // false
|
|
270
332
|
|
|
271
|
-
|
|
333
|
+
/**
|
|
334
|
+
* Check if value is an empty array
|
|
335
|
+
*/
|
|
272
336
|
isEmptyArray([]); // true
|
|
273
337
|
isEmptyArray([1, 2, 3]); // false
|
|
274
338
|
|
|
275
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Check if value is an empty object
|
|
341
|
+
*/
|
|
276
342
|
isEmptyObject({}); // true
|
|
277
343
|
isEmptyObject({ key: 'value' }); // false
|
|
278
344
|
|
|
279
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Check if value is a Date object
|
|
347
|
+
*/
|
|
280
348
|
isDate(new Date()); // true
|
|
281
349
|
isDate('2023-01-01'); // false
|
|
282
350
|
|
|
283
|
-
|
|
351
|
+
/**
|
|
352
|
+
* Check if value is a valid Date object
|
|
353
|
+
*/
|
|
284
354
|
isValidDate(new Date()); // true
|
|
285
355
|
isValidDate(new Date('invalid')); // false
|
|
286
356
|
|
|
287
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Check if value is a RegExp
|
|
359
|
+
*/
|
|
288
360
|
isRegExp(/test/); // true
|
|
289
361
|
isRegExp('test'); // false
|
|
290
362
|
|
|
291
|
-
|
|
363
|
+
/**
|
|
364
|
+
* Check if value is a Map or Set
|
|
365
|
+
*/
|
|
292
366
|
isMap(new Map()); // true
|
|
293
367
|
isSet(new Set()); // true
|
|
294
368
|
```
|
|
@@ -302,13 +376,19 @@ import {
|
|
|
302
376
|
calculatePercentage
|
|
303
377
|
} from '@react-hive/honey-utils';
|
|
304
378
|
|
|
305
|
-
|
|
379
|
+
/**
|
|
380
|
+
* Calculate Euclidean distance between two points
|
|
381
|
+
*/
|
|
306
382
|
calculateEuclideanDistance(0, 0, 3, 4); // 5
|
|
307
383
|
|
|
308
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Calculate moving speed
|
|
386
|
+
*/
|
|
309
387
|
calculateMovingSpeed(100, 5); // 20
|
|
310
388
|
|
|
311
|
-
|
|
389
|
+
/**
|
|
390
|
+
* Calculate percentage of a value
|
|
391
|
+
*/
|
|
312
392
|
calculatePercentage(200, 25); // 50
|
|
313
393
|
```
|
|
314
394
|
|
|
@@ -356,8 +436,6 @@ function divide(a: number, b: number): number {
|
|
|
356
436
|
|
|
357
437
|
## API Documentation
|
|
358
438
|
|
|
359
|
-
---
|
|
360
|
-
|
|
361
439
|
### String Utilities
|
|
362
440
|
|
|
363
441
|
- `toKebabCase(input: string): string` - Converts a string to kebab-case.
|
|
@@ -374,16 +452,18 @@ function divide(a: number, b: number): number {
|
|
|
374
452
|
- `chunk<T>(array: T[], size: number): T[][]` - Splits an array into smaller arrays ("chunks") of the specified size.
|
|
375
453
|
- `intersection<T>(...arrays: T[][]): T[]` - Returns an array of elements that exist in all provided arrays.
|
|
376
454
|
- `difference<T>(array: T[], exclude: T[]): T[]` - Returns a new array that contains items from `array` that are not present in `exclude`.
|
|
455
|
+
- `pipe(...fns: Function[]): Function` - Composes unary functions left-to-right. Returns a new function that applies all given functions in a sequence.
|
|
456
|
+
- `compose(...fns: Function[]): Function` - Composes unary functions **right-to-left**. Same as `pipe`, but applies functions in reverse order.
|
|
377
457
|
|
|
378
458
|
#### Asynchronous Utilities
|
|
379
459
|
|
|
380
|
-
- `forAsync<Item, Result>(array: Item[],
|
|
381
|
-
- `mapAsync<Item, Return>(array: Item[],
|
|
382
|
-
- `reduceAsync<Item, Accumulator>(array: Item[],
|
|
383
|
-
- `filterAsync<Item>(array: Item[],
|
|
384
|
-
- `someAsync<Item>(array: Item[],
|
|
385
|
-
- `everyAsync<Item>(array: Item[],
|
|
386
|
-
- `findAsync<Item>(array: Item[],
|
|
460
|
+
- `forAsync<Item, Result>(array: Item[], predicate: (item, index, array) => Promise<Result>): Promise<Result[]>` - Runs asynchronous operations on each array item *sequentially* and returns the results in the original order.
|
|
461
|
+
- `mapAsync<Item, Return>(array: Item[], predicate: (item, index, array) => Promise<Return>): Promise<Return[]>` - Executes an asynchronous function for each array item *in parallel* and returns a promise of all results.
|
|
462
|
+
- `reduceAsync<Item, Accumulator>(array: Item[], predicate, initialValue): Promise<Accumulator>` - Asynchronously reduces an array to a single accumulated value. Each step waits for the previous promise to resolve.
|
|
463
|
+
- `filterAsync<Item>(array: Item[], predicate): Promise<Item[]>` - Runs an asynchronous filter operation. Only includes items where `predicate(item)` resolves to `true`.
|
|
464
|
+
- `someAsync<Item>(array: Item[], predicate): Promise<boolean>` - Returns `true` if **any** item in the array passes the async predicate.
|
|
465
|
+
- `everyAsync<Item>(array: Item[], predicate): Promise<boolean>` - Returns `true` if **all** items in the array pass the async predicate.
|
|
466
|
+
- `findAsync<Item>(array: Item[], predicate): Promise<Item | null>` - Returns the first array item that passes the async predicate, or `null` if no match is found.
|
|
387
467
|
|
|
388
468
|
### Function Utilities
|
|
389
469
|
|
|
@@ -395,27 +475,28 @@ function divide(a: number, b: number): number {
|
|
|
395
475
|
### Type Guards
|
|
396
476
|
|
|
397
477
|
- `assert(condition: any, message: string): asserts condition` - Asserts that a condition is truthy, throwing an error with the provided message if it's not.
|
|
398
|
-
- `isString(value: unknown): value is string` - Checks if a value is a string
|
|
399
|
-
- `isNumber(value: unknown): value is number` - Checks if a value is a number
|
|
400
|
-
- `isBool(value: unknown): value is boolean` - Checks if a value is a boolean
|
|
401
|
-
- `isObject(value: unknown): value is object` - Checks if a value is an object
|
|
402
|
-
- `isFunction(value: unknown): value is Function` - Checks if a value is a function
|
|
403
|
-
- `isPromise<T = unknown>(value: unknown): value is Promise<T>` - Checks if a value is a Promise
|
|
404
|
-
- `isNil(value: unknown): value is null | undefined` - Checks if a value is null or undefined
|
|
405
|
-
- `isNilOrEmptyString(value: unknown): value is null | undefined` - Checks if a value is null
|
|
478
|
+
- `isString(value: unknown): value is string` - Checks if a value is a `string`.
|
|
479
|
+
- `isNumber(value: unknown): value is number` - Checks if a value is a `number`.
|
|
480
|
+
- `isBool(value: unknown): value is boolean` - Checks if a value is a `boolean`.
|
|
481
|
+
- `isObject(value: unknown): value is object` - Checks if a value is an `object`.
|
|
482
|
+
- `isFunction(value: unknown): value is Function` - Checks if a value is a `function`.
|
|
483
|
+
- `isPromise<T = unknown>(value: unknown): value is Promise<T>` - Checks if a value is a `Promise`.
|
|
484
|
+
- `isNil(value: unknown): value is null | undefined` - Checks if a value is `null` or `undefined`.
|
|
485
|
+
- `isNilOrEmptyString(value: unknown): value is null | undefined` - Checks if a value is `null`, `undefined`, or an empty string.
|
|
406
486
|
- `isArray(value: unknown): value is unknown[]` - Checks if a value is an array.
|
|
407
487
|
- `isEmptyArray(value: unknown): value is []` - Checks if a value is an empty array.
|
|
408
488
|
- `isEmptyObject(value: unknown): value is Record<string, never>` - Checks if a value is an empty object.
|
|
409
|
-
- `isNull(value: unknown): value is null` - Checks if a value is null
|
|
410
|
-
- `isUndefined(value: unknown): value is undefined` - Checks if a value is undefined
|
|
489
|
+
- `isNull(value: unknown): value is null` - Checks if a value is `null`.
|
|
490
|
+
- `isUndefined(value: unknown): value is undefined` - Checks if a value is `undefined`.
|
|
491
|
+
- `isDefined<T>(value: T): value is NonNullable<T>` - Checks if a value is neither `null` nor `undefined`.
|
|
411
492
|
- `isFiniteNumber(value: unknown): value is number` - Checks if a value is a finite number.
|
|
412
493
|
- `isInteger(value: unknown): value is number` - Checks if a value is an integer.
|
|
413
|
-
- `isDate(value: unknown): value is Date` - Checks if a value is a Date object.
|
|
414
|
-
- `isValidDate(value: unknown): value is Date` - Checks if a value is a valid Date object
|
|
415
|
-
- `isRegExp(value: unknown): value is RegExp` - Checks if a value is a RegExp object.
|
|
416
|
-
- `isMap(value: unknown): value is Map<unknown, unknown>` - Checks if a value is a Map
|
|
417
|
-
- `isSet(value: unknown): value is Set<unknown>` - Checks if a value is a Set
|
|
418
|
-
- `isSymbol(value: unknown): value is symbol` - Checks if a value is a Symbol
|
|
494
|
+
- `isDate(value: unknown): value is Date` - Checks if a value is a `Date` object.
|
|
495
|
+
- `isValidDate(value: unknown): value is Date` - Checks if a value is a valid `Date` object.
|
|
496
|
+
- `isRegExp(value: unknown): value is RegExp` - Checks if a value is a `RegExp` object.
|
|
497
|
+
- `isMap(value: unknown): value is Map<unknown, unknown>` - Checks if a value is a `Map`.
|
|
498
|
+
- `isSet(value: unknown): value is Set<unknown>` - Checks if a value is a `Set`.
|
|
499
|
+
- `isSymbol(value: unknown): value is symbol` - Checks if a value is a `Symbol`.
|
|
419
500
|
|
|
420
501
|
### Math Utilities
|
|
421
502
|
|
package/dist/array.d.ts
CHANGED
|
@@ -90,9 +90,9 @@ export declare const difference: <T>(array: T[], exclude: T[]) => T[];
|
|
|
90
90
|
* Useful when order or timing matters (e.g., rate limits, UI updates, animations).
|
|
91
91
|
*
|
|
92
92
|
* @param array - The array of items to iterate over.
|
|
93
|
-
* @param
|
|
93
|
+
* @param predicate - An async function to execute for each item. Must return a value.
|
|
94
94
|
*
|
|
95
|
-
* @returns A promise that resolves with an array of results from each
|
|
95
|
+
* @returns A promise that resolves with an array of results from each predicate call.
|
|
96
96
|
*
|
|
97
97
|
* @example
|
|
98
98
|
* ```ts
|
|
@@ -105,16 +105,16 @@ export declare const difference: <T>(array: T[], exclude: T[]) => T[];
|
|
|
105
105
|
* console.log(results); // [2, 4, 6]
|
|
106
106
|
* ```
|
|
107
107
|
*/
|
|
108
|
-
export declare const forAsync: <Item, Result>(array: Item[],
|
|
108
|
+
export declare const forAsync: <Item, Result>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<Result>) => Promise<Result[]>;
|
|
109
109
|
/**
|
|
110
110
|
* Executes an asynchronous operation on each element of an array and waits for all promises to resolve.
|
|
111
111
|
*
|
|
112
112
|
* @param array - The array of items to operate on.
|
|
113
|
-
* @param
|
|
113
|
+
* @param predicate - The asynchronous operation to perform on each item.
|
|
114
114
|
*
|
|
115
115
|
* @returns A promise that resolves with an array of results after all operations are completed.
|
|
116
116
|
*/
|
|
117
|
-
export declare const mapAsync: <Item, Return>(array: Item[],
|
|
117
|
+
export declare const mapAsync: <Item, Return>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<Return>) => Promise<Return[]>;
|
|
118
118
|
/**
|
|
119
119
|
* A generic function that processes an array asynchronously and filters the results
|
|
120
120
|
* based on the provided async condition.
|
|
@@ -123,29 +123,29 @@ export declare const mapAsync: <Item, Return>(array: Item[], callbackFn: (item:
|
|
|
123
123
|
* @template Return - The type of the items returned by the condition.
|
|
124
124
|
*
|
|
125
125
|
* @param array - An array of items to be processed.
|
|
126
|
-
* @param
|
|
126
|
+
* @param predicate - An async function that returns a condition for each item.
|
|
127
127
|
*
|
|
128
128
|
* @returns A Promise that resolves to an array of items that match the condition.
|
|
129
129
|
*/
|
|
130
|
-
export declare const filterAsync: <Item>(array: Item[],
|
|
130
|
+
export declare const filterAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<Item[]>;
|
|
131
131
|
/**
|
|
132
132
|
* Asynchronously checks if at least one element in the array satisfies the async condition.
|
|
133
133
|
*
|
|
134
134
|
* @param array - The array of items to check.
|
|
135
|
-
* @param
|
|
135
|
+
* @param predicate - An async function that returns a boolean.
|
|
136
136
|
*
|
|
137
137
|
* @returns A promise that resolves to true if any item passes the condition.
|
|
138
138
|
*/
|
|
139
|
-
export declare const someAsync: <Item>(array: Item[],
|
|
139
|
+
export declare const someAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<boolean>;
|
|
140
140
|
/**
|
|
141
141
|
* Asynchronously checks if all elements in the array satisfy the async condition.
|
|
142
142
|
*
|
|
143
143
|
* @param array - The array of items to check.
|
|
144
|
-
* @param
|
|
144
|
+
* @param predicate - An async function that returns a boolean.
|
|
145
145
|
*
|
|
146
146
|
* @returns A promise that resolves to true if all items pass the condition.
|
|
147
147
|
*/
|
|
148
|
-
export declare const everyAsync: <Item>(array: Item[],
|
|
148
|
+
export declare const everyAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<boolean>;
|
|
149
149
|
/**
|
|
150
150
|
* Asynchronously reduces an array to a single accumulated value.
|
|
151
151
|
*
|
|
@@ -153,18 +153,80 @@ export declare const everyAsync: <Item>(array: Item[], callbackFn: (item: Item,
|
|
|
153
153
|
* @template Accumulator - The type of the accumulated result.
|
|
154
154
|
*
|
|
155
155
|
* @param array - The array to reduce.
|
|
156
|
-
* @param
|
|
156
|
+
* @param predicate - The async reducer function that processes each item and returns the updated accumulator.
|
|
157
157
|
* @param initialValue - The initial accumulator value.
|
|
158
158
|
*
|
|
159
159
|
* @returns A promise that resolves to the final accumulated result.
|
|
160
160
|
*/
|
|
161
|
-
export declare const reduceAsync: <Item, Accumulator>(array: Item[],
|
|
161
|
+
export declare const reduceAsync: <Item, Accumulator>(array: Item[], predicate: (accumulator: Accumulator, item: Item, index: number, array: Item[]) => Promise<Accumulator>, initialValue: Accumulator) => Promise<Accumulator>;
|
|
162
162
|
/**
|
|
163
163
|
* Asynchronously finds the first element that satisfies the async condition.
|
|
164
164
|
*
|
|
165
165
|
* @param array - The array of items to search.
|
|
166
|
-
* @param
|
|
166
|
+
* @param predicate - An async function that returns a boolean.
|
|
167
167
|
*
|
|
168
168
|
* @returns A promise that resolves to the found item or null if none match.
|
|
169
169
|
*/
|
|
170
|
-
export declare const findAsync: <Item>(array: Item[],
|
|
170
|
+
export declare const findAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<Item | null>;
|
|
171
|
+
type PipeFn = (arg: unknown) => unknown;
|
|
172
|
+
type Pipe = {
|
|
173
|
+
<A, B>(fn1: (a: A) => B): (a: A) => B;
|
|
174
|
+
<A, B, C>(fn1: (a: A) => B, fn2: (b: B) => C): (a: A) => C;
|
|
175
|
+
<A, B, C, D>(fn1: (a: A) => B, fn2: (b: B) => C, fn3: (c: C) => D): (a: A) => D;
|
|
176
|
+
<A, B, C, D, E>(fn1: (a: A) => B, fn2: (b: B) => C, fn3: (c: C) => D, fn4: (d: D) => E): (a: A) => E;
|
|
177
|
+
<A, B, C, D, E, F>(fn1: (a: A) => B, fn2: (b: B) => C, fn3: (c: C) => D, fn4: (d: D) => E, fn5: (e: E) => F): (a: A) => F;
|
|
178
|
+
(...fns: PipeFn[]): (arg: unknown) => unknown;
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Composes multiple unary functions into a single function, applying them from left to right.
|
|
182
|
+
*
|
|
183
|
+
* Useful for building a data processing pipeline where the output of one function becomes the input of the next.
|
|
184
|
+
*
|
|
185
|
+
* Types are inferred up to 5 chained functions for full type safety. Beyond that, it falls back to the unknown.
|
|
186
|
+
*
|
|
187
|
+
* @param fns - A list of unary functions to compose.
|
|
188
|
+
*
|
|
189
|
+
* @returns A new function that applies all functions from left to right.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* const add = (x: number) => x + 1;
|
|
194
|
+
* const double = (x: number) => x * 2;
|
|
195
|
+
* const toStr = (x: number) => `Result: ${x}`;
|
|
196
|
+
*
|
|
197
|
+
* const result = pipe(add, double, toStr)(2);
|
|
198
|
+
* // => 'Result: 6'
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export declare const pipe: Pipe;
|
|
202
|
+
type ComposeFn = (arg: unknown) => unknown;
|
|
203
|
+
type Compose = {
|
|
204
|
+
<A, R>(fn1: (a: A) => R): (a: A) => R;
|
|
205
|
+
<A, B, R>(fn1: (b: B) => R, fn2: (a: A) => B): (a: A) => R;
|
|
206
|
+
<A, B, C, R>(fn1: (c: C) => R, fn2: (b: B) => C, fn3: (a: A) => B): (a: A) => R;
|
|
207
|
+
<A, B, C, D, R>(fn1: (d: D) => R, fn2: (c: C) => D, fn3: (b: B) => C, fn4: (a: A) => B): (a: A) => R;
|
|
208
|
+
<A, B, C, D, E, R>(fn1: (e: E) => R, fn2: (d: D) => E, fn3: (c: C) => D, fn4: (b: B) => C, fn5: (a: A) => B): (a: A) => R;
|
|
209
|
+
(...fns: ComposeFn[]): (arg: unknown) => unknown;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Composes multiple unary functions into a single function, applying them from **right to left**.
|
|
213
|
+
*
|
|
214
|
+
* Often used for building functional pipelines where the innermost function runs first.
|
|
215
|
+
* Types are inferred up to 5 chained functions for full type safety.
|
|
216
|
+
*
|
|
217
|
+
* @param fns - A list of unary functions to compose.
|
|
218
|
+
*
|
|
219
|
+
* @returns A new function that applies all functions from right to left.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* const add = (x: number) => x + 1;
|
|
224
|
+
* const double = (x: number) => x * 2;
|
|
225
|
+
* const toStr = (x: number) => `Result: ${x}`;
|
|
226
|
+
*
|
|
227
|
+
* const result = compose(toStr, double, add)(2);
|
|
228
|
+
* // => 'Result: 6'
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export declare const compose: Compose;
|
|
232
|
+
export {};
|