@naman_deep_singh/utils 2.3.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +228 -129
- package/dist/cjs/array/arrayExtensions.js +23 -23
- package/dist/cjs/core/index.js +14 -16
- package/dist/cjs/errors/CompressionError.js +19 -0
- package/dist/cjs/errors/ConnectionError.js +21 -0
- package/dist/cjs/errors/PoolError.js +20 -0
- package/dist/cjs/errors/TimeoutError.js +24 -0
- package/dist/cjs/errors/ValidationError.js +22 -0
- package/dist/cjs/errors/index.js +13 -0
- package/dist/cjs/extensions/index.js +9 -18
- package/dist/cjs/init/index.js +7 -16
- package/dist/cjs/init/initializer.js +10 -10
- package/dist/cjs/number/numberExtensions.js +23 -23
- package/dist/cjs/object/objectExtensions.js +14 -14
- package/dist/cjs/string/stringExtensions.js +22 -22
- package/dist/cjs/types/index.js +0 -17
- package/dist/cjs/utils/compression.js +455 -0
- package/dist/cjs/utils/index.js +35 -18
- package/dist/cjs/utils/pool.js +375 -0
- package/dist/cjs/utils/timeout.js +133 -0
- package/dist/esm/array/arrayExtensions.js +1 -1
- package/dist/esm/core/index.js +3 -2
- package/dist/esm/errors/CompressionError.js +15 -0
- package/dist/esm/errors/ConnectionError.js +17 -0
- package/dist/esm/errors/PoolError.js +16 -0
- package/dist/esm/errors/TimeoutError.js +20 -0
- package/dist/esm/errors/ValidationError.js +18 -0
- package/dist/esm/errors/index.js +5 -0
- package/dist/esm/extensions/index.js +4 -4
- package/dist/esm/init/index.js +2 -2
- package/dist/esm/init/initializer.js +5 -5
- package/dist/esm/number/numberExtensions.js +2 -2
- package/dist/esm/object/objectExtensions.js +1 -1
- package/dist/esm/string/stringExtensions.js +1 -1
- package/dist/esm/types/index.js +1 -3
- package/dist/esm/utils/compression.js +415 -0
- package/dist/esm/utils/index.js +16 -4
- package/dist/esm/utils/pool.js +370 -0
- package/dist/esm/utils/timeout.js +128 -0
- package/dist/types/core/index.d.ts +3 -2
- package/dist/types/errors/CompressionError.d.ts +8 -0
- package/dist/types/errors/ConnectionError.d.ts +9 -0
- package/dist/types/errors/PoolError.d.ts +8 -0
- package/dist/types/errors/TimeoutError.d.ts +12 -0
- package/dist/types/errors/ValidationError.d.ts +9 -0
- package/dist/types/errors/index.d.ts +5 -0
- package/dist/types/extensions/index.d.ts +4 -4
- package/dist/types/init/index.d.ts +2 -2
- package/dist/types/init/initializer.d.ts +1 -1
- package/dist/types/init/options.d.ts +1 -1
- package/dist/types/types/extensionTypes.d.ts +1 -1
- package/dist/types/types/index.d.ts +1 -2
- package/dist/types/utils/compression.d.ts +164 -0
- package/dist/types/utils/config.d.ts +1 -1
- package/dist/types/utils/index.d.ts +11 -3
- package/dist/types/utils/pool.d.ts +157 -0
- package/dist/types/utils/timeout.d.ts +64 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
|
+
```bash
|
|
1
2
|
@naman_deep_singh/utils
|
|
2
3
|
|
|
3
|
-
Version: 2.
|
|
4
|
+
**Version: 2.6.0**
|
|
4
5
|
|
|
5
|
-
Universal JavaScript prototype extensions for common development utilities. Works in both Node.js and browser environments with 70+ utility methods.
|
|
6
|
+
Universal JavaScript prototype extensions for common development utilities. Works in both Node.js and browser environments with 70+ utility methods, plus timeout management, connection pooling, and compression utilities.
|
|
6
7
|
|
|
7
|
-
⚠️ This library extends native prototypes (String, Array, Object, Number). Use consciously in shared or library code.
|
|
8
|
+
⚠️ Note: This library extends native prototypes (String, Array, Object, Number). Use consciously in shared or library code.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
What's New in v2.6.0
|
|
11
|
+
✨ New Utility Modules
|
|
12
|
+
Timeout Management: Async operations with timeout support, retry logic, and delays
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
Connection Pooling: Generic connection pooling with health checks and statistics
|
|
15
|
+
|
|
16
|
+
Compression Utilities: GZIP, Deflate, and Brotli compression with streaming support
|
|
17
|
+
|
|
18
|
+
Error Integration: Proper integration with @naman_deep_singh/errors package
|
|
19
|
+
|
|
20
|
+
Installation
|
|
21
|
+
bash
|
|
13
22
|
npm install @naman_deep_singh/utils
|
|
14
23
|
# or
|
|
15
24
|
pnpm add @naman_deep_singh/utils
|
|
16
25
|
Quick Start
|
|
26
|
+
1. Prototype Extensions
|
|
27
|
+
typescript
|
|
17
28
|
import { initializeExtensions } from '@naman_deep_singh/utils';
|
|
18
29
|
|
|
19
30
|
// Initialize all extensions
|
|
@@ -22,60 +33,142 @@ initializeExtensions();
|
|
|
22
33
|
// String utilities
|
|
23
34
|
"hello world".toCapitalize(); // "Hello world"
|
|
24
35
|
"hello world".capitalizeWords(); // "Hello World"
|
|
25
|
-
"hello world".reverseWords(); // "world hello"
|
|
26
|
-
"test@email.com".isEmail(); // true
|
|
27
|
-
"hello world".words(); // ["hello", "world"]
|
|
28
36
|
|
|
29
|
-
// Array utilities
|
|
37
|
+
// Array utilities
|
|
30
38
|
[1, 2, 2, 3].unique(); // [1, 2, 3]
|
|
31
39
|
[1, 2, 3, 4, 5].chunk(2); // [[1,2],[3,4],[5]]
|
|
32
|
-
[1, 2, 3].sample(); // random element
|
|
33
|
-
[1,2,3,4].last(); // 4
|
|
34
|
-
[{id:1},{id:2},{id:1}].uniqueBy(x => x.id); // [{id:1},{id:2}]
|
|
35
|
-
[3,1,2].sortBy(x => x); // [1,2,3]
|
|
36
40
|
|
|
37
41
|
// Number utilities
|
|
38
|
-
(42).toOrdinal();
|
|
39
|
-
(0.75).toPercent();
|
|
40
|
-
(3.14159).toFixedNumber(2); // 3.14
|
|
41
|
-
(5).randomUpTo(); // 0..5 random number
|
|
42
|
-
(5).times(i => console.log(i)); // 0..4
|
|
42
|
+
(42).toOrdinal(); // "42nd"
|
|
43
|
+
(0.75).toPercent(); // "75.00%"
|
|
43
44
|
|
|
44
45
|
// Object utilities
|
|
45
|
-
({ a: 1, b: 2 }).pick(['a']);
|
|
46
|
-
({
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
({ a: 1, b: 2 }).pick(['a']); // { a: 1 }
|
|
47
|
+
({}).isEmpty(); // true
|
|
48
|
+
2. Timeout Utilities
|
|
49
|
+
typescript
|
|
50
|
+
import { TimeoutManager } from '@naman_deep_singh/utils';
|
|
51
|
+
|
|
52
|
+
// Execute promise with timeout
|
|
53
|
+
const result = await TimeoutManager.withTimeout(
|
|
54
|
+
fetchData(),
|
|
55
|
+
5000,
|
|
56
|
+
'Fetch operation timed out'
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Create delay
|
|
60
|
+
await TimeoutManager.delay(1000);
|
|
61
|
+
|
|
62
|
+
// Retry with timeout
|
|
63
|
+
const data = await TimeoutManager.retryWithTimeout(fetchData, {
|
|
64
|
+
maxAttempts: 3,
|
|
65
|
+
timeoutPerAttempt: 3000,
|
|
66
|
+
backoffMultiplier: 2
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Decorator for class methods
|
|
70
|
+
class ApiService {
|
|
71
|
+
@withTimeout(5000)
|
|
72
|
+
async fetchUser(id: string) {
|
|
73
|
+
// Method automatically times out after 5s
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
3. Connection Pooling
|
|
77
|
+
typescript
|
|
78
|
+
import { GenericPool, PoolManager } from '@naman_deep_singh/utils';
|
|
79
|
+
|
|
80
|
+
// Create a database connection pool
|
|
81
|
+
const dbPool = new GenericPool({
|
|
82
|
+
name: 'database',
|
|
83
|
+
minConnections: 2,
|
|
84
|
+
maxConnections: 10,
|
|
85
|
+
createConnection: async () => {
|
|
86
|
+
return await createDatabaseConnection();
|
|
87
|
+
},
|
|
88
|
+
validateConnection: (conn) => conn.isHealthy()
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Acquire and use connection
|
|
92
|
+
const connection = await dbPool.acquire();
|
|
93
|
+
try {
|
|
94
|
+
await connection.query('SELECT * FROM users');
|
|
95
|
+
} finally {
|
|
96
|
+
await dbPool.release(connection);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Or use helper method
|
|
100
|
+
const users = await dbPool.withConnection(async (conn) => {
|
|
101
|
+
return await conn.query('SELECT * FROM users');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Pool statistics
|
|
105
|
+
console.log(dbPool.getStats());
|
|
106
|
+
4. Compression Utilities
|
|
107
|
+
typescript
|
|
108
|
+
import { Compression, CompressionAlgorithm } from '@naman_deep_singh/utils';
|
|
109
|
+
|
|
110
|
+
// Basic compression
|
|
111
|
+
const compressed = await Compression.compress('Hello World', {
|
|
112
|
+
algorithm: CompressionAlgorithm.GZIP,
|
|
113
|
+
level: 6
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const decompressed = await Compression.decompress(compressed);
|
|
117
|
+
|
|
118
|
+
// Compression with metrics
|
|
119
|
+
const result = await Compression.compressWithMetrics(largeData, {
|
|
120
|
+
algorithm: CompressionAlgorithm.BROTLI,
|
|
121
|
+
level: 9
|
|
122
|
+
});
|
|
123
|
+
console.log(result.compressionRatio); // 0.45 (55% reduction)
|
|
124
|
+
|
|
125
|
+
// Streaming compression
|
|
126
|
+
await Compression.compressStream(readableStream, writableStream);
|
|
127
|
+
|
|
128
|
+
// Check supported algorithms
|
|
129
|
+
const supported = Compression.getSupportedAlgorithms();
|
|
130
|
+
// ['gzip', 'deflate', 'brotli'] (if Node.js 10.16.0+)
|
|
131
|
+
5. Error Handling with Custom Errors
|
|
132
|
+
typescript
|
|
133
|
+
import {
|
|
134
|
+
TimeoutError,
|
|
135
|
+
PoolError,
|
|
136
|
+
CompressionError
|
|
137
|
+
} from '@naman_deep_singh/utils';
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
await TimeoutManager.withTimeout(slowOperation(), 1000);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error instanceof TimeoutError) {
|
|
143
|
+
console.log(`Timeout after ${error.timeoutMs}ms`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
52
146
|
Error Handling & Validation
|
|
53
|
-
|
|
147
|
+
Prototype Methods
|
|
148
|
+
All prototype extension methods include strict runtime validation and throw clear native errors.
|
|
54
149
|
|
|
150
|
+
typescript
|
|
55
151
|
"hello".count(123);
|
|
56
152
|
// TypeError: count: substring must be a string, got number
|
|
57
153
|
|
|
58
|
-
"hello".count("");
|
|
59
|
-
// TypeError: count: substring cannot be empty
|
|
60
|
-
|
|
61
154
|
[].sum();
|
|
62
155
|
// TypeError: sum: array must contain at least one number
|
|
63
156
|
|
|
64
|
-
({ a: 1 }).pick(null as any);
|
|
65
|
-
// TypeError: pick: keys must be an array, got object
|
|
66
|
-
|
|
67
157
|
(3.14).round(-1);
|
|
68
158
|
// TypeError: round: decimals must be a non-negative integer, got -1
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
159
|
+
Utility Errors
|
|
160
|
+
New utilities throw specific error classes from @naman_deep_singh/errors:
|
|
161
|
+
|
|
162
|
+
typescript
|
|
163
|
+
import {
|
|
164
|
+
TimeoutError, // Operation timeout (408)
|
|
165
|
+
PoolError, // Pool exhaustion (503)
|
|
166
|
+
ConnectionError, // Connection failure (502)
|
|
167
|
+
CompressionError // Compression failure (500)
|
|
168
|
+
} from '@naman_deep_singh/utils';
|
|
77
169
|
Configuration
|
|
78
170
|
Selective Extension Loading
|
|
171
|
+
typescript
|
|
79
172
|
import { initializeExtensions, extend } from '@naman_deep_singh/utils';
|
|
80
173
|
|
|
81
174
|
// Enable only specific prototypes
|
|
@@ -90,7 +183,7 @@ initializeExtensions({
|
|
|
90
183
|
extend.string();
|
|
91
184
|
extend.array();
|
|
92
185
|
Performance Configuration
|
|
93
|
-
|
|
186
|
+
typescript
|
|
94
187
|
import { setPerformanceConfig } from '@naman_deep_singh/utils';
|
|
95
188
|
|
|
96
189
|
setPerformanceConfig({
|
|
@@ -101,90 +194,64 @@ Notes:
|
|
|
101
194
|
|
|
102
195
|
Validation cannot be disabled
|
|
103
196
|
|
|
104
|
-
Caching is optional
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
197
|
+
Caching is optional (LRU cache)
|
|
198
|
+
|
|
199
|
+
Pool utilities have built-in health checks
|
|
200
|
+
|
|
201
|
+
API Reference
|
|
202
|
+
Timeout Utilities
|
|
203
|
+
typescript
|
|
204
|
+
TimeoutManager.withTimeout(promise, timeoutMs, errorMessage?);
|
|
205
|
+
TimeoutManager.delay(delayMs);
|
|
206
|
+
TimeoutManager.createTimeoutSignal(timeoutMs);
|
|
207
|
+
TimeoutManager.retryWithTimeout(fn, options);
|
|
208
|
+
TimeoutManager.raceWithTimeout(promises, timeoutMs);
|
|
209
|
+
Connection Pooling
|
|
210
|
+
typescript
|
|
211
|
+
GenericPool<T extends Connection> // Generic connection pool
|
|
212
|
+
PoolManager // Multi-pool manager
|
|
213
|
+
interface PoolConfig // Pool configuration
|
|
214
|
+
interface Connection // Connection interface
|
|
215
|
+
Compression Utilities
|
|
216
|
+
typescript
|
|
217
|
+
Compression.compress(data, options)
|
|
218
|
+
Compression.decompress(data, options)
|
|
219
|
+
Compression.compressWithMetrics(data, options)
|
|
220
|
+
Compression.createCompressionStream(algorithm, options)
|
|
221
|
+
Compression.createDecompressionStream(algorithm)
|
|
222
|
+
Compression.isAlgorithmSupported(algorithm)
|
|
223
|
+
Compression.getSupportedAlgorithms()
|
|
224
|
+
Prototype Extensions (Summary)
|
|
108
225
|
String Extensions (21 methods)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
Validation & Checks
|
|
118
|
-
|
|
119
|
-
"test@example.com".isEmail(); // true
|
|
120
|
-
"https://example.com".isUrl(); // true
|
|
121
|
-
"racecar".isPalindrome(); // true
|
|
122
|
-
Text Utilities
|
|
123
|
-
|
|
124
|
-
"Long text here".truncate(8); // "Long tex..."
|
|
125
|
-
"hello world".truncateWords(1); // "hello..."
|
|
126
|
-
"hello world".removeWhitespace(); // "helloworld"
|
|
127
|
-
"<p>Hello</p>".stripHtml(); // "Hello"
|
|
128
|
-
"hello".reverse(); // "olleh"
|
|
129
|
-
"hello world".reverseWords(); // "world hello"
|
|
130
|
-
"hello hello".count("hello"); // 2
|
|
131
|
-
"Hello World".slugify(); // "hello-world"
|
|
132
|
-
"hello world".words(); // ["hello", "world"]
|
|
133
|
-
"hello\nworld".lines(); // ["hello","world"]
|
|
226
|
+
typescript
|
|
227
|
+
.toCapitalize() .capitalizeWords() .toCamelCase()
|
|
228
|
+
.toKebabCase() .toSnakeCase() .toTitleCase()
|
|
229
|
+
.isEmail() .isUrl() .isPalindrome()
|
|
230
|
+
.truncate() .truncateWords() .removeWhitespace()
|
|
231
|
+
.stripHtml() .reverse() .reverseWords()
|
|
232
|
+
.count() .slugify() .words()
|
|
233
|
+
.lines()
|
|
134
234
|
Array Extensions (23 methods)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
[0,1,false,2,"",3].compact(); // [1,2,3] removes null, undefined, false, and empty strings
|
|
142
|
-
[1,2,3,4].last(); // 4
|
|
143
|
-
[3,1,2].sortBy(x => x); // [1,2,3]
|
|
144
|
-
Math
|
|
145
|
-
|
|
146
|
-
[1,2,3].sum(); // 6
|
|
147
|
-
[1,2,3].average(); // 2
|
|
148
|
-
Advanced
|
|
149
|
-
|
|
150
|
-
users.groupBy(u => u.age);
|
|
151
|
-
users.pluck('name');
|
|
152
|
-
[1,2,3,4].partition(n => n % 2 === 0);
|
|
153
|
-
// [[2,4],[1,3]]
|
|
154
|
-
Set Operations
|
|
155
|
-
|
|
156
|
-
[1,2,3].difference([2]); // [1,3]
|
|
157
|
-
[1,2,3].intersection([2,3]); // [2,3]
|
|
158
|
-
[1,2,3].union([3,4]); // [1,2,3,4]
|
|
159
|
-
Object Extensions (13 methods)
|
|
160
|
-
({}).isEmpty(); // true
|
|
161
|
-
({ a: 1, b: 2 }).pick(['a']); // { a: 1 }
|
|
162
|
-
({ a: 1, b: 2 }).omit(['b']); // { a: 1 }
|
|
163
|
-
({ a: 1, b: 2 }).mapValues(v => v*2); // { a: 2, b: 4 }
|
|
164
|
-
({ a: 1, b: 2 }).mapKeys(k => k+"_"); // { a_:1, b_:2 }
|
|
165
|
-
({ a: 1, b: 2 }).filterKeys(k => k==='a'); // { a:1 }
|
|
166
|
-
({ a: 1, b: 2 }).filterValues(v => v===1); // { a:1 }
|
|
167
|
-
({ a: { b: 2 } }).deepClone();
|
|
168
|
-
({ a: { b: 2 } }).deepFreeze();
|
|
169
|
-
data.hasPath('user.profile.name');
|
|
170
|
-
data.getPath('user.profile.email', 'N/A');
|
|
171
|
-
data.setPath('user.profile.email', 'john@example.com');
|
|
235
|
+
typescript
|
|
236
|
+
.unique() .uniqueBy() .shuffle()
|
|
237
|
+
.chunk() .compact() .last()
|
|
238
|
+
.sortBy() .sum() .average()
|
|
239
|
+
.groupBy() .pluck() .partition()
|
|
240
|
+
.difference() .intersection() .union()
|
|
172
241
|
Number Extensions (19 methods)
|
|
173
|
-
|
|
174
|
-
(
|
|
175
|
-
(
|
|
176
|
-
(
|
|
177
|
-
(
|
|
178
|
-
(
|
|
179
|
-
|
|
180
|
-
(
|
|
181
|
-
(
|
|
182
|
-
(
|
|
183
|
-
(
|
|
184
|
-
(123).sign(); // 1
|
|
242
|
+
typescript
|
|
243
|
+
.toPercent() .toFixedNumber() .toCurrency()
|
|
244
|
+
.clamp() .inRange() .isPrime()
|
|
245
|
+
.factorial() .randomUpTo() .times()
|
|
246
|
+
.toOrdinal() .sign()
|
|
247
|
+
Object Extensions (13 methods)
|
|
248
|
+
typescript
|
|
249
|
+
.isEmpty() .pick() .omit()
|
|
250
|
+
.mapValues() .mapKeys() .filterKeys()
|
|
251
|
+
.filterValues() .deepClone() .deepFreeze()
|
|
252
|
+
.hasPath() .getPath() .setPath()
|
|
185
253
|
Caching Details
|
|
186
254
|
Cached Methods (when enabled):
|
|
187
|
-
|
|
188
255
|
Number.isPrime()
|
|
189
256
|
|
|
190
257
|
Number.factorial()
|
|
@@ -192,27 +259,59 @@ Number.factorial()
|
|
|
192
259
|
Number.toRoman()
|
|
193
260
|
|
|
194
261
|
Custom Caching:
|
|
195
|
-
|
|
262
|
+
typescript
|
|
196
263
|
import { withCache } from '@naman_deep_singh/utils';
|
|
197
264
|
const result = withCache('key', () => expensiveFn());
|
|
265
|
+
Pool Health Checks:
|
|
266
|
+
Automatic idle connection cleanup
|
|
267
|
+
|
|
268
|
+
Maximum lifetime enforcement
|
|
269
|
+
|
|
270
|
+
Periodic health validation
|
|
271
|
+
|
|
272
|
+
Statistics tracking
|
|
273
|
+
|
|
198
274
|
TypeScript Support
|
|
199
|
-
Full
|
|
275
|
+
Full TypeScript support with:
|
|
276
|
+
|
|
277
|
+
Complete global augmentation
|
|
200
278
|
|
|
201
279
|
Strict typing for all methods
|
|
202
280
|
|
|
203
281
|
Zero any leakage in public APIs
|
|
204
282
|
|
|
205
|
-
|
|
283
|
+
Proper type imports/exports
|
|
284
|
+
|
|
285
|
+
typescript
|
|
286
|
+
const x: number[] = [1, 2, 2].unique();
|
|
206
287
|
const y: string = "hello".toCapitalize();
|
|
288
|
+
const z: CompressionResult = await compressWithMetrics(data);
|
|
207
289
|
Package Stats
|
|
208
|
-
|
|
290
|
+
70+ prototype utility methods
|
|
291
|
+
|
|
292
|
+
Timeout management utilities
|
|
293
|
+
|
|
294
|
+
Generic connection pooling
|
|
295
|
+
|
|
296
|
+
Multi-algorithm compression
|
|
209
297
|
|
|
210
|
-
Zero dependencies
|
|
298
|
+
Zero production dependencies (except peer deps)
|
|
211
299
|
|
|
212
|
-
Node.js + Browser
|
|
300
|
+
Node.js + Browser compatibility
|
|
213
301
|
|
|
214
|
-
TypeScript-first
|
|
302
|
+
TypeScript-first design
|
|
215
303
|
|
|
216
304
|
Optional LRU caching
|
|
217
305
|
|
|
218
|
-
Strict runtime validation
|
|
306
|
+
Strict runtime validation
|
|
307
|
+
|
|
308
|
+
Dependencies
|
|
309
|
+
json
|
|
310
|
+
{
|
|
311
|
+
"peerDependencies": {
|
|
312
|
+
"@naman_deep_singh/errors": "^2.3.0"
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
License
|
|
316
|
+
ISC © Naman Deep Singh
|
|
317
|
+
```
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extendArray = extendArray;
|
|
4
|
-
const
|
|
4
|
+
const defineExtension_js_1 = require("../utils/defineExtension.js");
|
|
5
5
|
let arrayExtended = false;
|
|
6
6
|
function extendArray() {
|
|
7
7
|
if (arrayExtended)
|
|
8
8
|
return;
|
|
9
9
|
arrayExtended = true;
|
|
10
|
-
(0,
|
|
10
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'unique', function () {
|
|
11
11
|
return [...new Set(this)];
|
|
12
12
|
});
|
|
13
|
-
(0,
|
|
13
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'shuffle', function () {
|
|
14
14
|
const arr = [...this];
|
|
15
15
|
for (let i = arr.length - 1; i > 0; i--) {
|
|
16
16
|
const j = Math.floor(Math.random() * (i + 1));
|
|
@@ -18,7 +18,7 @@ function extendArray() {
|
|
|
18
18
|
}
|
|
19
19
|
return arr;
|
|
20
20
|
});
|
|
21
|
-
(0,
|
|
21
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'chunk', function (size) {
|
|
22
22
|
if (!Number.isInteger(size) || size <= 0) {
|
|
23
23
|
throw new TypeError(`chunk: size must be a positive integer, got ${size}`);
|
|
24
24
|
}
|
|
@@ -28,7 +28,7 @@ function extendArray() {
|
|
|
28
28
|
}
|
|
29
29
|
return chunks;
|
|
30
30
|
});
|
|
31
|
-
(0,
|
|
31
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'groupBy', function (keyFn) {
|
|
32
32
|
if (typeof keyFn !== 'function') {
|
|
33
33
|
throw new TypeError(`groupBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
34
34
|
}
|
|
@@ -40,28 +40,28 @@ function extendArray() {
|
|
|
40
40
|
return groups;
|
|
41
41
|
}, {});
|
|
42
42
|
});
|
|
43
|
-
(0,
|
|
43
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'sum', function () {
|
|
44
44
|
if (this.length === 0) {
|
|
45
45
|
throw new TypeError('sum: array must contain at least one number');
|
|
46
46
|
}
|
|
47
47
|
return this.reduce((sum, num) => sum + num, 0);
|
|
48
48
|
});
|
|
49
|
-
(0,
|
|
49
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'average', function () {
|
|
50
50
|
if (this.length === 0) {
|
|
51
51
|
throw new TypeError('average: array must contain at least one number');
|
|
52
52
|
}
|
|
53
53
|
return this.reduce((sum, num) => sum + num, 0) / this.length;
|
|
54
54
|
});
|
|
55
|
-
(0,
|
|
55
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'compact', function () {
|
|
56
56
|
return this.filter((item) => item !== null && item !== undefined && item !== '');
|
|
57
57
|
});
|
|
58
|
-
(0,
|
|
58
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'compactTruthy', function () {
|
|
59
59
|
return this.filter(Boolean);
|
|
60
60
|
});
|
|
61
|
-
(0,
|
|
61
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'pluck', function (key) {
|
|
62
62
|
return this.map((item) => item[key]).filter((val) => val !== undefined);
|
|
63
63
|
});
|
|
64
|
-
(0,
|
|
64
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'findLast', function (predicate) {
|
|
65
65
|
if (typeof predicate !== 'function') {
|
|
66
66
|
throw new TypeError(`findLast: predicate must be a function, got ${typeof predicate}`);
|
|
67
67
|
}
|
|
@@ -71,7 +71,7 @@ function extendArray() {
|
|
|
71
71
|
}
|
|
72
72
|
return undefined;
|
|
73
73
|
});
|
|
74
|
-
(0,
|
|
74
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'partition', function (predicate) {
|
|
75
75
|
if (typeof predicate !== 'function') {
|
|
76
76
|
throw new TypeError(`partition: predicate must be a function, got ${typeof predicate}`);
|
|
77
77
|
}
|
|
@@ -80,44 +80,44 @@ function extendArray() {
|
|
|
80
80
|
this.forEach((item) => predicate(item) ? truthy.push(item) : falsy.push(item));
|
|
81
81
|
return [truthy, falsy];
|
|
82
82
|
});
|
|
83
|
-
(0,
|
|
83
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'flatten', function (depth = 1) {
|
|
84
84
|
return depth > 0
|
|
85
85
|
? this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.flatten(depth - 1) : val), [])
|
|
86
86
|
: this.slice();
|
|
87
87
|
});
|
|
88
|
-
(0,
|
|
88
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'deepFlatten', function () {
|
|
89
89
|
return this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.deepFlatten() : val), []);
|
|
90
90
|
});
|
|
91
|
-
(0,
|
|
91
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'difference', function (other) {
|
|
92
92
|
if (!Array.isArray(other)) {
|
|
93
93
|
throw new TypeError(`difference: other must be an array, got ${typeof other}`);
|
|
94
94
|
}
|
|
95
95
|
return this.filter((item) => !other.includes(item));
|
|
96
96
|
});
|
|
97
|
-
(0,
|
|
97
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'intersection', function (other) {
|
|
98
98
|
if (!Array.isArray(other)) {
|
|
99
99
|
throw new TypeError(`intersection: other must be an array, got ${typeof other}`);
|
|
100
100
|
}
|
|
101
101
|
return this.filter((item) => other.includes(item));
|
|
102
102
|
});
|
|
103
|
-
(0,
|
|
103
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'union', function (other) {
|
|
104
104
|
if (!Array.isArray(other)) {
|
|
105
105
|
throw new TypeError(`union: other must be an array, got ${typeof other}`);
|
|
106
106
|
}
|
|
107
107
|
return [...new Set([...this, ...other])];
|
|
108
108
|
});
|
|
109
|
-
(0,
|
|
109
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'sample', function () {
|
|
110
110
|
return this.length > 0
|
|
111
111
|
? this[Math.floor(Math.random() * this.length)]
|
|
112
112
|
: undefined;
|
|
113
113
|
});
|
|
114
|
-
(0,
|
|
114
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'take', function (count) {
|
|
115
115
|
return this.slice(0, Math.max(0, count));
|
|
116
116
|
});
|
|
117
|
-
(0,
|
|
117
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'drop', function (count) {
|
|
118
118
|
return this.slice(Math.max(0, count));
|
|
119
119
|
});
|
|
120
|
-
(0,
|
|
120
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'uniqueBy', function (keyFn) {
|
|
121
121
|
if (typeof keyFn !== 'function') {
|
|
122
122
|
throw new TypeError(`uniqueBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
123
123
|
}
|
|
@@ -132,7 +132,7 @@ function extendArray() {
|
|
|
132
132
|
}
|
|
133
133
|
return result;
|
|
134
134
|
});
|
|
135
|
-
(0,
|
|
135
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'sortBy', function (keyFn) {
|
|
136
136
|
if (typeof keyFn !== 'function') {
|
|
137
137
|
throw new TypeError(`sortBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
138
138
|
}
|
|
@@ -146,7 +146,7 @@ function extendArray() {
|
|
|
146
146
|
return 0;
|
|
147
147
|
});
|
|
148
148
|
});
|
|
149
|
-
(0,
|
|
149
|
+
(0, defineExtension_js_1.defineExtension)(Array.prototype, 'last', function () {
|
|
150
150
|
return this.length > 0 ? this[this.length - 1] : undefined;
|
|
151
151
|
});
|
|
152
152
|
}
|
package/dist/cjs/core/index.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPackageVersion = exports.withCache = exports.makeInternalCacheKey = exports.LRUCache = exports.getPerformanceConfig = exports.setPerformanceConfig = exports.validatePositiveInteger = exports.validateNumberRange = exports.validateArrayInput = exports.validateExtensionInput = void 0;
|
|
17
4
|
// Re-export core utilities
|
|
18
|
-
|
|
19
|
-
|
|
5
|
+
var validation_js_1 = require("./validation.js");
|
|
6
|
+
Object.defineProperty(exports, "validateExtensionInput", { enumerable: true, get: function () { return validation_js_1.validateExtensionInput; } });
|
|
7
|
+
Object.defineProperty(exports, "validateArrayInput", { enumerable: true, get: function () { return validation_js_1.validateArrayInput; } });
|
|
8
|
+
Object.defineProperty(exports, "validateNumberRange", { enumerable: true, get: function () { return validation_js_1.validateNumberRange; } });
|
|
9
|
+
Object.defineProperty(exports, "validatePositiveInteger", { enumerable: true, get: function () { return validation_js_1.validatePositiveInteger; } });
|
|
10
|
+
var performance_js_1 = require("./performance.js");
|
|
11
|
+
Object.defineProperty(exports, "setPerformanceConfig", { enumerable: true, get: function () { return performance_js_1.setPerformanceConfig; } });
|
|
12
|
+
Object.defineProperty(exports, "getPerformanceConfig", { enumerable: true, get: function () { return performance_js_1.getPerformanceConfig; } });
|
|
13
|
+
Object.defineProperty(exports, "LRUCache", { enumerable: true, get: function () { return performance_js_1.LRUCache; } });
|
|
14
|
+
Object.defineProperty(exports, "makeInternalCacheKey", { enumerable: true, get: function () { return performance_js_1.makeInternalCacheKey; } });
|
|
15
|
+
Object.defineProperty(exports, "withCache", { enumerable: true, get: function () { return performance_js_1.withCache; } });
|
|
16
|
+
var version_js_1 = require("./version.js");
|
|
17
|
+
Object.defineProperty(exports, "getPackageVersion", { enumerable: true, get: function () { return version_js_1.getPackageVersion; } });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompressionError = void 0;
|
|
4
|
+
const errors_1 = require("@naman_deep_singh/errors");
|
|
5
|
+
/**
|
|
6
|
+
* Compression error - when compression/decompression fails
|
|
7
|
+
*/
|
|
8
|
+
class CompressionError extends errors_1.AppError {
|
|
9
|
+
constructor(message, algorithm, details, cause) {
|
|
10
|
+
super(errors_1.ERROR_CODES.FILE_ERROR, 500, {
|
|
11
|
+
message,
|
|
12
|
+
algorithm,
|
|
13
|
+
...(details ? { details } : {}),
|
|
14
|
+
}, cause);
|
|
15
|
+
this.algorithm = algorithm;
|
|
16
|
+
this.name = 'CompressionError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.CompressionError = CompressionError;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Connection error - when connection fails
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.ConnectionError = void 0;
|
|
8
|
+
const errors_1 = require("@naman_deep_singh/errors");
|
|
9
|
+
class ConnectionError extends errors_1.AppError {
|
|
10
|
+
constructor(message, endpoint, details, cause) {
|
|
11
|
+
super(errors_1.ERROR_CODES.DEPENDENCY_FAILURE, 502, // 502 = Bad Gateway
|
|
12
|
+
{
|
|
13
|
+
message,
|
|
14
|
+
endpoint,
|
|
15
|
+
...(details ? { details } : {}),
|
|
16
|
+
}, cause);
|
|
17
|
+
this.endpoint = endpoint;
|
|
18
|
+
this.name = 'ConnectionError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.ConnectionError = ConnectionError;
|