@react-hive/honey-utils 1.7.0 → 1.9.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 CHANGED
@@ -75,18 +75,11 @@ const hash = hashString('background-color: red;'); // 'e4k1z0x'
75
75
 
76
76
  ```ts
77
77
  import {
78
- boolFilter,
78
+ compact,
79
79
  unique,
80
80
  chunk,
81
81
  intersection,
82
82
  difference,
83
- mapAsync,
84
- forAsync,
85
- reduceAsync,
86
- filterAsync,
87
- someAsync,
88
- everyAsync,
89
- findAsync,
90
83
  pipe,
91
84
  compose,
92
85
  } from '@react-hive/honey-utils';
@@ -94,7 +87,7 @@ import {
94
87
  /**
95
88
  * Filter out falsy values from an array
96
89
  */
97
- boolFilter([0, 1, false, 2, '', 3, null, undefined, true]);
90
+ compact([0, 1, false, 2, '', 3, null, undefined, true]);
98
91
  // ➜ [1, 2, 3, true]
99
92
 
100
93
  /**
@@ -121,10 +114,39 @@ intersection([1, 2, 3], [2, 3, 4]);
121
114
  difference([1, 2, 3, 4], [2, 4]);
122
115
  // ➜ [1, 3]
123
116
 
117
+ /**
118
+ * Compose functions from left to right
119
+ */
120
+ const double = (n: number) => n * 2;
121
+ const increment = (n: number) => n + 1;
122
+
123
+ pipe(double, increment)(3);
124
+ // ➜ 7 → increment(double(3)) → increment(6)
125
+
126
+ /**
127
+ * Compose functions from right to left
128
+ */
129
+ compose(increment, double)(3);
130
+ // ➜ 7 → increment(double(3)) → increment(6)
131
+ ```
132
+
133
+ ### Asynchronous Utilities
134
+
135
+ ```ts
136
+ import {
137
+ runParallel,
138
+ runSequential,
139
+ reduceAsync,
140
+ filterAsync,
141
+ someAsync,
142
+ everyAsync,
143
+ findAsync,
144
+ } from '@react-hive/honey-utils';
145
+
124
146
  /**
125
147
  * Run async operations in parallel and collect results
126
148
  */
127
- await mapAsync([1, 2, 3], async (n) => {
149
+ await runParallel([1, 2, 3], async (n) => {
128
150
  await delay(100);
129
151
  return n * 2;
130
152
  });
@@ -133,7 +155,7 @@ await mapAsync([1, 2, 3], async (n) => {
133
155
  /**
134
156
  * Run async operations sequentially and collect results
135
157
  */
136
- await forAsync([1, 2, 3], async (n, i) => {
158
+ await runSequential([1, 2, 3], async (n, i) => {
137
159
  await delay(100);
138
160
  return n * i;
139
161
  });
@@ -183,21 +205,6 @@ await findAsync([1, 3, 4, 5], async (n) => {
183
205
  return n % 2 === 0;
184
206
  });
185
207
  // ➜ 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)
201
208
  ```
202
209
 
203
210
  ### Function Utilities
