@react-hive/honey-utils 1.2.0 โ 1.4.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 +313 -0
- package/dist/README.md +313 -0
- package/dist/array.d.ts +71 -0
- package/dist/guards.d.ts +149 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.dev.cjs +324 -10
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/string.d.ts +36 -0
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# @react-hive/honey-utils
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@react-hive/honey-utils)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
A lightweight TypeScript utility library providing a collection of helper functions for common programming tasks.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- ๐ **Type Guards** - Functions for runtime type checking
|
|
11
|
+
- ๐งต **String Utilities** - String manipulation and transformation
|
|
12
|
+
- ๐ข **Array Utilities** - Array filtering and manipulation
|
|
13
|
+
- ๐งฎ **Math Utilities** - Common mathematical calculations
|
|
14
|
+
- ๐ฏ **Function Utilities** - Function handling helpers
|
|
15
|
+
- ๐ฅ๏ธ **DOM Utilities** - Browser DOM manipulation helpers
|
|
16
|
+
- ๐ฆ **Zero Dependencies** - Lightweight and dependency-free
|
|
17
|
+
- ๐ **TypeScript Support** - Full TypeScript type definitions
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Using npm
|
|
23
|
+
npm install @react-hive/honey-utils
|
|
24
|
+
|
|
25
|
+
# Using yarn
|
|
26
|
+
yarn add @react-hive/honey-utils
|
|
27
|
+
|
|
28
|
+
# Using pnpm
|
|
29
|
+
pnpm add @react-hive/honey-utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Importing
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// Import specific utilities
|
|
38
|
+
import { toKebabCase, isString, boolFilter } from '@react-hive/honey-utils';
|
|
39
|
+
|
|
40
|
+
// Or import everything
|
|
41
|
+
import * as HoneyUtils from '@react-hive/honey-utils';
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### String Utilities
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { toKebabCase, camelToDashCase, splitStringIntoWords, hashString } from '@react-hive/honey-utils';
|
|
48
|
+
|
|
49
|
+
// Convert string to kebab-case
|
|
50
|
+
toKebabCase('helloWorld'); // 'hello-world'
|
|
51
|
+
|
|
52
|
+
// Convert camelCase to dash-case
|
|
53
|
+
camelToDashCase('helloWorld'); // 'hello-world'
|
|
54
|
+
|
|
55
|
+
// Split string into words
|
|
56
|
+
splitStringIntoWords('hello world'); // ['hello', 'world']
|
|
57
|
+
|
|
58
|
+
// Generate a hash from a string
|
|
59
|
+
const hash = hashString('background-color: red;'); // 'e4k1z0x'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Array Utilities
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import {
|
|
66
|
+
boolFilter,
|
|
67
|
+
unique,
|
|
68
|
+
chunk,
|
|
69
|
+
intersection,
|
|
70
|
+
difference
|
|
71
|
+
} from '@react-hive/honey-utils';
|
|
72
|
+
|
|
73
|
+
// Filter out falsy values from an array
|
|
74
|
+
boolFilter([0, 1, false, 2, '', 3, null, undefined, true]); // [1, 2, 3, true]
|
|
75
|
+
|
|
76
|
+
// Remove duplicate values from an array
|
|
77
|
+
unique([1, 2, 2, 3, 1, 4]); // [1, 2, 3, 4]
|
|
78
|
+
|
|
79
|
+
// Split an array into chunks of specified size
|
|
80
|
+
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
81
|
+
|
|
82
|
+
// Find common elements between arrays
|
|
83
|
+
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
|
|
84
|
+
|
|
85
|
+
// Find elements in one array not in another
|
|
86
|
+
difference([1, 2, 3, 4], [2, 4]); // [1, 3]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Function Utilities
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { noop, invokeIfFunction } from '@react-hive/honey-utils';
|
|
93
|
+
|
|
94
|
+
// No-operation function
|
|
95
|
+
noop(); // does nothing
|
|
96
|
+
|
|
97
|
+
// Invoke if function, otherwise return value
|
|
98
|
+
const fn = (x: number) => x * 2;
|
|
99
|
+
|
|
100
|
+
invokeIfFunction(fn, 5); // 10
|
|
101
|
+
invokeIfFunction('not a function', 5); // 'not a function'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Type Guards
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import {
|
|
108
|
+
isString,
|
|
109
|
+
isNumber,
|
|
110
|
+
isBool,
|
|
111
|
+
isObject,
|
|
112
|
+
isFunction,
|
|
113
|
+
isPromise,
|
|
114
|
+
isNil,
|
|
115
|
+
isNilOrEmptyString,
|
|
116
|
+
isArray,
|
|
117
|
+
isEmptyArray,
|
|
118
|
+
isEmptyObject,
|
|
119
|
+
isNull,
|
|
120
|
+
isUndefined,
|
|
121
|
+
isDate,
|
|
122
|
+
isValidDate,
|
|
123
|
+
isRegExp,
|
|
124
|
+
isMap,
|
|
125
|
+
isSet
|
|
126
|
+
} from '@react-hive/honey-utils';
|
|
127
|
+
|
|
128
|
+
// Check if value is a string
|
|
129
|
+
isString('hello'); // true
|
|
130
|
+
isString(123); // false
|
|
131
|
+
|
|
132
|
+
// Check if value is a number
|
|
133
|
+
isNumber(123); // true
|
|
134
|
+
isNumber('123'); // false
|
|
135
|
+
|
|
136
|
+
// Check if value is a boolean
|
|
137
|
+
isBool(true); // true
|
|
138
|
+
isBool('true'); // false
|
|
139
|
+
|
|
140
|
+
// Check if value is an object
|
|
141
|
+
isObject({}); // true
|
|
142
|
+
isObject('object'); // false
|
|
143
|
+
|
|
144
|
+
// Check if value is a function
|
|
145
|
+
isFunction(() => {}); // true
|
|
146
|
+
isFunction({}); // false
|
|
147
|
+
|
|
148
|
+
// Check if value is a Promise
|
|
149
|
+
isPromise(Promise.resolve()); // true
|
|
150
|
+
isPromise({}); // false
|
|
151
|
+
|
|
152
|
+
// Check if value is null or undefined
|
|
153
|
+
isNil(null); // true
|
|
154
|
+
isNil(undefined); // true
|
|
155
|
+
isNil(''); // false
|
|
156
|
+
|
|
157
|
+
// Check if value is null, undefined, or empty string
|
|
158
|
+
isNilOrEmptyString(''); // true
|
|
159
|
+
isNilOrEmptyString(null); // true
|
|
160
|
+
isNilOrEmptyString('hello'); // false
|
|
161
|
+
|
|
162
|
+
// Check if value is an array
|
|
163
|
+
isArray([1, 2, 3]); // true
|
|
164
|
+
isArray({}); // false
|
|
165
|
+
|
|
166
|
+
// Check if value is an empty array
|
|
167
|
+
isEmptyArray([]); // true
|
|
168
|
+
isEmptyArray([1, 2, 3]); // false
|
|
169
|
+
|
|
170
|
+
// Check if value is an empty object
|
|
171
|
+
isEmptyObject({}); // true
|
|
172
|
+
isEmptyObject({ key: 'value' }); // false
|
|
173
|
+
|
|
174
|
+
// Check if value is a Date object
|
|
175
|
+
isDate(new Date()); // true
|
|
176
|
+
isDate('2023-01-01'); // false
|
|
177
|
+
|
|
178
|
+
// Check if value is a valid Date object
|
|
179
|
+
isValidDate(new Date()); // true
|
|
180
|
+
isValidDate(new Date('invalid')); // false
|
|
181
|
+
|
|
182
|
+
// Check if value is a RegExp
|
|
183
|
+
isRegExp(/test/); // true
|
|
184
|
+
isRegExp('test'); // false
|
|
185
|
+
|
|
186
|
+
// Check if value is a Map or Set
|
|
187
|
+
isMap(new Map()); // true
|
|
188
|
+
isSet(new Set()); // true
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Math Utilities
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import {
|
|
195
|
+
calculateEuclideanDistance,
|
|
196
|
+
calculateMovingSpeed,
|
|
197
|
+
calculatePercentage
|
|
198
|
+
} from '@react-hive/honey-utils';
|
|
199
|
+
|
|
200
|
+
// Calculate Euclidean distance between two points
|
|
201
|
+
calculateEuclideanDistance(0, 0, 3, 4); // 5
|
|
202
|
+
|
|
203
|
+
// Calculate moving speed
|
|
204
|
+
calculateMovingSpeed(100, 5); // 20
|
|
205
|
+
|
|
206
|
+
// Calculate percentage of a value
|
|
207
|
+
calculatePercentage(200, 25); // 50
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### DOM Utilities
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
import { getTransformationValues } from '@react-hive/honey-utils';
|
|
214
|
+
|
|
215
|
+
// Get transformation values from an HTML element
|
|
216
|
+
const element = document.getElementById('my-element');
|
|
217
|
+
if (element) {
|
|
218
|
+
const { translateX, translateY } = getTransformationValues(element);
|
|
219
|
+
|
|
220
|
+
console.log(`Element is translated by ${translateX}px horizontally and ${translateY}px vertically`);
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Assert Function
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { assert } from '@react-hive/honey-utils';
|
|
228
|
+
|
|
229
|
+
// Assert a condition
|
|
230
|
+
function divide(a: number, b: number): number {
|
|
231
|
+
assert(b !== 0, 'Cannot divide by zero');
|
|
232
|
+
return a / b;
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## API Documentation
|
|
237
|
+
|
|
238
|
+
### String Utilities
|
|
239
|
+
|
|
240
|
+
- **toKebabCase(input: string): string** - Converts a string to kebab-case
|
|
241
|
+
- **camelToDashCase(input: string): string** - Converts camelCase to dash-case
|
|
242
|
+
- **splitStringIntoWords(input: string): string[]** - Splits a string into an array of words
|
|
243
|
+
- **hashString(input: string): string** - Generates a short hash from a string
|
|
244
|
+
|
|
245
|
+
### Array Utilities
|
|
246
|
+
|
|
247
|
+
- **boolFilter<T>(array: (T | false | null | undefined)[]): T[]** - Filters out falsy values from an array
|
|
248
|
+
- **unique<T>(array: T[]): T[]** - Returns a new array with duplicate values removed
|
|
249
|
+
- **chunk<T>(array: T[], size: number): T[][]** - Splits an array into chunks of the specified size
|
|
250
|
+
- **intersection<T>(...arrays: T[][]): T[]** - Returns an array containing elements that exist in all provided arrays
|
|
251
|
+
- **difference<T>(array: T[], exclude: T[]): T[]** - Returns elements from the first array that don't exist in the second array
|
|
252
|
+
|
|
253
|
+
### Function Utilities
|
|
254
|
+
|
|
255
|
+
- **noop(): void** - A no-operation function
|
|
256
|
+
- **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
|
|
257
|
+
|
|
258
|
+
### Type Guards
|
|
259
|
+
|
|
260
|
+
- **isString(value: unknown): value is string** - Checks if a value is a string
|
|
261
|
+
- **isNumber(value: unknown): value is number** - Checks if a value is a number
|
|
262
|
+
- **isBool(value: unknown): value is boolean** - Checks if a value is a boolean
|
|
263
|
+
- **isObject(value: unknown): value is object** - Checks if a value is an object
|
|
264
|
+
- **isFunction(value: unknown): value is Function** - Checks if a value is a function
|
|
265
|
+
- **isPromise<T = unknown>(value: unknown): value is Promise<T>** - Checks if a value is a Promise
|
|
266
|
+
- **isNil(value: unknown): value is null | undefined** - Checks if a value is null or undefined
|
|
267
|
+
- **isNilOrEmptyString(value: unknown): value is null | undefined** - Checks if a value is null, undefined, or an empty string
|
|
268
|
+
- **isArray(value: unknown): value is unknown[]** - Checks if a value is an array
|
|
269
|
+
- **isEmptyArray(value: unknown): value is []** - Checks if a value is an empty array
|
|
270
|
+
- **isEmptyObject(value: unknown): value is Record<string, never>** - Checks if a value is an empty object
|
|
271
|
+
- **isNull(value: unknown): value is null** - Checks if a value is null
|
|
272
|
+
- **isUndefined(value: unknown): value is undefined** - Checks if a value is undefined
|
|
273
|
+
- **isFiniteNumber(value: unknown): value is number** - Checks if a value is a finite number
|
|
274
|
+
- **isInteger(value: unknown): value is number** - Checks if a value is an integer
|
|
275
|
+
- **isNaN(value: unknown): boolean** - Checks if a value is NaN
|
|
276
|
+
- **isDate(value: unknown): value is Date** - Checks if a value is a Date object
|
|
277
|
+
- **isValidDate(value: unknown): value is Date** - Checks if a value is a valid Date object (not Invalid Date)
|
|
278
|
+
- **isRegExp(value: unknown): value is RegExp** - Checks if a value is a RegExp object
|
|
279
|
+
- **isMap(value: unknown): value is Map<unknown, unknown>** - Checks if a value is a Map
|
|
280
|
+
- **isSet(value: unknown): value is Set<unknown>** - Checks if a value is a Set
|
|
281
|
+
- **isSymbol(value: unknown): value is symbol** - Checks if a value is a Symbol
|
|
282
|
+
|
|
283
|
+
### Math Utilities
|
|
284
|
+
|
|
285
|
+
- **calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number** - Calculates the Euclidean distance between two points
|
|
286
|
+
- **calculateMovingSpeed(delta: number, elapsedTime: number): number** - Calculates moving speed
|
|
287
|
+
- **calculatePercentage(value: number, percentage: number): number** - Calculates the specified percentage of a value
|
|
288
|
+
|
|
289
|
+
### DOM Utilities
|
|
290
|
+
|
|
291
|
+
- **getTransformationValues(element: HTMLElement): { translateX: number, translateY: number }** - Gets transformation values from an HTML element
|
|
292
|
+
|
|
293
|
+
### Other Utilities
|
|
294
|
+
|
|
295
|
+
- **assert(condition: any, message: string): asserts condition** - Asserts that a condition is truthy, throwing an error with the provided message if it's not
|
|
296
|
+
|
|
297
|
+
## Contributing
|
|
298
|
+
|
|
299
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
300
|
+
|
|
301
|
+
1. Fork the repository
|
|
302
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
303
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
304
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
305
|
+
5. Open a Pull Request
|
|
306
|
+
|
|
307
|
+
## License
|
|
308
|
+
|
|
309
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
310
|
+
|
|
311
|
+
## Author
|
|
312
|
+
|
|
313
|
+
Mykhailo Aliinyk <m.aliynik@gmail.com>
|
package/dist/README.md
CHANGED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# @react-hive/honey-utils
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@react-hive/honey-utils)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
A lightweight TypeScript utility library providing a collection of helper functions for common programming tasks.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- ๐ **Type Guards** - Functions for runtime type checking
|
|
11
|
+
- ๐งต **String Utilities** - String manipulation and transformation
|
|
12
|
+
- ๐ข **Array Utilities** - Array filtering and manipulation
|
|
13
|
+
- ๐งฎ **Math Utilities** - Common mathematical calculations
|
|
14
|
+
- ๐ฏ **Function Utilities** - Function handling helpers
|
|
15
|
+
- ๐ฅ๏ธ **DOM Utilities** - Browser DOM manipulation helpers
|
|
16
|
+
- ๐ฆ **Zero Dependencies** - Lightweight and dependency-free
|
|
17
|
+
- ๐ **TypeScript Support** - Full TypeScript type definitions
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Using npm
|
|
23
|
+
npm install @react-hive/honey-utils
|
|
24
|
+
|
|
25
|
+
# Using yarn
|
|
26
|
+
yarn add @react-hive/honey-utils
|
|
27
|
+
|
|
28
|
+
# Using pnpm
|
|
29
|
+
pnpm add @react-hive/honey-utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Importing
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// Import specific utilities
|
|
38
|
+
import { toKebabCase, isString, boolFilter } from '@react-hive/honey-utils';
|
|
39
|
+
|
|
40
|
+
// Or import everything
|
|
41
|
+
import * as HoneyUtils from '@react-hive/honey-utils';
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### String Utilities
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { toKebabCase, camelToDashCase, splitStringIntoWords, hashString } from '@react-hive/honey-utils';
|
|
48
|
+
|
|
49
|
+
// Convert string to kebab-case
|
|
50
|
+
toKebabCase('helloWorld'); // 'hello-world'
|
|
51
|
+
|
|
52
|
+
// Convert camelCase to dash-case
|
|
53
|
+
camelToDashCase('helloWorld'); // 'hello-world'
|
|
54
|
+
|
|
55
|
+
// Split string into words
|
|
56
|
+
splitStringIntoWords('hello world'); // ['hello', 'world']
|
|
57
|
+
|
|
58
|
+
// Generate a hash from a string
|
|
59
|
+
const hash = hashString('background-color: red;'); // 'e4k1z0x'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Array Utilities
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import {
|
|
66
|
+
boolFilter,
|
|
67
|
+
unique,
|
|
68
|
+
chunk,
|
|
69
|
+
intersection,
|
|
70
|
+
difference
|
|
71
|
+
} from '@react-hive/honey-utils';
|
|
72
|
+
|
|
73
|
+
// Filter out falsy values from an array
|
|
74
|
+
boolFilter([0, 1, false, 2, '', 3, null, undefined, true]); // [1, 2, 3, true]
|
|
75
|
+
|
|
76
|
+
// Remove duplicate values from an array
|
|
77
|
+
unique([1, 2, 2, 3, 1, 4]); // [1, 2, 3, 4]
|
|
78
|
+
|
|
79
|
+
// Split an array into chunks of specified size
|
|
80
|
+
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
81
|
+
|
|
82
|
+
// Find common elements between arrays
|
|
83
|
+
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
|
|
84
|
+
|
|
85
|
+
// Find elements in one array not in another
|
|
86
|
+
difference([1, 2, 3, 4], [2, 4]); // [1, 3]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Function Utilities
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { noop, invokeIfFunction } from '@react-hive/honey-utils';
|
|
93
|
+
|
|
94
|
+
// No-operation function
|
|
95
|
+
noop(); // does nothing
|
|
96
|
+
|
|
97
|
+
// Invoke if function, otherwise return value
|
|
98
|
+
const fn = (x: number) => x * 2;
|
|
99
|
+
|
|
100
|
+
invokeIfFunction(fn, 5); // 10
|
|
101
|
+
invokeIfFunction('not a function', 5); // 'not a function'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Type Guards
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import {
|
|
108
|
+
isString,
|
|
109
|
+
isNumber,
|
|
110
|
+
isBool,
|
|
111
|
+
isObject,
|
|
112
|
+
isFunction,
|
|
113
|
+
isPromise,
|
|
114
|
+
isNil,
|
|
115
|
+
isNilOrEmptyString,
|
|
116
|
+
isArray,
|
|
117
|
+
isEmptyArray,
|
|
118
|
+
isEmptyObject,
|
|
119
|
+
isNull,
|
|
120
|
+
isUndefined,
|
|
121
|
+
isDate,
|
|
122
|
+
isValidDate,
|
|
123
|
+
isRegExp,
|
|
124
|
+
isMap,
|
|
125
|
+
isSet
|
|
126
|
+
} from '@react-hive/honey-utils';
|
|
127
|
+
|
|
128
|
+
// Check if value is a string
|
|
129
|
+
isString('hello'); // true
|
|
130
|
+
isString(123); // false
|
|
131
|
+
|
|
132
|
+
// Check if value is a number
|
|
133
|
+
isNumber(123); // true
|
|
134
|
+
isNumber('123'); // false
|
|
135
|
+
|
|
136
|
+
// Check if value is a boolean
|
|
137
|
+
isBool(true); // true
|
|
138
|
+
isBool('true'); // false
|
|
139
|
+
|
|
140
|
+
// Check if value is an object
|
|
141
|
+
isObject({}); // true
|
|
142
|
+
isObject('object'); // false
|
|
143
|
+
|
|
144
|
+
// Check if value is a function
|
|
145
|
+
isFunction(() => {}); // true
|
|
146
|
+
isFunction({}); // false
|
|
147
|
+
|
|
148
|
+
// Check if value is a Promise
|
|
149
|
+
isPromise(Promise.resolve()); // true
|
|
150
|
+
isPromise({}); // false
|
|
151
|
+
|
|
152
|
+
// Check if value is null or undefined
|
|
153
|
+
isNil(null); // true
|
|
154
|
+
isNil(undefined); // true
|
|
155
|
+
isNil(''); // false
|
|
156
|
+
|
|
157
|
+
// Check if value is null, undefined, or empty string
|
|
158
|
+
isNilOrEmptyString(''); // true
|
|
159
|
+
isNilOrEmptyString(null); // true
|
|
160
|
+
isNilOrEmptyString('hello'); // false
|
|
161
|
+
|
|
162
|
+
// Check if value is an array
|
|
163
|
+
isArray([1, 2, 3]); // true
|
|
164
|
+
isArray({}); // false
|
|
165
|
+
|
|
166
|
+
// Check if value is an empty array
|
|
167
|
+
isEmptyArray([]); // true
|
|
168
|
+
isEmptyArray([1, 2, 3]); // false
|
|
169
|
+
|
|
170
|
+
// Check if value is an empty object
|
|
171
|
+
isEmptyObject({}); // true
|
|
172
|
+
isEmptyObject({ key: 'value' }); // false
|
|
173
|
+
|
|
174
|
+
// Check if value is a Date object
|
|
175
|
+
isDate(new Date()); // true
|
|
176
|
+
isDate('2023-01-01'); // false
|
|
177
|
+
|
|
178
|
+
// Check if value is a valid Date object
|
|
179
|
+
isValidDate(new Date()); // true
|
|
180
|
+
isValidDate(new Date('invalid')); // false
|
|
181
|
+
|
|
182
|
+
// Check if value is a RegExp
|
|
183
|
+
isRegExp(/test/); // true
|
|
184
|
+
isRegExp('test'); // false
|
|
185
|
+
|
|
186
|
+
// Check if value is a Map or Set
|
|
187
|
+
isMap(new Map()); // true
|
|
188
|
+
isSet(new Set()); // true
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Math Utilities
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import {
|
|
195
|
+
calculateEuclideanDistance,
|
|
196
|
+
calculateMovingSpeed,
|
|
197
|
+
calculatePercentage
|
|
198
|
+
} from '@react-hive/honey-utils';
|
|
199
|
+
|
|
200
|
+
// Calculate Euclidean distance between two points
|
|
201
|
+
calculateEuclideanDistance(0, 0, 3, 4); // 5
|
|
202
|
+
|
|
203
|
+
// Calculate moving speed
|
|
204
|
+
calculateMovingSpeed(100, 5); // 20
|
|
205
|
+
|
|
206
|
+
// Calculate percentage of a value
|
|
207
|
+
calculatePercentage(200, 25); // 50
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### DOM Utilities
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
import { getTransformationValues } from '@react-hive/honey-utils';
|
|
214
|
+
|
|
215
|
+
// Get transformation values from an HTML element
|
|
216
|
+
const element = document.getElementById('my-element');
|
|
217
|
+
if (element) {
|
|
218
|
+
const { translateX, translateY } = getTransformationValues(element);
|
|
219
|
+
|
|
220
|
+
console.log(`Element is translated by ${translateX}px horizontally and ${translateY}px vertically`);
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Assert Function
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { assert } from '@react-hive/honey-utils';
|
|
228
|
+
|
|
229
|
+
// Assert a condition
|
|
230
|
+
function divide(a: number, b: number): number {
|
|
231
|
+
assert(b !== 0, 'Cannot divide by zero');
|
|
232
|
+
return a / b;
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## API Documentation
|
|
237
|
+
|
|
238
|
+
### String Utilities
|
|
239
|
+
|
|
240
|
+
- **toKebabCase(input: string): string** - Converts a string to kebab-case
|
|
241
|
+
- **camelToDashCase(input: string): string** - Converts camelCase to dash-case
|
|
242
|
+
- **splitStringIntoWords(input: string): string[]** - Splits a string into an array of words
|
|
243
|
+
- **hashString(input: string): string** - Generates a short hash from a string
|
|
244
|
+
|
|
245
|
+
### Array Utilities
|
|
246
|
+
|
|
247
|
+
- **boolFilter<T>(array: (T | false | null | undefined)[]): T[]** - Filters out falsy values from an array
|
|
248
|
+
- **unique<T>(array: T[]): T[]** - Returns a new array with duplicate values removed
|
|
249
|
+
- **chunk<T>(array: T[], size: number): T[][]** - Splits an array into chunks of the specified size
|
|
250
|
+
- **intersection<T>(...arrays: T[][]): T[]** - Returns an array containing elements that exist in all provided arrays
|
|
251
|
+
- **difference<T>(array: T[], exclude: T[]): T[]** - Returns elements from the first array that don't exist in the second array
|
|
252
|
+
|
|
253
|
+
### Function Utilities
|
|
254
|
+
|
|
255
|
+
- **noop(): void** - A no-operation function
|
|
256
|
+
- **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
|
|
257
|
+
|
|
258
|
+
### Type Guards
|
|
259
|
+
|
|
260
|
+
- **isString(value: unknown): value is string** - Checks if a value is a string
|
|
261
|
+
- **isNumber(value: unknown): value is number** - Checks if a value is a number
|
|
262
|
+
- **isBool(value: unknown): value is boolean** - Checks if a value is a boolean
|
|
263
|
+
- **isObject(value: unknown): value is object** - Checks if a value is an object
|
|
264
|
+
- **isFunction(value: unknown): value is Function** - Checks if a value is a function
|
|
265
|
+
- **isPromise<T = unknown>(value: unknown): value is Promise<T>** - Checks if a value is a Promise
|
|
266
|
+
- **isNil(value: unknown): value is null | undefined** - Checks if a value is null or undefined
|
|
267
|
+
- **isNilOrEmptyString(value: unknown): value is null | undefined** - Checks if a value is null, undefined, or an empty string
|
|
268
|
+
- **isArray(value: unknown): value is unknown[]** - Checks if a value is an array
|
|
269
|
+
- **isEmptyArray(value: unknown): value is []** - Checks if a value is an empty array
|
|
270
|
+
- **isEmptyObject(value: unknown): value is Record<string, never>** - Checks if a value is an empty object
|
|
271
|
+
- **isNull(value: unknown): value is null** - Checks if a value is null
|
|
272
|
+
- **isUndefined(value: unknown): value is undefined** - Checks if a value is undefined
|
|
273
|
+
- **isFiniteNumber(value: unknown): value is number** - Checks if a value is a finite number
|
|
274
|
+
- **isInteger(value: unknown): value is number** - Checks if a value is an integer
|
|
275
|
+
- **isNaN(value: unknown): boolean** - Checks if a value is NaN
|
|
276
|
+
- **isDate(value: unknown): value is Date** - Checks if a value is a Date object
|
|
277
|
+
- **isValidDate(value: unknown): value is Date** - Checks if a value is a valid Date object (not Invalid Date)
|
|
278
|
+
- **isRegExp(value: unknown): value is RegExp** - Checks if a value is a RegExp object
|
|
279
|
+
- **isMap(value: unknown): value is Map<unknown, unknown>** - Checks if a value is a Map
|
|
280
|
+
- **isSet(value: unknown): value is Set<unknown>** - Checks if a value is a Set
|
|
281
|
+
- **isSymbol(value: unknown): value is symbol** - Checks if a value is a Symbol
|
|
282
|
+
|
|
283
|
+
### Math Utilities
|
|
284
|
+
|
|
285
|
+
- **calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number** - Calculates the Euclidean distance between two points
|
|
286
|
+
- **calculateMovingSpeed(delta: number, elapsedTime: number): number** - Calculates moving speed
|
|
287
|
+
- **calculatePercentage(value: number, percentage: number): number** - Calculates the specified percentage of a value
|
|
288
|
+
|
|
289
|
+
### DOM Utilities
|
|
290
|
+
|
|
291
|
+
- **getTransformationValues(element: HTMLElement): { translateX: number, translateY: number }** - Gets transformation values from an HTML element
|
|
292
|
+
|
|
293
|
+
### Other Utilities
|
|
294
|
+
|
|
295
|
+
- **assert(condition: any, message: string): asserts condition** - Asserts that a condition is truthy, throwing an error with the provided message if it's not
|
|
296
|
+
|
|
297
|
+
## Contributing
|
|
298
|
+
|
|
299
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
300
|
+
|
|
301
|
+
1. Fork the repository
|
|
302
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
303
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
304
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
305
|
+
5. Open a Pull Request
|
|
306
|
+
|
|
307
|
+
## License
|
|
308
|
+
|
|
309
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
310
|
+
|
|
311
|
+
## Author
|
|
312
|
+
|
|
313
|
+
Mykhailo Aliinyk <m.aliynik@gmail.com>
|
package/dist/array.d.ts
CHANGED
|
@@ -11,3 +11,74 @@
|
|
|
11
11
|
* @returns A new array containing only truthy `Item` values.
|
|
12
12
|
*/
|
|
13
13
|
export declare const boolFilter: <T>(array: (T | false | null | undefined)[]) => T[];
|
|
14
|
+
/**
|
|
15
|
+
* Returns a new array with duplicate values removed.
|
|
16
|
+
*
|
|
17
|
+
* Uses Set for efficient duplicate removal while preserving the original order.
|
|
18
|
+
*
|
|
19
|
+
* @template T - The type of the items in the array.
|
|
20
|
+
*
|
|
21
|
+
* @param array - The input array that may contain duplicate values.
|
|
22
|
+
*
|
|
23
|
+
* @returns A new array with only unique values, maintaining the original order.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* unique([1, 2, 2, 3, 1, 4]); // [1, 2, 3, 4]
|
|
28
|
+
* unique(['a', 'b', 'a', 'c']); // ['a', 'b', 'c']
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare const unique: <T>(array: T[]) => T[];
|
|
32
|
+
/**
|
|
33
|
+
* Splits an array into chunks of the specified size.
|
|
34
|
+
*
|
|
35
|
+
* Useful for pagination, batch processing, or creating grid layouts.
|
|
36
|
+
*
|
|
37
|
+
* @template T - The type of the items in the array.
|
|
38
|
+
*
|
|
39
|
+
* @param array - The input array to be chunked.
|
|
40
|
+
* @param size - The size of each chunk. Must be greater than 0.
|
|
41
|
+
*
|
|
42
|
+
* @returns An array of chunks, where each chunk is an array of the specified size
|
|
43
|
+
* (except possibly the last chunk, which may be smaller).
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
48
|
+
* chunk(['a', 'b', 'c', 'd'], 3); // [['a', 'b', 'c'], ['d']]
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare const chunk: <T>(array: T[], size: number) => T[][];
|
|
52
|
+
/**
|
|
53
|
+
* Returns an array containing elements that exist in all provided arrays.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The type of the items in the arrays.
|
|
56
|
+
*
|
|
57
|
+
* @param arrays - Two or more arrays to find common elements from.
|
|
58
|
+
*
|
|
59
|
+
* @returns A new array containing only the elements that exist in all input arrays.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
|
|
64
|
+
* intersection(['a', 'b', 'c'], ['b', 'c', 'd'], ['b', 'e']); // ['b']
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare const intersection: <T>(...arrays: T[][]) => T[];
|
|
68
|
+
/**
|
|
69
|
+
* Returns elements from the first array that don't exist in the second array.
|
|
70
|
+
*
|
|
71
|
+
* @template T - The type of the items in the arrays.
|
|
72
|
+
*
|
|
73
|
+
* @param array - The source array.
|
|
74
|
+
* @param exclude - The array containing elements to exclude.
|
|
75
|
+
*
|
|
76
|
+
* @returns A new array with elements from the first array that don't exist in the second array.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* difference([1, 2, 3, 4], [2, 4]); // [1, 3]
|
|
81
|
+
* difference(['a', 'b', 'c'], ['b']); // ['a', 'c']
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare const difference: <T>(array: T[], exclude: T[]) => T[];
|