@react-hive/honey-utils 1.5.0 โ 1.6.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 +167 -65
- package/dist/README.md +167 -65
- package/dist/array.d.ts +86 -0
- package/dist/dom.d.ts +44 -4
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.dev.cjs +197 -11
- 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/README.md
CHANGED
|
@@ -11,6 +11,8 @@ A lightweight TypeScript utility library providing a collection of helper functi
|
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
+
---
|
|
15
|
+
|
|
14
16
|
- ๐ **Type Guards** - Functions for runtime type checking
|
|
15
17
|
- ๐งต **String Utilities** - String manipulation and transformation
|
|
16
18
|
- ๐ข **Array Utilities** - Array filtering and manipulation
|
|
@@ -22,6 +24,8 @@ A lightweight TypeScript utility library providing a collection of helper functi
|
|
|
22
24
|
|
|
23
25
|
## Installation
|
|
24
26
|
|
|
27
|
+
---
|
|
28
|
+
|
|
25
29
|
```bash
|
|
26
30
|
# Using npm
|
|
27
31
|
npm install @react-hive/honey-utils
|
|
@@ -35,9 +39,11 @@ pnpm add @react-hive/honey-utils
|
|
|
35
39
|
|
|
36
40
|
## Usage
|
|
37
41
|
|
|
42
|
+
---
|
|
43
|
+
|
|
38
44
|
### Importing
|
|
39
45
|
|
|
40
|
-
```
|
|
46
|
+
```ts
|
|
41
47
|
// Import specific utilities
|
|
42
48
|
import { toKebabCase, isString, boolFilter } from '@react-hive/honey-utils';
|
|
43
49
|
|
|
@@ -47,7 +53,7 @@ import * as HoneyUtils from '@react-hive/honey-utils';
|
|
|
47
53
|
|
|
48
54
|
### String Utilities
|
|
49
55
|
|
|
50
|
-
```
|
|
56
|
+
```ts
|
|
51
57
|
import { toKebabCase, camelToDashCase, splitStringIntoWords, hashString } from '@react-hive/honey-utils';
|
|
52
58
|
|
|
53
59
|
// Convert string to kebab-case
|
|
@@ -65,34 +71,102 @@ const hash = hashString('background-color: red;'); // 'e4k1z0x'
|
|
|
65
71
|
|
|
66
72
|
### Array Utilities
|
|
67
73
|
|
|
68
|
-
```
|
|
69
|
-
import {
|
|
70
|
-
boolFilter,
|
|
71
|
-
unique,
|
|
72
|
-
chunk,
|
|
73
|
-
intersection,
|
|
74
|
-
difference
|
|
74
|
+
```ts
|
|
75
|
+
import {
|
|
76
|
+
boolFilter,
|
|
77
|
+
unique,
|
|
78
|
+
chunk,
|
|
79
|
+
intersection,
|
|
80
|
+
difference,
|
|
81
|
+
mapAsync,
|
|
82
|
+
forAsync,
|
|
83
|
+
reduceAsync,
|
|
84
|
+
filterAsync,
|
|
85
|
+
someAsync,
|
|
86
|
+
everyAsync,
|
|
87
|
+
findAsync,
|
|
75
88
|
} from '@react-hive/honey-utils';
|
|
76
89
|
|
|
77
90
|
// Filter out falsy values from an array
|
|
78
|
-
boolFilter([0, 1, false, 2, '', 3, null, undefined, true]);
|
|
91
|
+
boolFilter([0, 1, false, 2, '', 3, null, undefined, true]);
|
|
92
|
+
// โ [1, 2, 3, true]
|
|
79
93
|
|
|
80
94
|
// Remove duplicate values from an array
|
|
81
|
-
unique([1, 2, 2, 3, 1, 4]);
|
|
95
|
+
unique([1, 2, 2, 3, 1, 4]);
|
|
96
|
+
// โ [1, 2, 3, 4]
|
|
82
97
|
|
|
83
98
|
// Split an array into chunks of specified size
|
|
84
|
-
chunk([1, 2, 3, 4, 5], 2);
|
|
99
|
+
chunk([1, 2, 3, 4, 5], 2);
|
|
100
|
+
// โ [[1, 2], [3, 4], [5]]
|
|
85
101
|
|
|
86
102
|
// Find common elements between arrays
|
|
87
|
-
intersection([1, 2, 3], [2, 3, 4]);
|
|
103
|
+
intersection([1, 2, 3], [2, 3, 4]);
|
|
104
|
+
// โ [2, 3]
|
|
88
105
|
|
|
89
106
|
// Find elements in one array not in another
|
|
90
|
-
difference([1, 2, 3, 4], [2, 4]);
|
|
107
|
+
difference([1, 2, 3, 4], [2, 4]);
|
|
108
|
+
// โ [1, 3]
|
|
109
|
+
|
|
110
|
+
// Run async operations in parallel and collect results
|
|
111
|
+
await mapAsync([1, 2, 3], async (n) => {
|
|
112
|
+
await delay(100);
|
|
113
|
+
|
|
114
|
+
return n * 2;
|
|
115
|
+
});
|
|
116
|
+
// โ [2, 4, 6]
|
|
117
|
+
|
|
118
|
+
// Run async operations sequentially and collect results
|
|
119
|
+
await forAsync([1, 2, 3], async (n, i) => {
|
|
120
|
+
await delay(100);
|
|
121
|
+
|
|
122
|
+
return n * i;
|
|
123
|
+
});
|
|
124
|
+
// โ [0, 2, 6]
|
|
125
|
+
|
|
126
|
+
// Reduce array asynchronously
|
|
127
|
+
await reduceAsync([1, 2, 3], async (acc, n) => {
|
|
128
|
+
await delay(50);
|
|
129
|
+
|
|
130
|
+
return acc + n;
|
|
131
|
+
}, 0);
|
|
132
|
+
// โ 6
|
|
133
|
+
|
|
134
|
+
// Filter array asynchronously
|
|
135
|
+
await filterAsync([1, 2, 3, 4], async (n) => {
|
|
136
|
+
await delay(30);
|
|
137
|
+
|
|
138
|
+
return n % 2 === 0;
|
|
139
|
+
});
|
|
140
|
+
// โ [2, 4]
|
|
141
|
+
|
|
142
|
+
// Check if some items match condition asynchronously
|
|
143
|
+
await someAsync([1, 3, 5], async (n) => {
|
|
144
|
+
await delay(10);
|
|
145
|
+
|
|
146
|
+
return n % 2 === 0;
|
|
147
|
+
});
|
|
148
|
+
// โ false
|
|
149
|
+
|
|
150
|
+
// Check if all items match condition asynchronously
|
|
151
|
+
await everyAsync([2, 4, 6], async (n) => {
|
|
152
|
+
await delay(10);
|
|
153
|
+
|
|
154
|
+
return n % 2 === 0;
|
|
155
|
+
});
|
|
156
|
+
// โ true
|
|
157
|
+
|
|
158
|
+
// Find first matching item asynchronously
|
|
159
|
+
await findAsync([1, 3, 4, 5], async (n) => {
|
|
160
|
+
await delay(20);
|
|
161
|
+
|
|
162
|
+
return n % 2 === 0;
|
|
163
|
+
});
|
|
164
|
+
// โ 4
|
|
91
165
|
```
|
|
92
166
|
|
|
93
167
|
### Function Utilities
|
|
94
168
|
|
|
95
|
-
```
|
|
169
|
+
```ts
|
|
96
170
|
import { noop, invokeIfFunction, delay, retry } from '@react-hive/honey-utils';
|
|
97
171
|
|
|
98
172
|
// No-operation function
|
|
@@ -134,7 +208,7 @@ fetchWithRetry()
|
|
|
134
208
|
|
|
135
209
|
### Type Guards
|
|
136
210
|
|
|
137
|
-
```
|
|
211
|
+
```ts
|
|
138
212
|
import {
|
|
139
213
|
isString,
|
|
140
214
|
isNumber,
|
|
@@ -221,7 +295,7 @@ isSet(new Set()); // true
|
|
|
221
295
|
|
|
222
296
|
### Math Utilities
|
|
223
297
|
|
|
224
|
-
```
|
|
298
|
+
```ts
|
|
225
299
|
import {
|
|
226
300
|
calculateEuclideanDistance,
|
|
227
301
|
calculateMovingSpeed,
|
|
@@ -240,21 +314,37 @@ calculatePercentage(200, 25); // 50
|
|
|
240
314
|
|
|
241
315
|
### DOM Utilities
|
|
242
316
|
|
|
243
|
-
```
|
|
244
|
-
import {
|
|
317
|
+
```ts
|
|
318
|
+
import { parse2DMatrix, cloneBlob, convertBlobToFile } from '@react-hive/honey-utils';
|
|
245
319
|
|
|
246
|
-
//
|
|
320
|
+
// Extract transformation values from an HTML element's 2D matrix
|
|
247
321
|
const element = document.getElementById('my-element');
|
|
248
322
|
if (element) {
|
|
249
|
-
const { translateX, translateY } =
|
|
323
|
+
const { translateX, translateY, scaleX, scaleY, skewX, skewY } = parse2DMatrix(element);
|
|
250
324
|
|
|
251
325
|
console.log(`Element is translated by ${translateX}px horizontally and ${translateY}px vertically`);
|
|
326
|
+
console.log(`Element is scaled by ${scaleX} horizontally and ${scaleY} vertically`);
|
|
327
|
+
console.log(`Element is skewed by ${skewX} horizontally and ${skewY} vertically`);
|
|
252
328
|
}
|
|
329
|
+
|
|
330
|
+
// Clone a Blob object
|
|
331
|
+
const originalBlob = new Blob(['Hello World'], { type: 'text/plain' });
|
|
332
|
+
const clonedBlob = cloneBlob(originalBlob);
|
|
333
|
+
|
|
334
|
+
console.log(clonedBlob.type); // 'text/plain'
|
|
335
|
+
|
|
336
|
+
// Convert a Blob to a File
|
|
337
|
+
const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
338
|
+
const file = convertBlobToFile(blob, 'hello.txt');
|
|
339
|
+
|
|
340
|
+
console.log(file instanceof File); // true
|
|
341
|
+
console.log(file.name); // 'hello.txt'
|
|
342
|
+
console.log(file.type); // 'text/plain'
|
|
253
343
|
```
|
|
254
344
|
|
|
255
345
|
### Assert Function
|
|
256
346
|
|
|
257
|
-
```
|
|
347
|
+
```ts
|
|
258
348
|
import { assert } from '@react-hive/honey-utils';
|
|
259
349
|
|
|
260
350
|
// Assert a condition
|
|
@@ -266,66 +356,78 @@ function divide(a: number, b: number): number {
|
|
|
266
356
|
|
|
267
357
|
## API Documentation
|
|
268
358
|
|
|
359
|
+
---
|
|
360
|
+
|
|
269
361
|
### String Utilities
|
|
270
362
|
|
|
271
|
-
-
|
|
272
|
-
-
|
|
273
|
-
-
|
|
274
|
-
-
|
|
363
|
+
- `toKebabCase(input: string): string` - Converts a string to kebab-case.
|
|
364
|
+
- `camelToDashCase(input: string): string` - Converts camelCase to dash-case.
|
|
365
|
+
- `splitStringIntoWords(input: string): string[]` - Splits a string into an array of words.
|
|
366
|
+
- `hashString(input: string): string` - Generates a short hash from a string.
|
|
275
367
|
|
|
276
368
|
### Array Utilities
|
|
277
369
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
-
|
|
281
|
-
-
|
|
282
|
-
-
|
|
370
|
+
#### Synchronous Utilities
|
|
371
|
+
|
|
372
|
+
- `boolFilter<T>(array: (T | false | null | undefined)[]): T[]` - Filters out falsy values (`false`, `null`, `undefined`) from an array while keeping valid items.
|
|
373
|
+
- `unique<T>(array: T[]): T[]` - Returns a new array with all duplicate elements removed. Keeps only the first occurrence of each value.
|
|
374
|
+
- `chunk<T>(array: T[], size: number): T[][]` - Splits an array into smaller arrays ("chunks") of the specified size.
|
|
375
|
+
- `intersection<T>(...arrays: T[][]): T[]` - Returns an array of elements that exist in all provided arrays.
|
|
376
|
+
- `difference<T>(array: T[], exclude: T[]): T[]` - Returns a new array that contains items from `array` that are not present in `exclude`.
|
|
377
|
+
|
|
378
|
+
#### Asynchronous Utilities
|
|
379
|
+
|
|
380
|
+
- `forAsync<Item, Result>(array: Item[], callbackFn: (item, index, array) => Promise<Result>): Promise<Result[]>` - Runs asynchronous operations on each array item *sequentially* and returns the results in the original order.
|
|
381
|
+
- `mapAsync<Item, Return>(array: Item[], callbackFn: (item, index, array) => Promise<Return>): Promise<Return[]>` - Executes an asynchronous function for each array item *in parallel* and returns a promise of all results.
|
|
382
|
+
- `reduceAsync<Item, Accumulator>(array: Item[], callbackFn, initialValue): Promise<Accumulator>` - Asynchronously reduces an array to a single accumulated value. Each step waits for the previous promise to resolve.
|
|
383
|
+
- `filterAsync<Item>(array: Item[], callbackFn): Promise<Item[]>` - Runs an asynchronous filter operation. Only includes items where `callbackFn(item)` resolves to `true`.
|
|
384
|
+
- `someAsync<Item>(array: Item[], callbackFn): Promise<boolean>` - Returns `true` if **any** item in the array passes the async predicate.
|
|
385
|
+
- `everyAsync<Item>(array: Item[], callbackFn): Promise<boolean>` - Returns `true` if **all** items in the array pass the async predicate.
|
|
386
|
+
- `findAsync<Item>(array: Item[], callbackFn): Promise<Item | null>` - Returns the first array item that passes the async predicate, or `null` if no match is found.
|
|
283
387
|
|
|
284
388
|
### Function Utilities
|
|
285
389
|
|
|
286
|
-
-
|
|
287
|
-
-
|
|
288
|
-
-
|
|
289
|
-
-
|
|
390
|
+
- `noop(): void` - A no-operation function.
|
|
391
|
+
- `invokeIfFunction<Args extends any[], Result>(input: ((...args: Args) => Result) | Result, ...args: Args): Result` - Invokes the input if it's a function, otherwise returns it as-is.
|
|
392
|
+
- `delay(delayMs: number): Promise<void>` - Creates a promise that resolves after the specified delay in milliseconds.
|
|
393
|
+
- `retry<Task, TaskResult>(task: Task, options?: RetryOptions): Function` - Wraps an asynchronous function with retry logic, with configurable max attempts, delay between retries, exponential backoff, and retry callbacks.
|
|
290
394
|
|
|
291
395
|
### Type Guards
|
|
292
396
|
|
|
293
|
-
-
|
|
294
|
-
-
|
|
295
|
-
-
|
|
296
|
-
-
|
|
297
|
-
-
|
|
298
|
-
-
|
|
299
|
-
-
|
|
300
|
-
-
|
|
301
|
-
-
|
|
302
|
-
-
|
|
303
|
-
-
|
|
304
|
-
-
|
|
305
|
-
-
|
|
306
|
-
-
|
|
307
|
-
-
|
|
308
|
-
-
|
|
309
|
-
-
|
|
310
|
-
-
|
|
311
|
-
-
|
|
312
|
-
-
|
|
313
|
-
-
|
|
314
|
-
-
|
|
397
|
+
- `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, undefined, or an empty string.
|
|
406
|
+
- `isArray(value: unknown): value is unknown[]` - Checks if a value is an array.
|
|
407
|
+
- `isEmptyArray(value: unknown): value is []` - Checks if a value is an empty array.
|
|
408
|
+
- `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.
|
|
411
|
+
- `isFiniteNumber(value: unknown): value is number` - Checks if a value is a finite number.
|
|
412
|
+
- `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 (not Invalid Date).
|
|
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.
|
|
315
419
|
|
|
316
420
|
### Math Utilities
|
|
317
421
|
|
|
318
|
-
-
|
|
319
|
-
-
|
|
320
|
-
-
|
|
422
|
+
- `calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number` - Calculates the Euclidean distance between two points.
|
|
423
|
+
- `calculateMovingSpeed(delta: number, elapsedTime: number): number` - Calculates moving speed.
|
|
424
|
+
- `calculatePercentage(value: number, percentage: number): number` - Calculates the specified percentage of a value.
|
|
321
425
|
|
|
322
426
|
### DOM Utilities
|
|
323
427
|
|
|
324
|
-
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
- **assert(condition: any, message: string): asserts condition** - Asserts that a condition is truthy, throwing an error with the provided message if it's not
|
|
428
|
+
- `parse2DMatrix(element: HTMLElement): { translateX: number, translateY: number, scaleX: number, scaleY: number, skewX: number, skewY: number }` - Extracts transformation values (translate, scale, skew) from the 2D transformation matrix of a given HTML element.
|
|
429
|
+
- `cloneBlob(blob: Blob): Blob` - Creates a clone of a Blob object with the same content and type as the original.
|
|
430
|
+
- `convertBlobToFile(blob: Blob, fileName: string): File` - Converts a Blob object into a File object with the specified name.
|
|
329
431
|
|
|
330
432
|
## Contributing
|
|
331
433
|
|