@@ -445,9 +452,7 @@ function divide(a: number, b: number): number {
445
452
 
446
453
  ### Array Utilities
447
454
 
448
- #### Synchronous Utilities
449
-
450
- - `boolFilter<T>(array: (T | false | null | undefined)[]): T[]` - Filters out falsy values (`false`, `null`, `undefined`) from an array while keeping valid items.
455
+ - `compact<T>(array: (T | Falsy)[]): T[]` – Returns a new array with all falsy values (false, null, undefined, 0, '', NaN) removed, preserving only truthy items of type `T`.
451
456
  - `unique<T>(array: T[]): T[]` - Returns a new array with all duplicate elements removed. Keeps only the first occurrence of each value.
452
457
  - `chunk<T>(array: T[], size: number): T[][]` - Splits an array into smaller arrays ("chunks") of the specified size.
453
458
  - `intersection<T>(...arrays: T[][]): T[]` - Returns an array of elements that exist in all provided arrays.
@@ -455,16 +460,6 @@ function divide(a: number, b: number): number {
455
460
  - `pipe(...fns: Function[]): Function` - Composes unary functions left-to-right. Returns a new function that applies all given functions in a sequence.
456
461
  - `compose(...fns: Function[]): Function` - Composes unary functions **right-to-left**. Same as `pipe`, but applies functions in reverse order.
457
462
 
458
- #### Asynchronous Utilities
459
-
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.
467
-
468
463
  ### Function Utilities
469
464
 
470
465
  - `noop(): void` - A no-operation function.
@@ -501,7 +496,7 @@ function divide(a: number, b: number): number {
501
496
  ### Math Utilities
502
497
 
503
498
  - `calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number` - Calculates the Euclidean distance between two points.
504
- - `calculateMovingSpeed(delta: number, elapsedTime: number): number` - Calculates moving speed.
499
+ - `calculateMovingSpeed(distance: number, elapsedTime: number): number` - Calculates moving speed.
505
500
  - `calculatePercentage(value: number, percentage: number): number` - Calculates the specified percentage of a value.
506
501
 
507
502
  ### DOM Utilities
@@ -510,6 +505,16 @@ function divide(a: number, b: number): number {
510
505
  - `cloneBlob(blob: Blob): Blob` - Creates a clone of a Blob object with the same content and type as the original.
511
506
  - `convertBlobToFile(blob: Blob, fileName: string): File` - Converts a Blob object into a File object with the specified name.
512
507
 
508
+ ### Asynchronous Utilities
509
+
510
+ - `runSequential<Item, Result>(array: Item[], fn: (item, index, array) => Promise<Result>): Promise<Result[]>` - Runs asynchronous operations on each array item *sequentially* and returns the results in the original order.
511
+ - `runParallel<Item, Result>(array: Item[], fn: (item, index, array) => Promise<Result>): Promise<Result[]>` - Executes an asynchronous function for each array item *in parallel* and returns a promise of all results.
512
+ - `reduceAsync<Item, Accumulator>(array: Item[], fn, initialValue): Promise<Accumulator>` - Asynchronously reduces an array to a single accumulated value. Each step waits for the previous promise to resolve.
513
+ - `filterAsync<Item>(array: Item[], predicate): Promise<Item[]>` - Runs an asynchronous filter operation. Only includes items where `predicate(item)` resolves to `true`.
514
+ - `someAsync<Item>(array: Item[], predicate): Promise<boolean>` - Returns `true` if **any** item in the array passes the async predicate.
515
+ - `everyAsync<Item>(array: Item[], predicate): Promise<boolean>` - Returns `true` if **all** items in the array pass the async predicate.
516
+ - `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.
517
+
513
518
  ## Contributing
514
519
 
515
520
  Contributions are welcome! Please feel free to submit a Pull Request.
package/dist/README.md CHANGED
@@ -75,18 +75,11 @@ const hash = hashString('background-color: red;'); // 'e4k1z0x'
75
75
 
76
76
  ```ts
77
77
  import {
78
- boolFilter,
78
+ compact,
79
79
  unique,
80
80
  chunk,
81
81
  intersection,
82
82
  difference,
83
- mapAsync,
84
- forAsync,
85
- reduceAsync,
86
- filterAsync,
87
- someAsync,
88
- everyAsync,
89
- findAsync,
90
83
  pipe,
91
84
  compose,
92
85
  } from '@react-hive/honey-utils';
@@ -94,7 +87,7 @@ import {
94
87
  /**
95
88
  * Filter out falsy values from an array
96
89
  */
97
- boolFilter([0, 1, false, 2, '', 3, null, undefined, true]);
90
+ compact([0, 1, false, 2, '', 3, null, undefined, true]);
98
91
  // ➜ [1, 2, 3, true]
99
92
 
100
93
  /**
@@ -121,10 +114,39 @@ intersection([1, 2, 3], [2, 3, 4]);
121
114
  difference([1, 2, 3, 4], [2, 4]);
122
115
  // ➜ [1, 3]
123
116
 
117
+ /**
118
+ * Compose functions from left to right
119
+ */
120
+ const double = (n: number) => n * 2;
121
+ const increment = (n: number) => n + 1;
122
+
123
+ pipe(double, increment)(3);
124
+ // ➜ 7 → increment(double(3)) → increment(6)
125
+
126
+ /**
127
+ * Compose functions from right to left
128
+ */
129
+ compose(increment, double)(3);
130
+ // ➜ 7 → increment(double(3)) → increment(6)
131
+ ```
132
+
133
+ ### Asynchronous Utilities
134
+
135
+ ```ts
136
+ import {
137
+ runParallel,
138
+ runSequential,
139
+ reduceAsync,
140
+ filterAsync,
141
+ someAsync,
142
+ everyAsync,
143
+ findAsync,
144
+ } from '@react-hive/honey-utils';
145
+
124
146
  /**
125
147
  * Run async operations in parallel and collect results
126
148
  */
127
- await mapAsync([1, 2, 3], async (n) => {
149
+ await runParallel([1, 2, 3], async (n) => {
128
150
  await delay(100);
129
151
  return n * 2;
130
152
  });
@@ -133,7 +155,7 @@ await mapAsync([1, 2, 3], async (n) => {
133
155
  /**
134
156
  * Run async operations sequentially and collect results
135
157
  */
136
- await forAsync([1, 2, 3], async (n, i) => {
158
+ await runSequential([1, 2, 3], async (n, i) => {
137
159
  await delay(100);
138
160
  return n * i;
139
161
  });
@@ -183,21 +205,6 @@ await findAsync([1, 3, 4, 5], async (n) => {
183
205
  return n % 2 === 0;
184
206
  });
185
207
  // ➜ 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)
201
208
  ```
202
209
 
203
210
  ### Function Utilities
@@ -445,9 +452,7 @@ function divide(a: number, b: number): number {
445
452
 
446
453
  ### Array Utilities
447
454
 
448
- #### Synchronous Utilities
449
-
450
- - `boolFilter<T>(array: (T | false | null | undefined)[]): T[]` - Filters out falsy values (`false`, `null`, `undefined`) from an array while keeping valid items.
455
+ - `compact<T>(array: (T | Falsy)[]): T[]` – Returns a new array with all falsy values (false, null, undefined, 0, '', NaN) removed, preserving only truthy items of type `T`.
451
456
  - `unique<T>(array: T[]): T[]` - Returns a new array with all duplicate elements removed. Keeps only the first occurrence of each value.
452
457
  - `chunk<T>(array: T[], size: number): T[][]` - Splits an array into smaller arrays ("chunks") of the specified size.
453
458
  - `intersection<T>(...arrays: T[][]): T[]` - Returns an array of elements that exist in all provided arrays.
@@ -455,16 +460,6 @@ function divide(a: number, b: number): number {
455
460
  - `pipe(...fns: Function[]): Function` - Composes unary functions left-to-right. Returns a new function that applies all given functions in a sequence.
456
461
  - `compose(...fns: Function[]): Function` - Composes unary functions **right-to-left**. Same as `pipe`, but applies functions in reverse order.
457
462
 
458
- #### Asynchronous Utilities
459
-
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.
467
-
468
463
  ### Function Utilities
469
464
 
470
465
  - `noop(): void` - A no-operation function.
@@ -501,7 +496,7 @@ function divide(a: number, b: number): number {
501
496
  ### Math Utilities
502
497
 
503
498
  - `calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number` - Calculates the Euclidean distance between two points.
504
- - `calculateMovingSpeed(delta: number, elapsedTime: number): number` - Calculates moving speed.
499
+ - `calculateMovingSpeed(distance: number, elapsedTime: number): number` - Calculates moving speed.
505
500
  - `calculatePercentage(value: number, percentage: number): number` - Calculates the specified percentage of a value.
506
501
 
507
502
  ### DOM Utilities
@@ -510,6 +505,16 @@ function divide(a: number, b: number): number {
510
505
  - `cloneBlob(blob: Blob): Blob` - Creates a clone of a Blob object with the same content and type as the original.
511
506
  - `convertBlobToFile(blob: Blob, fileName: string): File` - Converts a Blob object into a File object with the specified name.
512
507
 
508
+ ### Asynchronous Utilities
509
+
510
+ - `runSequential<Item, Result>(array: Item[], fn: (item, index, array) => Promise<Result>): Promise<Result[]>` - Runs asynchronous operations on each array item *sequentially* and returns the results in the original order.
511
+ - `runParallel<Item, Result>(array: Item[], fn: (item, index, array) => Promise<Result>): Promise<Result[]>` - Executes an asynchronous function for each array item *in parallel* and returns a promise of all results.
512
+ - `reduceAsync<Item, Accumulator>(array: Item[], fn, initialValue): Promise<Accumulator>` - Asynchronously reduces an array to a single accumulated value. Each step waits for the previous promise to resolve.
513
+ - `filterAsync<Item>(array: Item[], predicate): Promise<Item[]>` - Runs an asynchronous filter operation. Only includes items where `predicate(item)` resolves to `true`.
514
+ - `someAsync<Item>(array: Item[], predicate): Promise<boolean>` - Returns `true` if **any** item in the array passes the async predicate.
515
+ - `everyAsync<Item>(array: Item[], predicate): Promise<boolean>` - Returns `true` if **all** items in the array pass the async predicate.
516
+ - `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.
517
+
513
518
  ## Contributing
514
519
 
515
520
  Contributions are welcome! Please feel free to submit a Pull Request.
package/dist/array.d.ts CHANGED
@@ -1,16 +1,26 @@
1
1
  /**
2
- * Filters out `null`, `undefined`, and other falsy values from an array,
3
- * returning a typed array of only truthy `Item` values.
2
+ * Represents all falsy values.
3
+ */
4
+ type Falsy = false | null | undefined | 0 | '' | typeof NaN;
5
+ /**
6
+ * Removes all falsy values from an array.
4
7
  *
5
- * Useful when working with optional or nullable items that need to be sanitized.
8
+ * Falsy values include: `false`, `0`, `''` (empty string), `null`, `undefined`, and `NaN`.
6
9
  *
7
- * @template T - The type of the items in the array.
10
+ * Useful for cleaning up arrays with optional, nullable, or conditionally included items.
8
11
  *
9
- * @param array - An array of items that may include `null`, `undefined`, or falsy values.
12
+ * @template T - The type of the truthy items.
10
13
  *
11
- * @returns A new array containing only truthy `Item` values.
14
+ * @param array - An array possibly containing falsy values.
15
+ *
16
+ * @returns A new array containing only truthy values of type `T`.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * compact([0, 1, false, 2, '', 3, null, undefined, NaN]); // [1, 2, 3]
21
+ * ```
12
22
  */
13
- export declare const boolFilter: <T>(array: (T | false | null | undefined)[]) => T[];
23
+ export declare const compact: <T>(array: (T | Falsy)[]) => T[];
14
24
  /**
15
25
  * Returns a new array with duplicate values removed.
16
26
  *
@@ -82,92 +92,6 @@ export declare const intersection: <T>(...arrays: T[][]) => T[];
82
92
  * ```
83
93
  */
84
94
  export declare const difference: <T>(array: T[], exclude: T[]) => T[];
85
- /**
86
- * Asynchronously iterates over an array and executes an async function on each item sequentially,
87
- * collecting the results.
88
- *
89
- * Unlike `Promise.all`, this runs each promise one after another (not in parallel).
90
- * Useful when order or timing matters (e.g., rate limits, UI updates, animations).
91
- *
92
- * @param array - The array of items to iterate over.
93
- * @param predicate - An async function to execute for each item. Must return a value.
94
- *
95
- * @returns A promise that resolves with an array of results from each predicate call.
96
- *
97
- * @example
98
- * ```ts
99
- * const results = await forAsync([1, 2, 3], async (item) => {
100
- * await delay(100);
101
- *
102
- * return item * 2;
103
- * });
104
- *
105
- * console.log(results); // [2, 4, 6]
106
- * ```
107
- */
108
- export declare const forAsync: <Item, Result>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<Result>) => Promise<Result[]>;
109
- /**
110
- * Executes an asynchronous operation on each element of an array and waits for all promises to resolve.
111
- *
112
- * @param array - The array of items to operate on.
113
- * @param predicate - The asynchronous operation to perform on each item.
114
- *
115
- * @returns A promise that resolves with an array of results after all operations are completed.
116
- */
117
- export declare const mapAsync: <Item, Return>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<Return>) => Promise<Return[]>;
118
- /**
119
- * A generic function that processes an array asynchronously and filters the results
120
- * based on the provided async condition.
121
- *
122
- * @template Item - The type of the items in the array.
123
- * @template Return - The type of the items returned by the condition.
124
- *
125
- * @param array - An array of items to be processed.
126
- * @param predicate - An async function that returns a condition for each item.
127
- *
128
- * @returns A Promise that resolves to an array of items that match the condition.
129
- */
130
- export declare const filterAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<Item[]>;
131
- /**
132
- * Asynchronously checks if at least one element in the array satisfies the async condition.
133
- *
134
- * @param array - The array of items to check.
135
- * @param predicate - An async function that returns a boolean.
136
- *
137
- * @returns A promise that resolves to true if any item passes the condition.
138
- */
139
- export declare const someAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<boolean>;
140
- /**
141
- * Asynchronously checks if all elements in the array satisfy the async condition.
142
- *
143
- * @param array - The array of items to check.
144
- * @param predicate - An async function that returns a boolean.
145
- *
146
- * @returns A promise that resolves to true if all items pass the condition.
147
- */
148
- export declare const everyAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<boolean>;
149
- /**
150
- * Asynchronously reduces an array to a single accumulated value.
151
- *
152
- * @template Item - The type of items in the array.
153
- * @template Accumulator - The type of the accumulated result.
154
- *
155
- * @param array - The array to reduce.
156
- * @param predicate - The async reducer function that processes each item and returns the updated accumulator.
157
- * @param initialValue - The initial accumulator value.
158
- *
159
- * @returns A promise that resolves to the final accumulated result.
160
- */
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
- /**
163
- * Asynchronously finds the first element that satisfies the async condition.
164
- *
165
- * @param array - The array of items to search.
166
- * @param predicate - An async function that returns a boolean.
167
- *
168
- * @returns A promise that resolves to the found item or null if none match.
169
- */
170
- export declare const findAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<Item | null>;
171
95
  type PipeFn = (arg: unknown) => unknown;
172
96
  type Pipe = {
173
97
  <A, B>(fn1: (a: A) => B): (a: A) => B;
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Asynchronously iterates over an array and executes an async function on each item sequentially,
3
+ * collecting the results.
4
+ *
5
+ * Useful when order or timing matters (e.g., rate limits, UI updates, animations).
6
+ *
7
+ * @param array - The array of items to iterate over.
8
+ * @param fn - An async function to execute for each item. Must return a value.
9
+ *
10
+ * @returns A promise that resolves with an array of results from each function call.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const results = await runSequential([1, 2, 3], async (item) => {
15
+ * await delay(100);
16
+ *
17
+ * return item * 2;
18
+ * });
19
+ *
20
+ * console.log(results); // [2, 4, 6]
21
+ * ```
22
+ */
23
+ export declare const runSequential: <Item, Result>(array: Item[], fn: (item: Item, index: number, array: Item[]) => Promise<Result>) => Promise<Result[]>;
24
+ /**
25
+ * Executes an asynchronous operation on each element of an array and waits for all promises to resolve.
26
+ *
27
+ * @param array - The array of items to operate on.
28
+ * @param fn - The asynchronous operation to perform on each item.
29
+ *
30
+ * @returns A promise that resolves with an array of results after all operations are completed.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const results = await runParallel([1, 2, 3], async (item) => {
35
+ * await delay(100);
36
+ *
37
+ * return item * 2;
38
+ * });
39
+ *
40
+ * console.log(results); // [2, 4, 6]
41
+ * ```
42
+ */
43
+ export declare const runParallel: <Item, Result>(array: Item[], fn: (item: Item, index: number, array: Item[]) => Promise<Result>) => Promise<Result[]>;
44
+ /**
45
+ * Asynchronously filters an array based on a provided async predicate function.
46
+ *
47
+ * Each item is passed to the `predicate` function in parallel, and only the items
48
+ * for which the predicate resolves to `true` are included in the final result.
49
+ *
50
+ * Useful for filtering based on asynchronous conditions such as API calls,
51
+ * file system access, or any other delayed operations.
52
+ *
53
+ * @template Item - The type of the items in the input array.
54
+ *
55
+ * @param array - The array of items to filter.
56
+ * @param predicate - An async function that returns a boolean indicating whether to keep each item.
57
+ * Receives `(item, index, array)` as arguments.
58
+ *
59
+ * @returns A promise that resolves to a new array containing only the items for which the predicate returned `true`.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * // Filter numbers that are even after a simulated delay
64
+ * const result = await filterAsync([1, 2, 3, 4], async (num) => {
65
+ * await delay(100);
66
+ *
67
+ * return num % 2 === 0;
68
+ * });
69
+ *
70
+ * console.log(result); // [2, 4]
71
+ * ```
72
+ */
73
+ export declare const filterAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<Item[]>;
74
+ /**
75
+ * Asynchronously checks if at least one element in the array satisfies the async condition.
76
+ *
77
+ * @param array - The array of items to check.
78
+ * @param predicate - An async function that returns a boolean.
79
+ *
80
+ * @returns A promise that resolves to true if any item passes the condition.
81
+ */
82
+ export declare const someAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<boolean>;
83
+ /**
84
+ * Asynchronously checks if all elements in the array satisfy the async condition.
85
+ *
86
+ * @param array - The array of items to check.
87
+ * @param predicate - An async function that returns a boolean.
88
+ *
89
+ * @returns A promise that resolves to true if all items pass the condition.
90
+ */
91
+ export declare const everyAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<boolean>;
92
+ /**
93
+ * Asynchronously reduces an array to a single accumulated value.
94
+ *
95
+ * @template Item - The type of items in the array.
96
+ * @template Accumulator - The type of the accumulated result.
97
+ *
98
+ * @param array - The array to reduce.
99
+ * @param fn - The async reducer function that processes each item and returns the updated accumulator.
100
+ * @param initialValue - The initial accumulator value.
101
+ *
102
+ * @returns A promise that resolves to the final accumulated result.
103
+ */
104
+ export declare const reduceAsync: <Item, Accumulator>(array: Item[], fn: (accumulator: Accumulator, item: Item, index: number, array: Item[]) => Promise<Accumulator>, initialValue: Accumulator) => Promise<Accumulator>;
105
+ /**
106
+ * Asynchronously finds the first element that satisfies the async condition.
107
+ *
108
+ * @param array - The array of items to search.
109
+ * @param predicate - An async function that returns a boolean.
110
+ *
111
+ * @returns A promise that resolves to the found item or null if none match.
112
+ */
113
+ export declare const findAsync: <Item>(array: Item[], predicate: (item: Item, index: number, array: Item[]) => Promise<boolean>) => Promise<Item | null>;
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- (()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{assert:()=>s,boolFilter:()=>F,calculateEuclideanDistance:()=>K,calculateMovingSpeed:()=>U,calculatePercentage:()=>W,camelToDashCase:()=>n,chunk:()=>D,cloneBlob:()=>H,compose:()=>q,convertBlobToFile:()=>J,delay:()=>Z,difference:()=>x,everyAsync:()=>I,filterAsync:()=>X,findAsync:()=>$,forAsync:()=>B,hashString:()=>i,intersection:()=>E,invokeIfFunction:()=>V,isArray:()=>h,isBool:()=>p,isDate:()=>A,isDefined:()=>u,isEmptyArray:()=>d,isEmptyObject:()=>m,isFiniteNumber:()=>j,isFunction:()=>b,isInteger:()=>C,isMap:()=>M,isNil:()=>l,isNilOrEmptyString:()=>c,isNull:()=>o,isNumber:()=>f,isObject:()=>g,isPromise:()=>w,isRegExp:()=>v,isSet:()=>k,isString:()=>y,isSymbol:()=>O,isUndefined:()=>P,isValidDate:()=>S,mapAsync:()=>T,noop:()=>z,parse2DMatrix:()=>G,pipe:()=>L,reduceAsync:()=>R,retry:()=>_,someAsync:()=>Y,splitStringIntoWords:()=>a,toKebabCase:()=>r,unique:()=>N});const r=e=>e.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase(),n=e=>{const t=e.charAt(0),r=e.slice(1);return t.toLowerCase()+r.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`)},a=e=>e.split(" ").filter(Boolean),i=e=>{let t=5381;for(let r=0;r<e.length;r++)t=33*t^e.charCodeAt(r);return(t>>>0).toString(36)};function s(e,t){if(!e)throw new Error(t)}const o=e=>null===e,l=e=>null==e,c=e=>""===e||l(e),u=e=>null!=e,y=e=>"string"==typeof e,f=e=>"number"==typeof e,p=e=>"boolean"==typeof e,g=e=>"object"==typeof e,m=e=>g(e)&&!o(e)&&0===Object.keys(e).length,h=e=>Array.isArray(e),d=e=>h(e)&&0===e.length,b=e=>"function"==typeof e,w=e=>b(e?.then),A=e=>e instanceof Date,S=e=>A(e)&&!isNaN(e.getTime()),v=e=>e instanceof RegExp,M=e=>e instanceof Map,k=e=>e instanceof Set,O=e=>"symbol"==typeof e,P=e=>void 0===e,j=e=>f(e)&&isFinite(e),C=e=>f(e)&&Number.isInteger(e),F=e=>e.filter(Boolean),N=e=>[...new Set(e)],D=(e,t)=>(s(t>0,"Chunk size must be greater than 0"),Array.from({length:Math.ceil(e.length/t)},(r,n)=>e.slice(n*t,(n+1)*t))),E=(...e)=>{if(0===e.length)return[];if(1===e.length)return[...e[0]];const[t,...r]=e;return N(t).filter(e=>r.every(t=>t.includes(e)))},x=(e,t)=>e.filter(e=>!t.includes(e)),B=async(e,t)=>{const r=[];for(let n=0;n<e.length;n++)r.push(await t(e[n],n,e));return r},T=async(e,t)=>Promise.all(e.map(t)),X=async(e,t)=>{const r=await T(e,async(e,r,n)=>!!await t(e,r,n)&&e);return F(r)},Y=async(e,t)=>{for(let r=0;r<e.length;r++)if(await t(e[r],r,e))return!0;return!1},I=async(e,t)=>{for(let r=0;r<e.length;r++)if(!await t(e[r],r,e))return!1;return!0},R=async(e,t,r)=>{let n=r;for(let r=0;r<e.length;r++)n=await t(n,e[r],r,e);return n},$=async(e,t)=>{for(let r=0;r<e.length;r++)if(await t(e[r],r,e))return e[r];return null},L=(...e)=>t=>e.reduce((e,t)=>t(e),t),q=(...e)=>t=>e.reduceRight((e,t)=>t(e),t),z=()=>{},V=(e,...t)=>"function"==typeof e?e(...t):e,Z=e=>new Promise(t=>setTimeout(t,e)),_=(e,{maxAttempts:t=3,delayMs:r=300,backoff:n=!0,onRetry:a}={})=>async(...i)=>{let s;for(let o=1;o<=t;o++)try{return await e(...i)}catch(e){if(s=e,o<t){a?.(o,e);const t=n?r*2**(o-1):r;await Z(t)}}throw s},K=(e,t,r,n)=>{const a=r-e,i=n-t;return Math.sqrt(a**2+i**2)},U=(e,t)=>Math.abs(e/t),W=(e,t)=>e*t/100,G=e=>{const t=window.getComputedStyle(e).getPropertyValue("transform").match(/^matrix\((.+)\)$/);if(!t)return{translateX:0,translateY:0,scaleX:1,scaleY:1,skewX:0,skewY:0};const[r,n,a,i,s,o]=t[1].split(", ").map(parseFloat);return{translateX:s,translateY:o,scaleX:r,scaleY:i,skewX:a,skewY:n}},H=e=>new Blob([e],{type:e.type}),J=(e,t)=>new File([e],t,{type:e.type});module.exports=t})();
1
+ (()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function r(e,t){if(!e)throw new Error(t)}e.r(t),e.d(t,{assert:()=>r,calculateEuclideanDistance:()=>K,calculateMovingSpeed:()=>U,calculatePercentage:()=>W,camelToDashCase:()=>$,chunk:()=>j,cloneBlob:()=>H,compact:()=>k,compose:()=>E,convertBlobToFile:()=>J,delay:()=>Z,difference:()=>N,everyAsync:()=>X,filterAsync:()=>B,findAsync:()=>I,hashString:()=>q,intersection:()=>C,invokeIfFunction:()=>V,isArray:()=>f,isBool:()=>c,isDate:()=>h,isDefined:()=>o,isEmptyArray:()=>p,isEmptyObject:()=>y,isFiniteNumber:()=>M,isFunction:()=>g,isInteger:()=>P,isMap:()=>w,isNil:()=>a,isNilOrEmptyString:()=>i,isNull:()=>n,isNumber:()=>l,isObject:()=>u,isPromise:()=>m,isRegExp:()=>b,isSet:()=>S,isString:()=>s,isSymbol:()=>A,isUndefined:()=>v,isValidDate:()=>d,noop:()=>z,parse2DMatrix:()=>G,pipe:()=>D,reduceAsync:()=>Y,retry:()=>_,runParallel:()=>x,runSequential:()=>F,someAsync:()=>T,splitStringIntoWords:()=>L,toKebabCase:()=>R,unique:()=>O});const n=e=>null===e,a=e=>null==e,i=e=>""===e||a(e),o=e=>null!=e,s=e=>"string"==typeof e,l=e=>"number"==typeof e,c=e=>"boolean"==typeof e,u=e=>"object"==typeof e,y=e=>u(e)&&!n(e)&&0===Object.keys(e).length,f=e=>Array.isArray(e),p=e=>f(e)&&0===e.length,g=e=>"function"==typeof e,m=e=>g(e?.then),h=e=>e instanceof Date,d=e=>h(e)&&!isNaN(e.getTime()),b=e=>e instanceof RegExp,w=e=>e instanceof Map,S=e=>e instanceof Set,A=e=>"symbol"==typeof e,v=e=>void 0===e,M=e=>l(e)&&isFinite(e),P=e=>l(e)&&Number.isInteger(e),k=e=>e.filter(Boolean),O=e=>[...new Set(e)],j=(e,t)=>(r(t>0,"Chunk size must be greater than 0"),Array.from({length:Math.ceil(e.length/t)},(r,n)=>e.slice(n*t,(n+1)*t))),C=(...e)=>{if(0===e.length)return[];if(1===e.length)return[...e[0]];const[t,...r]=e;return O(t).filter(e=>r.every(t=>t.includes(e)))},N=(e,t)=>e.filter(e=>!t.includes(e)),D=(...e)=>t=>e.reduce((e,t)=>t(e),t),E=(...e)=>t=>e.reduceRight((e,t)=>t(e),t),F=async(e,t)=>{const r=[];for(let n=0;n<e.length;n++)r.push(await t(e[n],n,e));return r},x=async(e,t)=>Promise.all(e.map(t)),B=async(e,t)=>{const r=await x(e,async(e,r,n)=>!!await t(e,r,n)&&e);return k(r)},T=async(e,t)=>{for(let r=0;r<e.length;r++)if(await t(e[r],r,e))return!0;return!1},X=async(e,t)=>{for(let r=0;r<e.length;r++)if(!await t(e[r],r,e))return!1;return!0},Y=async(e,t,r)=>{let n=r;for(let r=0;r<e.length;r++)n=await t(n,e[r],r,e);return n},I=async(e,t)=>{for(let r=0;r<e.length;r++)if(await t(e[r],r,e))return e[r];return null},R=e=>e.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase(),$=e=>{const t=e.charAt(0),r=e.slice(1);return t.toLowerCase()+r.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`)},L=e=>e.split(" ").filter(Boolean),q=e=>{let t=5381;for(let r=0;r<e.length;r++)t=33*t^e.charCodeAt(r);return(t>>>0).toString(36)},z=()=>{},V=(e,...t)=>"function"==typeof e?e(...t):e,Z=e=>new Promise(t=>setTimeout(t,e)),_=(e,{maxAttempts:t=3,delayMs:r=300,backoff:n=!0,onRetry:a}={})=>async(...i)=>{let o;for(let s=1;s<=t;s++)try{return await e(...i)}catch(e){if(o=e,s<t){a?.(s,e);const t=n?r*2**(s-1):r;await Z(t)}}throw o},K=(e,t,r,n)=>{const a=r-e,i=n-t;return Math.hypot(a,i)},U=(e,t)=>Math.abs(e/t),W=(e,t)=>e*t/100,G=e=>{const t=window.getComputedStyle(e).getPropertyValue("transform").match(/^matrix\((.+)\)$/);if(!t)return{translateX:0,translateY:0,scaleX:1,scaleY:1,skewX:0,skewY:0};const[r,n,a,i,o,s]=t[1].split(", ").map(parseFloat);return{translateX:o,translateY:s,scaleX:r,scaleY:i,skewX:a,skewY:n}},H=e=>new Blob([e],{type:e.type}),J=(e,t)=>new File([e],t,{type:e.type});module.exports=t})();
2
2
  //# sourceMappingURL=index.cjs.map