@xxanderwp/jstoolkit 1.0.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/LICENSE +0 -0
- package/README.md +247 -0
- package/dist/index.d.ts +956 -0
- package/dist/index.esm.js +1459 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +1506 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
File without changes
|
package/README.md
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
# @xxanderwp/jstoolkit
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@xxanderwp/jstoolkit)
|
|
4
|
+

|
|
5
|
+
[](https://github.com/XXanderWP/JsToolkit/actions)
|
|
6
|
+
[](https://github.com/XXanderWP/JsToolkit/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
A comprehensive Swiss Army knife of JavaScript/TypeScript utilities for everyday development. Clean, tested, and tree-shakeable.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @xxanderwp/jstoolkit
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- 🎯 **Tree-shakeable** - Only import what you need
|
|
19
|
+
- 📦 **TypeScript** - Full TypeScript support with type definitions
|
|
20
|
+
- ✅ **Well-tested** - High test coverage with Jest
|
|
21
|
+
- 🚀 **Zero dependencies** (except md5 for hashing)
|
|
22
|
+
- 📚 **Comprehensive** - 120+ utility functions across 9 categories
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { formatNumber, randomString, chunk } from '@xxanderwp/jstoolkit';
|
|
28
|
+
|
|
29
|
+
// Format numbers with thousands separator
|
|
30
|
+
formatNumber(1000000); // "1 000 000"
|
|
31
|
+
|
|
32
|
+
// Generate random string
|
|
33
|
+
randomString(10); // "aB3dE7fG9h"
|
|
34
|
+
|
|
35
|
+
// Split array into chunks
|
|
36
|
+
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Modules
|
|
40
|
+
|
|
41
|
+
### String Utilities
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { string } from '@xxanderwp/jstoolkit';
|
|
45
|
+
// or import individual functions
|
|
46
|
+
import { randomString, capitalize, truncate } from '@xxanderwp/jstoolkit';
|
|
47
|
+
|
|
48
|
+
randomString(10); // Random alphanumeric string
|
|
49
|
+
capitalize('hello'); // "Hello"
|
|
50
|
+
truncate('Long text...', 10); // "Long te..."
|
|
51
|
+
toCamelCase('hello-world'); // "helloWorld"
|
|
52
|
+
toSnakeCase('helloWorld'); // "hello_world"
|
|
53
|
+
toKebabCase('helloWorld'); // "hello-world"
|
|
54
|
+
removeWhitespace('hello world'); // "helloworld"
|
|
55
|
+
countOccurrences('test test', 'test'); // 2
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Array Utilities
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { array } from '@xxanderwp/jstoolkit';
|
|
62
|
+
|
|
63
|
+
randomElement([1, 2, 3]); // Random element
|
|
64
|
+
shuffle([1, 2, 3, 4]); // Shuffled array
|
|
65
|
+
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
66
|
+
unique([1, 2, 2, 3]); // [1, 2, 3]
|
|
67
|
+
intersection([1, 2], [2, 3]); // [2]
|
|
68
|
+
difference([1, 2], [2, 3]); // [1]
|
|
69
|
+
groupBy(arr, item => item.type); // Group by property
|
|
70
|
+
flatten([[1, 2], [3, 4]]); // [1, 2, 3, 4]
|
|
71
|
+
sortBy(arr, [{ key: 'age', order: 'ASC' }]); // Sort by multiple keys
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Math Utilities
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { math } from '@xxanderwp/jstoolkit';
|
|
78
|
+
|
|
79
|
+
randomInt(1, 10); // Random integer
|
|
80
|
+
randomFloat(1.5, 5.5); // Random float
|
|
81
|
+
round(3.14159, 2); // 3.14
|
|
82
|
+
lerp(0, 10, 0.5); // 5 (interpolation)
|
|
83
|
+
lerp2D(point1, point2, 0.5); // Interpolate 2D points
|
|
84
|
+
clamp(15, 0, 10); // 10
|
|
85
|
+
mapRange(5, 0, 10, 0, 100); // 50
|
|
86
|
+
percentage(25, 100); // 25
|
|
87
|
+
average([1, 2, 3, 4, 5]); // 3
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Time Utilities
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { time } from '@xxanderwp/jstoolkit';
|
|
94
|
+
|
|
95
|
+
timestamp(); // Current Unix timestamp (seconds)
|
|
96
|
+
timestampMs(); // Current timestamp (milliseconds)
|
|
97
|
+
formatDateTime(); // "07.02.2026 15:30"
|
|
98
|
+
formatDate(); // "07.02.2026"
|
|
99
|
+
formatDuration(3665); // "01:01:05"
|
|
100
|
+
formatTimestamp(1234567890); // Smart date/time formatting
|
|
101
|
+
sleep(1000); // Promise-based delay
|
|
102
|
+
timeAgo(date); // "2 hours ago"
|
|
103
|
+
isToday(date); // Check if date is today
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Encoding Utilities
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { encoding } from '@xxanderwp/jstoolkit';
|
|
110
|
+
|
|
111
|
+
toBase64('hello'); // Base64 encode
|
|
112
|
+
fromBase64('aGVsbG8='); // Base64 decode
|
|
113
|
+
hash('password'); // MD5 hash
|
|
114
|
+
encodeUri('hello world'); // URL encode
|
|
115
|
+
decodeUri('hello%20world'); // URL decode
|
|
116
|
+
hexToBytes('48656c6c6f'); // Hex to bytes
|
|
117
|
+
bytesToHex(bytes); // Bytes to hex
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Validation Utilities
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { validation } from '@xxanderwp/jstoolkit';
|
|
124
|
+
|
|
125
|
+
isEmoji('😀'); // true
|
|
126
|
+
isEmail('test@example.com'); // true
|
|
127
|
+
isUrl('https://example.com'); // true
|
|
128
|
+
isImageUrl('pic.jpg'); // true
|
|
129
|
+
isNumeric('123'); // true
|
|
130
|
+
isAlpha('abc'); // true
|
|
131
|
+
isAlphanumeric('abc123'); // true
|
|
132
|
+
isEmpty(null); // true
|
|
133
|
+
isPlainObject({}); // true
|
|
134
|
+
extractUrl('Visit example.com'); // { url, domain }
|
|
135
|
+
isIPv4('192.168.1.1'); // true
|
|
136
|
+
isHexColor('#fff'); // true
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Format Utilities
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { format } from '@xxanderwp/jstoolkit';
|
|
143
|
+
|
|
144
|
+
formatNumber(1000000); // "1 000 000"
|
|
145
|
+
formatBytes(1536); // "1.5 KB"
|
|
146
|
+
formatCurrency(1234.56); // "$1,234.56"
|
|
147
|
+
formatPhone('1234567890'); // "(123) 456-7890"
|
|
148
|
+
formatCardNumber('1234567890123456'); // "1234 5678 9012 3456"
|
|
149
|
+
maskString('1234567890'); // "******7890"
|
|
150
|
+
formatPercentage(50.5); // "50.50%"
|
|
151
|
+
pluralize(2, 'item'); // "2 items"
|
|
152
|
+
abbreviateNumber(1500000); // "1.5M"
|
|
153
|
+
titleCase('hello world'); // "Hello World"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Random Utilities
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { random } from '@xxanderwp/jstoolkit';
|
|
160
|
+
|
|
161
|
+
randomInt(1, 10); // Random integer
|
|
162
|
+
randomFloat(1.5, 5.5); // Random float
|
|
163
|
+
randomBoolean(); // Random true/false
|
|
164
|
+
randomSample([1, 2, 3], 2); // Pick 2 random elements
|
|
165
|
+
randomColor(); // "#a3c2f1"
|
|
166
|
+
randomUUID(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
167
|
+
randomWeighted([1, 2, 3], [10, 5, 1]); // Weighted random choice
|
|
168
|
+
randomDate(start, end); // Random date in range
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Object Utilities
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import { object } from '@xxanderwp/jstoolkit';
|
|
175
|
+
|
|
176
|
+
deepClone(obj); // Deep clone object
|
|
177
|
+
deepMerge(target, source1, source2); // Deep merge objects
|
|
178
|
+
get(obj, 'user.address.city'); // Get nested property
|
|
179
|
+
set(obj, 'user.name', 'John'); // Set nested property
|
|
180
|
+
pick(obj, ['a', 'b']); // Pick properties
|
|
181
|
+
omit(obj, ['c']); // Omit properties
|
|
182
|
+
flatten({ a: { b: 1 } }); // { 'a.b': 1 }
|
|
183
|
+
unflatten({ 'a.b': 1 }); // { a: { b: 1 } }
|
|
184
|
+
keys(obj); // Get all keys (including nested)
|
|
185
|
+
values(obj); // Get all values (including nested)
|
|
186
|
+
isEqual(obj1, obj2); // Deep equality check
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Usage Patterns
|
|
190
|
+
|
|
191
|
+
### Namespace Import
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import { string, array, math, object } from '@xxanderwp/jstoolkit';
|
|
195
|
+
|
|
196
|
+
string.capitalize('hello');
|
|
197
|
+
array.unique([1, 2, 2, 3]);
|
|
198
|
+
math.round(3.14159, 2);
|
|
199
|
+
object.deepClone(obj);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Named Import (Tree-shakeable)
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import { capitalize, unique, round } from '@xxanderwp/jstoolkit';
|
|
206
|
+
|
|
207
|
+
capitalize('hello');
|
|
208
|
+
unique([1, 2, 2, 3]);
|
|
209
|
+
round(3.14159, 2);
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## TypeScript Support
|
|
213
|
+
|
|
214
|
+
Full TypeScript support with type definitions included:
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
import { chunk, groupBy } from '@xxanderwp/jstoolkit';
|
|
218
|
+
|
|
219
|
+
const numbers: number[] = [1, 2, 3, 4, 5];
|
|
220
|
+
const chunks: number[][] = chunk(numbers, 2);
|
|
221
|
+
|
|
222
|
+
interface User {
|
|
223
|
+
name: string;
|
|
224
|
+
role: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const users: User[] = [/* ... */];
|
|
228
|
+
const grouped: Record<string, User[]> = groupBy(users, u => u.role);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Browser Support
|
|
232
|
+
|
|
233
|
+
Works in all modern browsers and Node.js environments (Node 14+).
|
|
234
|
+
|
|
235
|
+
## Contributing
|
|
236
|
+
|
|
237
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
238
|
+
|
|
239
|
+
## License
|
|
240
|
+
|
|
241
|
+
MIT © xxanderwp
|
|
242
|
+
|
|
243
|
+
## Links
|
|
244
|
+
|
|
245
|
+
- [GitHub Repository](https://github.com/xxanderwp/jstoolkit)
|
|
246
|
+
- [NPM Package](https://www.npmjs.com/package/@xxanderwp/jstoolkit)
|
|
247
|
+
- [Issue Tracker](https://github.com/xxanderwp/jstoolkit/issues)
|