@react-hive/honey-utils 1.1.0 โ 1.3.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 +292 -0
- package/dist/README.md +292 -0
- package/dist/dom.d.ts +13 -0
- package/dist/guards.d.ts +148 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.dev.cjs +291 -6
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/math.d.ts +29 -0
- package/dist/string.d.ts +12 -3
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -0,0 +1,292 @@
|
|
|
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 { boolFilter } from '@react-hive/honey-utils';
|
|
66
|
+
|
|
67
|
+
// Filter out falsy values from an array
|
|
68
|
+
const array = [0, 1, false, 2, '', 3, null, undefined, true];
|
|
69
|
+
boolFilter(array); // [1, 2, 3, true]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Function Utilities
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { noop, invokeIfFunction } from '@react-hive/honey-utils';
|
|
76
|
+
|
|
77
|
+
// No-operation function
|
|
78
|
+
noop(); // does nothing
|
|
79
|
+
|
|
80
|
+
// Invoke if function, otherwise return value
|
|
81
|
+
const fn = (x: number) => x * 2;
|
|
82
|
+
|
|
83
|
+
invokeIfFunction(fn, 5); // 10
|
|
84
|
+
invokeIfFunction('not a function', 5); // 'not a function'
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Type Guards
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import {
|
|
91
|
+
isString,
|
|
92
|
+
isNumber,
|
|
93
|
+
isBool,
|
|
94
|
+
isObject,
|
|
95
|
+
isFunction,
|
|
96
|
+
isPromise,
|
|
97
|
+
isNil,
|
|
98
|
+
isNilOrEmptyString,
|
|
99
|
+
isArray,
|
|
100
|
+
isEmptyArray,
|
|
101
|
+
isEmptyObject,
|
|
102
|
+
isNull,
|
|
103
|
+
isUndefined,
|
|
104
|
+
isDate,
|
|
105
|
+
isValidDate,
|
|
106
|
+
isRegExp,
|
|
107
|
+
isMap,
|
|
108
|
+
isSet
|
|
109
|
+
} from '@react-hive/honey-utils';
|
|
110
|
+
|
|
111
|
+
// Check if value is a string
|
|
112
|
+
isString('hello'); // true
|
|
113
|
+
isString(123); // false
|
|
114
|
+
|
|
115
|
+
// Check if value is a number
|
|
116
|
+
isNumber(123); // true
|
|
117
|
+
isNumber('123'); // false
|
|
118
|
+
|
|
119
|
+
// Check if value is a boolean
|
|
120
|
+
isBool(true); // true
|
|
121
|
+
isBool('true'); // false
|
|
122
|
+
|
|
123
|
+
// Check if value is an object
|
|
124
|
+
isObject({}); // true
|
|
125
|
+
isObject('object'); // false
|
|
126
|
+
|
|
127
|
+
// Check if value is a function
|
|
128
|
+
isFunction(() => {}); // true
|
|
129
|
+
isFunction({}); // false
|
|
130
|
+
|
|
131
|
+
// Check if value is a Promise
|
|
132
|
+
isPromise(Promise.resolve()); // true
|
|
133
|
+
isPromise({}); // false
|
|
134
|
+
|
|
135
|
+
// Check if value is null or undefined
|
|
136
|
+
isNil(null); // true
|
|
137
|
+
isNil(undefined); // true
|
|
138
|
+
isNil(''); // false
|
|
139
|
+
|
|
140
|
+
// Check if value is null, undefined, or empty string
|
|
141
|
+
isNilOrEmptyString(''); // true
|
|
142
|
+
isNilOrEmptyString(null); // true
|
|
143
|
+
isNilOrEmptyString('hello'); // false
|
|
144
|
+
|
|
145
|
+
// Check if value is an array
|
|
146
|
+
isArray([1, 2, 3]); // true
|
|
147
|
+
isArray({}); // false
|
|
148
|
+
|
|
149
|
+
// Check if value is an empty array
|
|
150
|
+
isEmptyArray([]); // true
|
|
151
|
+
isEmptyArray([1, 2, 3]); // false
|
|
152
|
+
|
|
153
|
+
// Check if value is an empty object
|
|
154
|
+
isEmptyObject({}); // true
|
|
155
|
+
isEmptyObject({ key: 'value' }); // false
|
|
156
|
+
|
|
157
|
+
// Check if value is a Date object
|
|
158
|
+
isDate(new Date()); // true
|
|
159
|
+
isDate('2023-01-01'); // false
|
|
160
|
+
|
|
161
|
+
// Check if value is a valid Date object
|
|
162
|
+
isValidDate(new Date()); // true
|
|
163
|
+
isValidDate(new Date('invalid')); // false
|
|
164
|
+
|
|
165
|
+
// Check if value is a RegExp
|
|
166
|
+
isRegExp(/test/); // true
|
|
167
|
+
isRegExp('test'); // false
|
|
168
|
+
|
|
169
|
+
// Check if value is a Map or Set
|
|
170
|
+
isMap(new Map()); // true
|
|
171
|
+
isSet(new Set()); // true
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Math Utilities
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import {
|
|
178
|
+
calculateEuclideanDistance,
|
|
179
|
+
calculateMovingSpeed,
|
|
180
|
+
calculatePercentage
|
|
181
|
+
} from '@react-hive/honey-utils';
|
|
182
|
+
|
|
183
|
+
// Calculate Euclidean distance between two points
|
|
184
|
+
calculateEuclideanDistance(0, 0, 3, 4); // 5
|
|
185
|
+
|
|
186
|
+
// Calculate moving speed
|
|
187
|
+
calculateMovingSpeed(100, 5); // 20
|
|
188
|
+
|
|
189
|
+
// Calculate percentage of a value
|
|
190
|
+
calculatePercentage(200, 25); // 50
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### DOM Utilities
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { getTransformationValues } from '@react-hive/honey-utils';
|
|
197
|
+
|
|
198
|
+
// Get transformation values from an HTML element
|
|
199
|
+
const element = document.getElementById('my-element');
|
|
200
|
+
if (element) {
|
|
201
|
+
const { translateX, translateY } = getTransformationValues(element);
|
|
202
|
+
|
|
203
|
+
console.log(`Element is translated by ${translateX}px horizontally and ${translateY}px vertically`);
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Assert Function
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import { assert } from '@react-hive/honey-utils';
|
|
211
|
+
|
|
212
|
+
// Assert a condition
|
|
213
|
+
function divide(a: number, b: number): number {
|
|
214
|
+
assert(b !== 0, 'Cannot divide by zero');
|
|
215
|
+
return a / b;
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## API Documentation
|
|
220
|
+
|
|
221
|
+
### String Utilities
|
|
222
|
+
|
|
223
|
+
- **toKebabCase(input: string): string** - Converts a string to kebab-case
|
|
224
|
+
- **camelToDashCase(input: string): string** - Converts camelCase to dash-case
|
|
225
|
+
- **splitStringIntoWords(input: string): string[]** - Splits a string into an array of words
|
|
226
|
+
- **hashString(input: string): string** - Generates a short hash from a string
|
|
227
|
+
|
|
228
|
+
### Array Utilities
|
|
229
|
+
|
|
230
|
+
- **boolFilter<T>(array: (T | false | null | undefined)[]): T[]** - Filters out falsy values from an array
|
|
231
|
+
|
|
232
|
+
### Function Utilities
|
|
233
|
+
|
|
234
|
+
- **noop(): void** - A no-operation function
|
|
235
|
+
- **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
|
|
236
|
+
|
|
237
|
+
### Type Guards
|
|
238
|
+
|
|
239
|
+
- **isString(value: unknown): value is string** - Checks if a value is a string
|
|
240
|
+
- **isNumber(value: unknown): value is number** - Checks if a value is a number
|
|
241
|
+
- **isBool(value: unknown): value is boolean** - Checks if a value is a boolean
|
|
242
|
+
- **isObject(value: unknown): value is object** - Checks if a value is an object
|
|
243
|
+
- **isFunction(value: unknown): value is Function** - Checks if a value is a function
|
|
244
|
+
- **isPromise<T = unknown>(value: unknown): value is Promise<T>** - Checks if a value is a Promise
|
|
245
|
+
- **isNil(value: unknown): value is null | undefined** - Checks if a value is null or undefined
|
|
246
|
+
- **isNilOrEmptyString(value: unknown): value is null | undefined** - Checks if a value is null, undefined, or an empty string
|
|
247
|
+
- **isArray(value: unknown): value is unknown[]** - Checks if a value is an array
|
|
248
|
+
- **isEmptyArray(value: unknown): value is []** - Checks if a value is an empty array
|
|
249
|
+
- **isEmptyObject(value: unknown): value is Record<string, never>** - Checks if a value is an empty object
|
|
250
|
+
- **isNull(value: unknown): value is null** - Checks if a value is null
|
|
251
|
+
- **isUndefined(value: unknown): value is undefined** - Checks if a value is undefined
|
|
252
|
+
- **isFiniteNumber(value: unknown): value is number** - Checks if a value is a finite number
|
|
253
|
+
- **isInteger(value: unknown): value is number** - Checks if a value is an integer
|
|
254
|
+
- **isNaN(value: unknown): boolean** - Checks if a value is NaN
|
|
255
|
+
- **isDate(value: unknown): value is Date** - Checks if a value is a Date object
|
|
256
|
+
- **isValidDate(value: unknown): value is Date** - Checks if a value is a valid Date object (not Invalid Date)
|
|
257
|
+
- **isRegExp(value: unknown): value is RegExp** - Checks if a value is a RegExp object
|
|
258
|
+
- **isMap(value: unknown): value is Map<unknown, unknown>** - Checks if a value is a Map
|
|
259
|
+
- **isSet(value: unknown): value is Set<unknown>** - Checks if a value is a Set
|
|
260
|
+
- **isSymbol(value: unknown): value is symbol** - Checks if a value is a Symbol
|
|
261
|
+
|
|
262
|
+
### Math Utilities
|
|
263
|
+
|
|
264
|
+
- **calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number** - Calculates the Euclidean distance between two points
|
|
265
|
+
- **calculateMovingSpeed(delta: number, elapsedTime: number): number** - Calculates moving speed
|
|
266
|
+
- **calculatePercentage(value: number, percentage: number): number** - Calculates the specified percentage of a value
|
|
267
|
+
|
|
268
|
+
### DOM Utilities
|
|
269
|
+
|
|
270
|
+
- **getTransformationValues(element: HTMLElement): { translateX: number, translateY: number }** - Gets transformation values from an HTML element
|
|
271
|
+
|
|
272
|
+
### Other Utilities
|
|
273
|
+
|
|
274
|
+
- **assert(condition: any, message: string): asserts condition** - Asserts that a condition is truthy, throwing an error with the provided message if it's not
|
|
275
|
+
|
|
276
|
+
## Contributing
|
|
277
|
+
|
|
278
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
279
|
+
|
|
280
|
+
1. Fork the repository
|
|
281
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
282
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
283
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
284
|
+
5. Open a Pull Request
|
|
285
|
+
|
|
286
|
+
## License
|
|
287
|
+
|
|
288
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
289
|
+
|
|
290
|
+
## Author
|
|
291
|
+
|
|
292
|
+
Mykhailo Aliinyk <m.aliynik@gmail.com>
|
package/dist/README.md
CHANGED
|
@@ -0,0 +1,292 @@
|
|
|
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 { boolFilter } from '@react-hive/honey-utils';
|
|
66
|
+
|
|
67
|
+
// Filter out falsy values from an array
|
|
68
|
+
const array = [0, 1, false, 2, '', 3, null, undefined, true];
|
|
69
|
+
boolFilter(array); // [1, 2, 3, true]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Function Utilities
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { noop, invokeIfFunction } from '@react-hive/honey-utils';
|
|
76
|
+
|
|
77
|
+
// No-operation function
|
|
78
|
+
noop(); // does nothing
|
|
79
|
+
|
|
80
|
+
// Invoke if function, otherwise return value
|
|
81
|
+
const fn = (x: number) => x * 2;
|
|
82
|
+
|
|
83
|
+
invokeIfFunction(fn, 5); // 10
|
|
84
|
+
invokeIfFunction('not a function', 5); // 'not a function'
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Type Guards
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import {
|
|
91
|
+
isString,
|
|
92
|
+
isNumber,
|
|
93
|
+
isBool,
|
|
94
|
+
isObject,
|
|
95
|
+
isFunction,
|
|
96
|
+
isPromise,
|
|
97
|
+
isNil,
|
|
98
|
+
isNilOrEmptyString,
|
|
99
|
+
isArray,
|
|
100
|
+
isEmptyArray,
|
|
101
|
+
isEmptyObject,
|
|
102
|
+
isNull,
|
|
103
|
+
isUndefined,
|
|
104
|
+
isDate,
|
|
105
|
+
isValidDate,
|
|
106
|
+
isRegExp,
|
|
107
|
+
isMap,
|
|
108
|
+
isSet
|
|
109
|
+
} from '@react-hive/honey-utils';
|
|
110
|
+
|
|
111
|
+
// Check if value is a string
|
|
112
|
+
isString('hello'); // true
|
|
113
|
+
isString(123); // false
|
|
114
|
+
|
|
115
|
+
// Check if value is a number
|
|
116
|
+
isNumber(123); // true
|
|
117
|
+
isNumber('123'); // false
|
|
118
|
+
|
|
119
|
+
// Check if value is a boolean
|
|
120
|
+
isBool(true); // true
|
|
121
|
+
isBool('true'); // false
|
|
122
|
+
|
|
123
|
+
// Check if value is an object
|
|
124
|
+
isObject({}); // true
|
|
125
|
+
isObject('object'); // false
|
|
126
|
+
|
|
127
|
+
// Check if value is a function
|
|
128
|
+
isFunction(() => {}); // true
|
|
129
|
+
isFunction({}); // false
|
|
130
|
+
|
|
131
|
+
// Check if value is a Promise
|
|
132
|
+
isPromise(Promise.resolve()); // true
|
|
133
|
+
isPromise({}); // false
|
|
134
|
+
|
|
135
|
+
// Check if value is null or undefined
|
|
136
|
+
isNil(null); // true
|
|
137
|
+
isNil(undefined); // true
|
|
138
|
+
isNil(''); // false
|
|
139
|
+
|
|
140
|
+
// Check if value is null, undefined, or empty string
|
|
141
|
+
isNilOrEmptyString(''); // true
|
|
142
|
+
isNilOrEmptyString(null); // true
|
|
143
|
+
isNilOrEmptyString('hello'); // false
|
|
144
|
+
|
|
145
|
+
// Check if value is an array
|
|
146
|
+
isArray([1, 2, 3]); // true
|
|
147
|
+
isArray({}); // false
|
|
148
|
+
|
|
149
|
+
// Check if value is an empty array
|
|
150
|
+
isEmptyArray([]); // true
|
|
151
|
+
isEmptyArray([1, 2, 3]); // false
|
|
152
|
+
|
|
153
|
+
// Check if value is an empty object
|
|
154
|
+
isEmptyObject({}); // true
|
|
155
|
+
isEmptyObject({ key: 'value' }); // false
|
|
156
|
+
|
|
157
|
+
// Check if value is a Date object
|
|
158
|
+
isDate(new Date()); // true
|
|
159
|
+
isDate('2023-01-01'); // false
|
|
160
|
+
|
|
161
|
+
// Check if value is a valid Date object
|
|
162
|
+
isValidDate(new Date()); // true
|
|
163
|
+
isValidDate(new Date('invalid')); // false
|
|
164
|
+
|
|
165
|
+
// Check if value is a RegExp
|
|
166
|
+
isRegExp(/test/); // true
|
|
167
|
+
isRegExp('test'); // false
|
|
168
|
+
|
|
169
|
+
// Check if value is a Map or Set
|
|
170
|
+
isMap(new Map()); // true
|
|
171
|
+
isSet(new Set()); // true
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Math Utilities
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import {
|
|
178
|
+
calculateEuclideanDistance,
|
|
179
|
+
calculateMovingSpeed,
|
|
180
|
+
calculatePercentage
|
|
181
|
+
} from '@react-hive/honey-utils';
|
|
182
|
+
|
|
183
|
+
// Calculate Euclidean distance between two points
|
|
184
|
+
calculateEuclideanDistance(0, 0, 3, 4); // 5
|
|
185
|
+
|
|
186
|
+
// Calculate moving speed
|
|
187
|
+
calculateMovingSpeed(100, 5); // 20
|
|
188
|
+
|
|
189
|
+
// Calculate percentage of a value
|
|
190
|
+
calculatePercentage(200, 25); // 50
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### DOM Utilities
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { getTransformationValues } from '@react-hive/honey-utils';
|
|
197
|
+
|
|
198
|
+
// Get transformation values from an HTML element
|
|
199
|
+
const element = document.getElementById('my-element');
|
|
200
|
+
if (element) {
|
|
201
|
+
const { translateX, translateY } = getTransformationValues(element);
|
|
202
|
+
|
|
203
|
+
console.log(`Element is translated by ${translateX}px horizontally and ${translateY}px vertically`);
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Assert Function
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import { assert } from '@react-hive/honey-utils';
|
|
211
|
+
|
|
212
|
+
// Assert a condition
|
|
213
|
+
function divide(a: number, b: number): number {
|
|
214
|
+
assert(b !== 0, 'Cannot divide by zero');
|
|
215
|
+
return a / b;
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## API Documentation
|
|
220
|
+
|
|
221
|
+
### String Utilities
|
|
222
|
+
|
|
223
|
+
- **toKebabCase(input: string): string** - Converts a string to kebab-case
|
|
224
|
+
- **camelToDashCase(input: string): string** - Converts camelCase to dash-case
|
|
225
|
+
- **splitStringIntoWords(input: string): string[]** - Splits a string into an array of words
|
|
226
|
+
- **hashString(input: string): string** - Generates a short hash from a string
|
|
227
|
+
|
|
228
|
+
### Array Utilities
|
|
229
|
+
|
|
230
|
+
- **boolFilter<T>(array: (T | false | null | undefined)[]): T[]** - Filters out falsy values from an array
|
|
231
|
+
|
|
232
|
+
### Function Utilities
|
|
233
|
+
|
|
234
|
+
- **noop(): void** - A no-operation function
|
|
235
|
+
- **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
|
|
236
|
+
|
|
237
|
+
### Type Guards
|
|
238
|
+
|
|
239
|
+
- **isString(value: unknown): value is string** - Checks if a value is a string
|
|
240
|
+
- **isNumber(value: unknown): value is number** - Checks if a value is a number
|
|
241
|
+
- **isBool(value: unknown): value is boolean** - Checks if a value is a boolean
|
|
242
|
+
- **isObject(value: unknown): value is object** - Checks if a value is an object
|
|
243
|
+
- **isFunction(value: unknown): value is Function** - Checks if a value is a function
|
|
244
|
+
- **isPromise<T = unknown>(value: unknown): value is Promise<T>** - Checks if a value is a Promise
|
|
245
|
+
- **isNil(value: unknown): value is null | undefined** - Checks if a value is null or undefined
|
|
246
|
+
- **isNilOrEmptyString(value: unknown): value is null | undefined** - Checks if a value is null, undefined, or an empty string
|
|
247
|
+
- **isArray(value: unknown): value is unknown[]** - Checks if a value is an array
|
|
248
|
+
- **isEmptyArray(value: unknown): value is []** - Checks if a value is an empty array
|
|
249
|
+
- **isEmptyObject(value: unknown): value is Record<string, never>** - Checks if a value is an empty object
|
|
250
|
+
- **isNull(value: unknown): value is null** - Checks if a value is null
|
|
251
|
+
- **isUndefined(value: unknown): value is undefined** - Checks if a value is undefined
|
|
252
|
+
- **isFiniteNumber(value: unknown): value is number** - Checks if a value is a finite number
|
|
253
|
+
- **isInteger(value: unknown): value is number** - Checks if a value is an integer
|
|
254
|
+
- **isNaN(value: unknown): boolean** - Checks if a value is NaN
|
|
255
|
+
- **isDate(value: unknown): value is Date** - Checks if a value is a Date object
|
|
256
|
+
- **isValidDate(value: unknown): value is Date** - Checks if a value is a valid Date object (not Invalid Date)
|
|
257
|
+
- **isRegExp(value: unknown): value is RegExp** - Checks if a value is a RegExp object
|
|
258
|
+
- **isMap(value: unknown): value is Map<unknown, unknown>** - Checks if a value is a Map
|
|
259
|
+
- **isSet(value: unknown): value is Set<unknown>** - Checks if a value is a Set
|
|
260
|
+
- **isSymbol(value: unknown): value is symbol** - Checks if a value is a Symbol
|
|
261
|
+
|
|
262
|
+
### Math Utilities
|
|
263
|
+
|
|
264
|
+
- **calculateEuclideanDistance(startX: number, startY: number, endX: number, endY: number): number** - Calculates the Euclidean distance between two points
|
|
265
|
+
- **calculateMovingSpeed(delta: number, elapsedTime: number): number** - Calculates moving speed
|
|
266
|
+
- **calculatePercentage(value: number, percentage: number): number** - Calculates the specified percentage of a value
|
|
267
|
+
|
|
268
|
+
### DOM Utilities
|
|
269
|
+
|
|
270
|
+
- **getTransformationValues(element: HTMLElement): { translateX: number, translateY: number }** - Gets transformation values from an HTML element
|
|
271
|
+
|
|
272
|
+
### Other Utilities
|
|
273
|
+
|
|
274
|
+
- **assert(condition: any, message: string): asserts condition** - Asserts that a condition is truthy, throwing an error with the provided message if it's not
|
|
275
|
+
|
|
276
|
+
## Contributing
|
|
277
|
+
|
|
278
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
279
|
+
|
|
280
|
+
1. Fork the repository
|
|
281
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
282
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
283
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
284
|
+
5. Open a Pull Request
|
|
285
|
+
|
|
286
|
+
## License
|
|
287
|
+
|
|
288
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
289
|
+
|
|
290
|
+
## Author
|
|
291
|
+
|
|
292
|
+
Mykhailo Aliinyk <m.aliynik@gmail.com>
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface HTMLElementTransformationValues {
|
|
2
|
+
translateX: number;
|
|
3
|
+
translateY: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Get various transformation values from the transformation matrix of an element.
|
|
7
|
+
*
|
|
8
|
+
* @param element - The element with a transformation applied.
|
|
9
|
+
*
|
|
10
|
+
* @returns An object containing transformation values.
|
|
11
|
+
*/
|
|
12
|
+
export declare const getTransformationValues: (element: HTMLElement) => HTMLElementTransformationValues;
|
|
13
|
+
export {};
|