@naman_deep_singh/js-extensions 1.0.0 → 1.1.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 +388 -39
- package/dist/{array-extensions.js → cjs/array-extensions.js} +18 -0
- package/dist/{index.d.ts → cjs/index.d.ts} +6 -0
- package/dist/{index.js → cjs/index.js} +12 -3
- package/dist/cjs/number-extensions.js +103 -0
- package/dist/cjs/object-extensions.js +90 -0
- package/dist/cjs/performance.d.ts +18 -0
- package/dist/cjs/performance.js +63 -0
- package/dist/{string-extensions.js → cjs/string-extensions.js} +15 -0
- package/dist/{types.d.ts → cjs/types.d.ts} +20 -0
- package/dist/cjs/validation.d.ts +4 -0
- package/dist/cjs/validation.js +31 -0
- package/dist/esm/array-extensions.d.ts +1 -0
- package/dist/esm/array-extensions.js +80 -0
- package/dist/esm/index.d.ts +46 -0
- package/dist/esm/index.js +50 -0
- package/dist/esm/number-extensions.d.ts +1 -0
- package/dist/esm/number-extensions.js +100 -0
- package/dist/esm/object-extensions.d.ts +1 -0
- package/dist/esm/object-extensions.js +87 -0
- package/dist/esm/performance.d.ts +18 -0
- package/dist/esm/performance.js +57 -0
- package/dist/esm/string-extensions.d.ts +1 -0
- package/dist/esm/string-extensions.js +66 -0
- package/dist/esm/types.d.ts +71 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/validation.d.ts +4 -0
- package/dist/esm/validation.js +25 -0
- package/dist/types/array-extensions.d.ts +1 -0
- package/dist/types/index.d.ts +46 -0
- package/dist/types/number-extensions.d.ts +1 -0
- package/dist/types/object-extensions.d.ts +1 -0
- package/dist/types/performance.d.ts +18 -0
- package/dist/types/string-extensions.d.ts +1 -0
- package/dist/types/types.d.ts +71 -0
- package/dist/types/validation.d.ts +4 -0
- package/package.json +23 -8
- package/dist/number-extensions.js +0 -72
- package/dist/object-extensions.js +0 -53
- package/src/array-extensions.ts +0 -73
- package/src/index.ts +0 -55
- package/src/number-extensions.ts +0 -73
- package/src/object-extensions.ts +0 -53
- package/src/string-extensions.ts +0 -63
- package/src/types.ts +0 -56
- package/tsconfig.json +0 -22
- /package/dist/{array-extensions.d.ts → cjs/array-extensions.d.ts} +0 -0
- /package/dist/{number-extensions.d.ts → cjs/number-extensions.d.ts} +0 -0
- /package/dist/{object-extensions.d.ts → cjs/object-extensions.d.ts} +0 -0
- /package/dist/{string-extensions.d.ts → cjs/string-extensions.d.ts} +0 -0
- /package/dist/{types.js → cjs/types.js} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# @naman_deep_singh/js-extensions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Version:** 1.1.0
|
|
4
|
+
|
|
5
|
+
Universal JavaScript prototype extensions for common development utilities. Works in both Node.js and browser environments with 50+ utility methods.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -10,9 +12,7 @@ npm install @naman_deep_singh/js-extensions
|
|
|
10
12
|
pnpm add @naman_deep_singh/js-extensions
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
### Initialize All Extensions
|
|
15
|
+
## Quick Start
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
18
|
import { initExtensions } from '@naman_deep_singh/js-extensions';
|
|
@@ -20,63 +20,290 @@ import { initExtensions } from '@naman_deep_singh/js-extensions';
|
|
|
20
20
|
// Initialize all extensions
|
|
21
21
|
initExtensions();
|
|
22
22
|
|
|
23
|
-
//
|
|
23
|
+
// String utilities
|
|
24
24
|
"hello world".toCapitalize(); // "Hello world"
|
|
25
|
+
"test@email.com".isEmail(); // true
|
|
26
|
+
"hello world".words(); // ["hello", "world"]
|
|
27
|
+
|
|
28
|
+
// Array utilities
|
|
25
29
|
[1, 2, 2, 3].unique(); // [1, 2, 3]
|
|
30
|
+
[1, 2, 3, 4, 5].chunk(2); // [[1, 2], [3, 4], [5]]
|
|
31
|
+
[1, 2, 3].sample(); // Random element
|
|
32
|
+
|
|
33
|
+
// Number utilities
|
|
34
|
+
(42).toOrdinal(); // "42nd"
|
|
35
|
+
(0.75).toPercent(); // "75.00%"
|
|
36
|
+
(5).times(i => console.log(i)); // Logs 0,1,2,3,4
|
|
37
|
+
|
|
38
|
+
// Object utilities
|
|
39
|
+
({a: 1, b: 2}).pick(['a']); // {a: 1}
|
|
40
|
+
({}).isEmpty(); // true
|
|
41
|
+
({user: {name: "John"}}).getPath('user.name'); // "John"
|
|
26
42
|
```
|
|
27
43
|
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
28
46
|
### Selective Extensions
|
|
29
47
|
|
|
30
48
|
```typescript
|
|
31
|
-
import { extend } from '@naman_deep_singh/js-extensions';
|
|
49
|
+
import { initExtensions, extend } from '@naman_deep_singh/js-extensions';
|
|
50
|
+
|
|
51
|
+
// Only specific types
|
|
52
|
+
initExtensions({
|
|
53
|
+
string: true,
|
|
54
|
+
array: true,
|
|
55
|
+
object: false,
|
|
56
|
+
number: false
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Individual extension functions
|
|
60
|
+
extend.string(); // Only string methods
|
|
61
|
+
extend.array(); // Only array methods
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Performance Configuration
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { initExtensions, setPerformanceConfig } from '@naman_deep_singh/js-extensions';
|
|
32
68
|
|
|
33
|
-
//
|
|
34
|
-
|
|
69
|
+
// Configure performance options
|
|
70
|
+
initExtensions({
|
|
71
|
+
performance: {
|
|
72
|
+
enableCaching: true, // Cache expensive operations
|
|
73
|
+
maxCacheSize: 200, // LRU cache size
|
|
74
|
+
enableValidation: false // Skip input validation for speed
|
|
75
|
+
}
|
|
76
|
+
});
|
|
35
77
|
|
|
36
|
-
//
|
|
37
|
-
|
|
78
|
+
// Or configure separately
|
|
79
|
+
setPerformanceConfig({
|
|
80
|
+
enableCaching: true,
|
|
81
|
+
maxCacheSize: 100
|
|
82
|
+
});
|
|
38
83
|
```
|
|
39
84
|
|
|
40
85
|
## String Extensions
|
|
41
86
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
87
|
+
### Case Conversion
|
|
88
|
+
```typescript
|
|
89
|
+
"hello world".toCapitalize(); // "Hello world"
|
|
90
|
+
"hello-world".toCamelCase(); // "helloWorld"
|
|
91
|
+
"HelloWorld".toKebabCase(); // "hello-world"
|
|
92
|
+
"Hello World".toSnakeCase(); // "hello_world"
|
|
93
|
+
"hello world".toTitleCase(); // "Hello World"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Validation
|
|
97
|
+
```typescript
|
|
98
|
+
"test@example.com".isEmail(); // true
|
|
99
|
+
"https://example.com".isUrl(); // true
|
|
100
|
+
"racecar".isPalindrome(); // true
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Text Processing
|
|
104
|
+
```typescript
|
|
105
|
+
"Long text here".truncate(8); // "Long tex..."
|
|
106
|
+
"Long text here".truncate(8, "!"); // "Long tex!"
|
|
107
|
+
"hello world".removeWhitespace(); // "helloworld"
|
|
108
|
+
"<p>Hello</p>".stripHtml(); // "Hello"
|
|
109
|
+
"hello".reverse(); // "olleh"
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Padding & Formatting
|
|
113
|
+
```typescript
|
|
114
|
+
"5".padStart(3, "0"); // "005"
|
|
115
|
+
"5".padEnd(3, "0"); // "500"
|
|
116
|
+
"hello world hello".count("hello"); // 2
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Text Analysis
|
|
120
|
+
```typescript
|
|
121
|
+
"hello world test".words(); // ["hello", "world", "test"]
|
|
122
|
+
"line1\nline2\nline3".lines(); // ["line1", "line2", "line3"]
|
|
123
|
+
```
|
|
51
124
|
|
|
52
125
|
## Array Extensions
|
|
53
126
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
127
|
+
### Basic Operations
|
|
128
|
+
```typescript
|
|
129
|
+
[1, 2, 2, 3, 1].unique(); // [1, 2, 3]
|
|
130
|
+
[1, 2, 3, 4, 5].shuffle(); // [3, 1, 5, 2, 4] (random)
|
|
131
|
+
[1, 2, 3, 4, 5].chunk(2); // [[1, 2], [3, 4], [5]]
|
|
132
|
+
[0, 1, false, 2, "", 3].compact(); // [1, 2, 3]
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Mathematical Operations
|
|
136
|
+
```typescript
|
|
137
|
+
[1, 2, 3, 4, 5].sum(); // 15
|
|
138
|
+
[1, 2, 3, 4, 5].average(); // 3
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Advanced Filtering & Grouping
|
|
142
|
+
```typescript
|
|
143
|
+
const users = [{name: 'John', age: 25}, {name: 'Jane', age: 30}];
|
|
144
|
+
users.groupBy(u => u.age > 25); // {false: [{name: 'John'...}], true: [{name: 'Jane'...}]}
|
|
145
|
+
users.pluck('name'); // ['John', 'Jane']
|
|
146
|
+
|
|
147
|
+
[1, 2, 3, 4, 5].partition(x => x % 2 === 0); // [[2, 4], [1, 3, 5]]
|
|
148
|
+
[1, 2, 3, 4, 5].findLast(x => x > 3); // 5
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Array Manipulation
|
|
152
|
+
```typescript
|
|
153
|
+
[1, [2, [3, 4]]].flatten(1); // [1, 2, [3, 4]]
|
|
154
|
+
[1, [2, [3, 4]]].deepFlatten(); // [1, 2, 3, 4]
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Set Operations
|
|
158
|
+
```typescript
|
|
159
|
+
[1, 2, 3].difference([2, 3, 4]); // [1]
|
|
160
|
+
[1, 2, 3].intersection([2, 3, 4]); // [2, 3]
|
|
161
|
+
[1, 2, 3].union([3, 4, 5]); // [1, 2, 3, 4, 5]
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Sampling & Slicing
|
|
165
|
+
```typescript
|
|
166
|
+
[1, 2, 3, 4, 5].sample(); // Random element (e.g., 3)
|
|
167
|
+
[1, 2, 3, 4, 5].take(3); // [1, 2, 3]
|
|
168
|
+
[1, 2, 3, 4, 5].drop(2); // [3, 4, 5]
|
|
169
|
+
```
|
|
62
170
|
|
|
63
171
|
## Object Extensions
|
|
64
172
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
173
|
+
### Basic Operations
|
|
174
|
+
```typescript
|
|
175
|
+
({}).isEmpty(); // true
|
|
176
|
+
({a: 1, b: 2}).isEmpty(); // false
|
|
177
|
+
|
|
178
|
+
const obj = {a: 1, b: 2, c: 3};
|
|
179
|
+
obj.pick(['a', 'c']); // {a: 1, c: 3}
|
|
180
|
+
obj.omit(['b']); // {a: 1, c: 3}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Deep Operations
|
|
184
|
+
```typescript
|
|
185
|
+
const original = {a: 1, b: {c: 2}};
|
|
186
|
+
const cloned = original.deepClone(); // Complete deep copy
|
|
187
|
+
const frozen = original.deepFreeze(); // Recursively frozen
|
|
188
|
+
|
|
189
|
+
const obj1 = {a: 1, b: 2};
|
|
190
|
+
const obj2 = {c: 3, d: 4};
|
|
191
|
+
obj1.merge(obj2); // {a: 1, b: 2, c: 3, d: 4}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Path Operations
|
|
195
|
+
```typescript
|
|
196
|
+
const data = {
|
|
197
|
+
user: {
|
|
198
|
+
profile: {
|
|
199
|
+
name: "John",
|
|
200
|
+
age: 30
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
data.hasPath('user.profile.name'); // true
|
|
206
|
+
data.hasPath('user.profile.email'); // false
|
|
207
|
+
data.getPath('user.profile.name'); // "John"
|
|
208
|
+
data.getPath('user.profile.email', 'N/A'); // "N/A"
|
|
209
|
+
data.setPath('user.profile.email', 'john@example.com');
|
|
210
|
+
// Sets nested property, creates path if needed
|
|
211
|
+
```
|
|
70
212
|
|
|
71
213
|
## Number Extensions
|
|
72
214
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
215
|
+
### Formatting
|
|
216
|
+
```typescript
|
|
217
|
+
(0.75).toPercent(); // "75.00%"
|
|
218
|
+
(0.123).toPercent(1); // "12.3%"
|
|
219
|
+
(1234.56).toCurrency(); // "$1,234.56"
|
|
220
|
+
(1234.56).toCurrency('EUR', 'de-DE'); // "1.234,56 €"
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Ordinal & Roman
|
|
224
|
+
```typescript
|
|
225
|
+
(1).toOrdinal(); // "1st"
|
|
226
|
+
(42).toOrdinal(); // "42nd"
|
|
227
|
+
(1984).toRoman(); // "MCMLXXXIV"
|
|
228
|
+
(2023).toRoman(); // "MMXXIII"
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Mathematical Operations
|
|
232
|
+
```typescript
|
|
233
|
+
(15).clamp(10, 20); // 15
|
|
234
|
+
(5).clamp(10, 20); // 10
|
|
235
|
+
(25).clamp(10, 20); // 20
|
|
236
|
+
|
|
237
|
+
(15).inRange(10, 20); // true
|
|
238
|
+
(5).inRange(10, 20); // false
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Number Properties
|
|
242
|
+
```typescript
|
|
243
|
+
(4).isEven(); // true
|
|
244
|
+
(5).isOdd(); // true
|
|
245
|
+
(7).isPrime(); // true
|
|
246
|
+
(5).factorial(); // 120
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Precision & Math
|
|
250
|
+
```typescript
|
|
251
|
+
(3.14159).round(2); // 3.14
|
|
252
|
+
(3.14159).ceil(2); // 3.15
|
|
253
|
+
(3.14159).floor(2); // 3.14
|
|
254
|
+
(-5).abs(); // 5
|
|
255
|
+
(-5).sign(); // -1
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Iteration
|
|
259
|
+
```typescript
|
|
260
|
+
(3).times(i => console.log(`Item ${i}`));
|
|
261
|
+
// Logs: "Item 0", "Item 1", "Item 2"
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Performance Configuration
|
|
265
|
+
|
|
266
|
+
### Caching System
|
|
267
|
+
```typescript
|
|
268
|
+
import { setPerformanceConfig, getPerformanceConfig } from '@naman_deep_singh/js-extensions';
|
|
269
|
+
|
|
270
|
+
// Enable caching for expensive operations
|
|
271
|
+
setPerformanceConfig({
|
|
272
|
+
enableCaching: true,
|
|
273
|
+
maxCacheSize: 200,
|
|
274
|
+
enableValidation: true
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Check current config
|
|
278
|
+
const config = getPerformanceConfig();
|
|
279
|
+
console.log(config); // {enableCaching: true, maxCacheSize: 200, enableValidation: true}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Methods with Built-in Caching
|
|
283
|
+
The following methods automatically use LRU cache when enabled:
|
|
284
|
+
- **`isPrime()`** - Prime number calculations cached for repeated calls
|
|
285
|
+
- **`factorial()`** - Factorial results cached to avoid recalculation
|
|
286
|
+
- **`toRoman()`** - Roman numeral conversions cached for reuse
|
|
287
|
+
- **`deepClone()`** - Deep clone operations cached for identical objects
|
|
288
|
+
|
|
289
|
+
### External Caching
|
|
290
|
+
```typescript
|
|
291
|
+
import { withCache } from '@naman_deep_singh/js-extensions';
|
|
292
|
+
|
|
293
|
+
// Use caching in your own functions
|
|
294
|
+
const expensiveOperation = (input: string) => {
|
|
295
|
+
return withCache(`myOp_${input}`, () => {
|
|
296
|
+
// Your expensive computation here
|
|
297
|
+
return complexCalculation(input);
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Memory Management
|
|
303
|
+
The LRU (Least Recently Used) cache automatically:
|
|
304
|
+
- Removes oldest entries when cache is full
|
|
305
|
+
- Moves frequently accessed items to front
|
|
306
|
+
- Prevents memory leaks in long-running applications
|
|
80
307
|
|
|
81
308
|
## Browser Usage
|
|
82
309
|
|
|
@@ -85,5 +312,127 @@ initExtensions({ array: true, object: true, string: false, number: false });
|
|
|
85
312
|
<script>
|
|
86
313
|
// Extensions are automatically initialized
|
|
87
314
|
console.log("hello".toCapitalize()); // "Hello"
|
|
315
|
+
console.log([1,2,2,3].unique()); // [1, 2, 3]
|
|
316
|
+
console.log((42).toOrdinal()); // "42nd"
|
|
317
|
+
console.log({a: 1, b: 2}.pick(['a'])); // {a: 1}
|
|
88
318
|
</script>
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## TypeScript Support
|
|
322
|
+
|
|
323
|
+
Full TypeScript support with complete type definitions:
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
// All methods are fully typed
|
|
327
|
+
const result: string = "hello".toCapitalize();
|
|
328
|
+
const numbers: number[] = [1, 2, 2, 3].unique();
|
|
329
|
+
const picked: Pick<{a: number, b: string}, 'a'> = {a: 1, b: "test"}.pick(['a']);
|
|
330
|
+
|
|
331
|
+
// Performance configuration is also typed
|
|
332
|
+
const config: PerformanceConfig = {
|
|
333
|
+
enableCaching: true,
|
|
334
|
+
maxCacheSize: 100,
|
|
335
|
+
enableValidation: false
|
|
336
|
+
};
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
## Package Stats
|
|
340
|
+
|
|
341
|
+
- **60 utility methods** across 4 JavaScript types
|
|
342
|
+
- **Zero dependencies** - lightweight and fast
|
|
343
|
+
- **Universal compatibility** - Node.js and browser
|
|
344
|
+
- **TypeScript native** - complete type definitions
|
|
345
|
+
- **Performance optimized** - optional caching system
|
|
346
|
+
- **Tree-shakable** - selective imports supported
|
|
347
|
+
|
|
348
|
+
## Complete API Reference
|
|
349
|
+
|
|
350
|
+
### Core Functions
|
|
351
|
+
```typescript
|
|
352
|
+
initExtensions(options?: ExtensionOptions): void
|
|
353
|
+
extendAll(): void
|
|
354
|
+
setPerformanceConfig(config: Partial<PerformanceConfig>): void
|
|
355
|
+
getPerformanceConfig(): PerformanceConfig
|
|
356
|
+
|
|
357
|
+
// Individual extension functions
|
|
358
|
+
extend.string(): void
|
|
359
|
+
extend.array(): void
|
|
360
|
+
extend.object(): void
|
|
361
|
+
extend.number(): void
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### String Methods (17 methods)
|
|
365
|
+
```typescript
|
|
366
|
+
toCapitalize(): string
|
|
367
|
+
toCamelCase(): string
|
|
368
|
+
toKebabCase(): string
|
|
369
|
+
toSnakeCase(): string
|
|
370
|
+
toTitleCase(): string
|
|
371
|
+
truncate(length: number, suffix?: string): string
|
|
372
|
+
isEmail(): boolean
|
|
373
|
+
isUrl(): boolean
|
|
374
|
+
isPalindrome(): boolean
|
|
375
|
+
removeWhitespace(): string
|
|
376
|
+
stripHtml(): string
|
|
377
|
+
reverse(): string
|
|
378
|
+
padStart(targetLength: number, padString?: string): string
|
|
379
|
+
padEnd(targetLength: number, padString?: string): string
|
|
380
|
+
count(substring: string): number
|
|
381
|
+
words(): string[]
|
|
382
|
+
lines(): string[]
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### Array Methods (18 methods)
|
|
386
|
+
```typescript
|
|
387
|
+
unique<T>(): T[]
|
|
388
|
+
shuffle<T>(): T[]
|
|
389
|
+
chunk<T>(size: number): T[][]
|
|
390
|
+
groupBy<T, K>(keyFn: (item: T) => K): Record<K, T[]>
|
|
391
|
+
sum(): number
|
|
392
|
+
average(): number
|
|
393
|
+
compact<T>(): T[]
|
|
394
|
+
pluck<T, K>(key: K): T[K][]
|
|
395
|
+
findLast<T>(predicate: (item: T) => boolean): T | undefined
|
|
396
|
+
partition<T>(predicate: (item: T) => boolean): [T[], T[]]
|
|
397
|
+
flatten(depth?: number): any[]
|
|
398
|
+
deepFlatten(): any[]
|
|
399
|
+
difference<T>(other: T[]): T[]
|
|
400
|
+
intersection<T>(other: T[]): T[]
|
|
401
|
+
union<T>(other: T[]): T[]
|
|
402
|
+
sample<T>(): T | undefined
|
|
403
|
+
take<T>(count: number): T[]
|
|
404
|
+
drop<T>(count: number): T[]
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### Object Methods (9 methods)
|
|
408
|
+
```typescript
|
|
409
|
+
isEmpty(): boolean
|
|
410
|
+
pick<T, K>(keys: K[]): Pick<T, K>
|
|
411
|
+
omit<T, K>(keys: K[]): Omit<T, K>
|
|
412
|
+
deepClone<T>(): T
|
|
413
|
+
deepFreeze<T>(): T
|
|
414
|
+
merge(other: Record<string, any>): Record<string, any>
|
|
415
|
+
hasPath(path: string): boolean
|
|
416
|
+
getPath(path: string, defaultValue?: any): any
|
|
417
|
+
setPath(path: string, value: any): any
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### Number Methods (16 methods)
|
|
421
|
+
```typescript
|
|
422
|
+
toPercent(decimals?: number): string
|
|
423
|
+
toCurrency(currency?: string, locale?: string): string
|
|
424
|
+
toOrdinal(): string
|
|
425
|
+
toRoman(): string
|
|
426
|
+
clamp(min: number, max: number): number
|
|
427
|
+
inRange(min: number, max: number): boolean
|
|
428
|
+
isEven(): boolean
|
|
429
|
+
isOdd(): boolean
|
|
430
|
+
isPrime(): boolean
|
|
431
|
+
factorial(): number
|
|
432
|
+
round(decimals?: number): number
|
|
433
|
+
ceil(decimals?: number): number
|
|
434
|
+
floor(decimals?: number): number
|
|
435
|
+
abs(): number
|
|
436
|
+
sign(): number
|
|
437
|
+
times(callback: (index: number) => void): void
|
|
89
438
|
```
|
|
@@ -62,4 +62,22 @@ function extendArray() {
|
|
|
62
62
|
Array.prototype.deepFlatten = function () {
|
|
63
63
|
return this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.deepFlatten() : val), []);
|
|
64
64
|
};
|
|
65
|
+
Array.prototype.difference = function (other) {
|
|
66
|
+
return this.filter(item => !other.includes(item));
|
|
67
|
+
};
|
|
68
|
+
Array.prototype.intersection = function (other) {
|
|
69
|
+
return this.filter(item => other.includes(item));
|
|
70
|
+
};
|
|
71
|
+
Array.prototype.union = function (other) {
|
|
72
|
+
return [...new Set([...this, ...other])];
|
|
73
|
+
};
|
|
74
|
+
Array.prototype.sample = function () {
|
|
75
|
+
return this.length > 0 ? this[Math.floor(Math.random() * this.length)] : undefined;
|
|
76
|
+
};
|
|
77
|
+
Array.prototype.take = function (count) {
|
|
78
|
+
return this.slice(0, Math.max(0, count));
|
|
79
|
+
};
|
|
80
|
+
Array.prototype.drop = function (count) {
|
|
81
|
+
return this.slice(Math.max(0, count));
|
|
82
|
+
};
|
|
65
83
|
}
|
|
@@ -2,12 +2,15 @@ import { extendString } from './string-extensions';
|
|
|
2
2
|
import { extendArray } from './array-extensions';
|
|
3
3
|
import { extendObject } from './object-extensions';
|
|
4
4
|
import { extendNumber } from './number-extensions';
|
|
5
|
+
import { setPerformanceConfig, getPerformanceConfig, type PerformanceConfig, withCache } from './performance';
|
|
5
6
|
import './types';
|
|
7
|
+
export { withCache };
|
|
6
8
|
export interface ExtensionOptions {
|
|
7
9
|
string?: boolean;
|
|
8
10
|
array?: boolean;
|
|
9
11
|
object?: boolean;
|
|
10
12
|
number?: boolean;
|
|
13
|
+
performance?: PerformanceConfig;
|
|
11
14
|
}
|
|
12
15
|
/**
|
|
13
16
|
* Initialize JavaScript prototype extensions
|
|
@@ -27,6 +30,7 @@ export declare const extend: {
|
|
|
27
30
|
object: typeof extendObject;
|
|
28
31
|
number: typeof extendNumber;
|
|
29
32
|
};
|
|
33
|
+
export { setPerformanceConfig, getPerformanceConfig, type PerformanceConfig };
|
|
30
34
|
declare const _default: {
|
|
31
35
|
initExtensions: typeof initExtensions;
|
|
32
36
|
extendAll: typeof extendAll;
|
|
@@ -36,5 +40,7 @@ declare const _default: {
|
|
|
36
40
|
object: typeof extendObject;
|
|
37
41
|
number: typeof extendNumber;
|
|
38
42
|
};
|
|
43
|
+
setPerformanceConfig: typeof setPerformanceConfig;
|
|
44
|
+
getPerformanceConfig: typeof getPerformanceConfig;
|
|
39
45
|
};
|
|
40
46
|
export default _default;
|
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extend = void 0;
|
|
3
|
+
exports.getPerformanceConfig = exports.setPerformanceConfig = exports.extend = exports.withCache = void 0;
|
|
4
4
|
exports.initExtensions = initExtensions;
|
|
5
5
|
exports.extendAll = extendAll;
|
|
6
6
|
const string_extensions_1 = require("./string-extensions");
|
|
7
7
|
const array_extensions_1 = require("./array-extensions");
|
|
8
8
|
const object_extensions_1 = require("./object-extensions");
|
|
9
9
|
const number_extensions_1 = require("./number-extensions");
|
|
10
|
+
const performance_1 = require("./performance");
|
|
11
|
+
Object.defineProperty(exports, "setPerformanceConfig", { enumerable: true, get: function () { return performance_1.setPerformanceConfig; } });
|
|
12
|
+
Object.defineProperty(exports, "getPerformanceConfig", { enumerable: true, get: function () { return performance_1.getPerformanceConfig; } });
|
|
13
|
+
Object.defineProperty(exports, "withCache", { enumerable: true, get: function () { return performance_1.withCache; } });
|
|
10
14
|
require("./types");
|
|
11
15
|
/**
|
|
12
16
|
* Initialize JavaScript prototype extensions
|
|
13
17
|
* @param options - Configure which extensions to enable (default: all enabled)
|
|
14
18
|
*/
|
|
15
19
|
function initExtensions(options = {}) {
|
|
16
|
-
const { string = true, array = true, object = true, number = true } = options;
|
|
20
|
+
const { string = true, array = true, object = true, number = true, performance } = options;
|
|
21
|
+
if (performance) {
|
|
22
|
+
(0, performance_1.setPerformanceConfig)(performance);
|
|
23
|
+
}
|
|
17
24
|
if (string)
|
|
18
25
|
(0, string_extensions_1.extendString)();
|
|
19
26
|
if (array)
|
|
@@ -41,5 +48,7 @@ exports.extend = {
|
|
|
41
48
|
exports.default = {
|
|
42
49
|
initExtensions,
|
|
43
50
|
extendAll,
|
|
44
|
-
extend: exports.extend
|
|
51
|
+
extend: exports.extend,
|
|
52
|
+
setPerformanceConfig: performance_1.setPerformanceConfig,
|
|
53
|
+
getPerformanceConfig: performance_1.getPerformanceConfig
|
|
45
54
|
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendNumber = extendNumber;
|
|
4
|
+
// Number prototype extensions
|
|
5
|
+
const performance_1 = require("./performance");
|
|
6
|
+
function extendNumber() {
|
|
7
|
+
Number.prototype.toPercent = function (decimals = 2) {
|
|
8
|
+
return (this.valueOf() * 100).toFixed(decimals) + '%';
|
|
9
|
+
};
|
|
10
|
+
Number.prototype.toCurrency = function (currency = 'USD', locale = 'en-US') {
|
|
11
|
+
return new Intl.NumberFormat(locale, {
|
|
12
|
+
style: 'currency',
|
|
13
|
+
currency: currency
|
|
14
|
+
}).format(this.valueOf());
|
|
15
|
+
};
|
|
16
|
+
Number.prototype.clamp = function (min, max) {
|
|
17
|
+
return Math.min(Math.max(this.valueOf(), min), max);
|
|
18
|
+
};
|
|
19
|
+
Number.prototype.isEven = function () {
|
|
20
|
+
return this.valueOf() % 2 === 0;
|
|
21
|
+
};
|
|
22
|
+
Number.prototype.isOdd = function () {
|
|
23
|
+
return this.valueOf() % 2 !== 0;
|
|
24
|
+
};
|
|
25
|
+
Number.prototype.isPrime = function () {
|
|
26
|
+
const num = this.valueOf();
|
|
27
|
+
return (0, performance_1.withCache)(`prime_${num}`, () => {
|
|
28
|
+
if (num < 2)
|
|
29
|
+
return false;
|
|
30
|
+
for (let i = 2; i <= Math.sqrt(num); i++) {
|
|
31
|
+
if (num % i === 0)
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
Number.prototype.factorial = function () {
|
|
38
|
+
const num = Math.floor(this.valueOf());
|
|
39
|
+
return (0, performance_1.withCache)(`factorial_${num}`, () => {
|
|
40
|
+
if (num < 0)
|
|
41
|
+
return NaN;
|
|
42
|
+
if (num === 0 || num === 1)
|
|
43
|
+
return 1;
|
|
44
|
+
let result = 1;
|
|
45
|
+
for (let i = 2; i <= num; i++) {
|
|
46
|
+
result *= i;
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
Number.prototype.toOrdinal = function () {
|
|
52
|
+
const num = Math.floor(this.valueOf());
|
|
53
|
+
const suffix = ['th', 'st', 'nd', 'rd'];
|
|
54
|
+
const v = num % 100;
|
|
55
|
+
return num + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]);
|
|
56
|
+
};
|
|
57
|
+
Number.prototype.toRoman = function () {
|
|
58
|
+
const num = Math.floor(this.valueOf());
|
|
59
|
+
return (0, performance_1.withCache)(`roman_${num}`, () => {
|
|
60
|
+
if (num <= 0 || num >= 4000)
|
|
61
|
+
return num.toString();
|
|
62
|
+
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
|
|
63
|
+
const symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
|
|
64
|
+
let result = '';
|
|
65
|
+
let n = num;
|
|
66
|
+
for (let i = 0; i < values.length; i++) {
|
|
67
|
+
while (n >= values[i]) {
|
|
68
|
+
result += symbols[i];
|
|
69
|
+
n -= values[i];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
Number.prototype.inRange = function (min, max) {
|
|
76
|
+
const num = this.valueOf();
|
|
77
|
+
return num >= min && num <= max;
|
|
78
|
+
};
|
|
79
|
+
Number.prototype.round = function (decimals = 0) {
|
|
80
|
+
const factor = Math.pow(10, decimals);
|
|
81
|
+
return Math.round(this.valueOf() * factor) / factor;
|
|
82
|
+
};
|
|
83
|
+
Number.prototype.ceil = function (decimals = 0) {
|
|
84
|
+
const factor = Math.pow(10, decimals);
|
|
85
|
+
return Math.ceil(this.valueOf() * factor) / factor;
|
|
86
|
+
};
|
|
87
|
+
Number.prototype.floor = function (decimals = 0) {
|
|
88
|
+
const factor = Math.pow(10, decimals);
|
|
89
|
+
return Math.floor(this.valueOf() * factor) / factor;
|
|
90
|
+
};
|
|
91
|
+
Number.prototype.abs = function () {
|
|
92
|
+
return Math.abs(this.valueOf());
|
|
93
|
+
};
|
|
94
|
+
Number.prototype.sign = function () {
|
|
95
|
+
return Math.sign(this.valueOf());
|
|
96
|
+
};
|
|
97
|
+
Number.prototype.times = function (callback) {
|
|
98
|
+
const num = Math.floor(this.valueOf());
|
|
99
|
+
for (let i = 0; i < num; i++) {
|
|
100
|
+
callback(i);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|