@naman_deep_singh/utils 2.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 +218 -0
- package/dist/cjs/array/array-extensions.d.ts +1 -0
- package/dist/cjs/array/array-extensions.js +152 -0
- package/dist/cjs/array/index.d.ts +1 -0
- package/dist/cjs/array/index.js +5 -0
- package/dist/cjs/core/index.d.ts +2 -0
- package/dist/cjs/core/index.js +19 -0
- package/dist/cjs/core/performance.d.ts +17 -0
- package/dist/cjs/core/performance.js +82 -0
- package/dist/cjs/core/validation.d.ts +4 -0
- package/dist/cjs/core/validation.js +31 -0
- package/dist/cjs/core/version.d.ts +1 -0
- package/dist/cjs/core/version.js +9 -0
- package/dist/cjs/extensions/index.d.ts +4 -0
- package/dist/cjs/extensions/index.js +21 -0
- package/dist/cjs/index.d.ts +61 -0
- package/dist/cjs/index.js +77 -0
- package/dist/cjs/init/index.d.ts +2 -0
- package/dist/cjs/init/index.js +19 -0
- package/dist/cjs/init/initializer.d.ts +3 -0
- package/dist/cjs/init/initializer.js +28 -0
- package/dist/cjs/init/options.d.ts +3 -0
- package/dist/cjs/init/options.js +23 -0
- package/dist/cjs/number/index.d.ts +1 -0
- package/dist/cjs/number/index.js +5 -0
- package/dist/cjs/number/number-extensions.d.ts +1 -0
- package/dist/cjs/number/number-extensions.js +142 -0
- package/dist/cjs/object/index.d.ts +1 -0
- package/dist/cjs/object/index.js +5 -0
- package/dist/cjs/object/object-extensions.d.ts +1 -0
- package/dist/cjs/object/object-extensions.js +148 -0
- package/dist/cjs/string/index.d.ts +1 -0
- package/dist/cjs/string/index.js +5 -0
- package/dist/cjs/string/string-extensions.d.ts +1 -0
- package/dist/cjs/string/string-extensions.js +127 -0
- package/dist/cjs/types/extension-types.d.ts +9 -0
- package/dist/cjs/types/extension-types.js +2 -0
- package/dist/cjs/types/global-augmentations.d.ts +72 -0
- package/dist/cjs/types/global-augmentations.js +2 -0
- package/dist/cjs/types/index.d.ts +2 -0
- package/dist/cjs/types/index.js +19 -0
- package/dist/cjs/utils/config.d.ts +4 -0
- package/dist/cjs/utils/config.js +25 -0
- package/dist/cjs/utils/defineExtension.d.ts +1 -0
- package/dist/cjs/utils/defineExtension.js +13 -0
- package/dist/cjs/utils/helpers.d.ts +5 -0
- package/dist/cjs/utils/helpers.js +37 -0
- package/dist/cjs/utils/index.d.ts +3 -0
- package/dist/cjs/utils/index.js +20 -0
- package/dist/esm/array/array-extensions.d.ts +1 -0
- package/dist/esm/array/array-extensions.js +149 -0
- package/dist/esm/array/index.d.ts +1 -0
- package/dist/esm/array/index.js +1 -0
- package/dist/esm/core/index.d.ts +2 -0
- package/dist/esm/core/index.js +3 -0
- package/dist/esm/core/performance.d.ts +17 -0
- package/dist/esm/core/performance.js +74 -0
- package/dist/esm/core/validation.d.ts +4 -0
- package/dist/esm/core/validation.js +25 -0
- package/dist/esm/core/version.d.ts +1 -0
- package/dist/esm/core/version.js +5 -0
- package/dist/esm/extensions/index.d.ts +4 -0
- package/dist/esm/extensions/index.js +5 -0
- package/dist/esm/index.d.ts +61 -0
- package/dist/esm/index.js +68 -0
- package/dist/esm/init/index.d.ts +2 -0
- package/dist/esm/init/index.js +3 -0
- package/dist/esm/init/initializer.d.ts +3 -0
- package/dist/esm/init/initializer.js +24 -0
- package/dist/esm/init/options.d.ts +3 -0
- package/dist/esm/init/options.js +19 -0
- package/dist/esm/number/index.d.ts +1 -0
- package/dist/esm/number/index.js +1 -0
- package/dist/esm/number/number-extensions.d.ts +1 -0
- package/dist/esm/number/number-extensions.js +139 -0
- package/dist/esm/object/index.d.ts +1 -0
- package/dist/esm/object/index.js +1 -0
- package/dist/esm/object/object-extensions.d.ts +1 -0
- package/dist/esm/object/object-extensions.js +145 -0
- package/dist/esm/string/index.d.ts +1 -0
- package/dist/esm/string/index.js +1 -0
- package/dist/esm/string/string-extensions.d.ts +1 -0
- package/dist/esm/string/string-extensions.js +124 -0
- package/dist/esm/types/extension-types.d.ts +9 -0
- package/dist/esm/types/extension-types.js +1 -0
- package/dist/esm/types/global-augmentations.d.ts +72 -0
- package/dist/esm/types/global-augmentations.js +1 -0
- package/dist/esm/types/index.d.ts +2 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/utils/config.d.ts +4 -0
- package/dist/esm/utils/config.js +20 -0
- package/dist/esm/utils/defineExtension.d.ts +1 -0
- package/dist/esm/utils/defineExtension.js +10 -0
- package/dist/esm/utils/helpers.d.ts +5 -0
- package/dist/esm/utils/helpers.js +30 -0
- package/dist/esm/utils/index.d.ts +3 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/types/array/array-extensions.d.ts +1 -0
- package/dist/types/array/index.d.ts +1 -0
- package/dist/types/core/index.d.ts +2 -0
- package/dist/types/core/performance.d.ts +17 -0
- package/dist/types/core/validation.d.ts +4 -0
- package/dist/types/core/version.d.ts +1 -0
- package/dist/types/extensions/index.d.ts +4 -0
- package/dist/types/index.d.ts +61 -0
- package/dist/types/init/index.d.ts +2 -0
- package/dist/types/init/initializer.d.ts +3 -0
- package/dist/types/init/options.d.ts +3 -0
- package/dist/types/number/index.d.ts +1 -0
- package/dist/types/number/number-extensions.d.ts +1 -0
- package/dist/types/object/index.d.ts +1 -0
- package/dist/types/object/object-extensions.d.ts +1 -0
- package/dist/types/string/index.d.ts +1 -0
- package/dist/types/string/string-extensions.d.ts +1 -0
- package/dist/types/types/extension-types.d.ts +9 -0
- package/dist/types/types/global-augmentations.d.ts +72 -0
- package/dist/types/types/index.d.ts +2 -0
- package/dist/types/utils/config.d.ts +4 -0
- package/dist/types/utils/defineExtension.d.ts +1 -0
- package/dist/types/utils/helpers.d.ts +5 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
@naman_deep_singh/utils
|
|
2
|
+
|
|
3
|
+
Version: 2.1.0
|
|
4
|
+
|
|
5
|
+
Universal JavaScript prototype extensions for common development utilities. Works in both Node.js and browser environments with 70+ utility methods.
|
|
6
|
+
|
|
7
|
+
⚠️ This library extends native prototypes (String, Array, Object, Number). Use consciously in shared or library code.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
```bash
|
|
13
|
+
npm install @naman_deep_singh/utils
|
|
14
|
+
# or
|
|
15
|
+
pnpm add @naman_deep_singh/utils
|
|
16
|
+
Quick Start
|
|
17
|
+
import { initializeExtensions } from '@naman_deep_singh/utils';
|
|
18
|
+
|
|
19
|
+
// Initialize all extensions
|
|
20
|
+
initializeExtensions();
|
|
21
|
+
|
|
22
|
+
// String utilities
|
|
23
|
+
"hello world".toCapitalize(); // "Hello world"
|
|
24
|
+
"hello world".capitalizeWords(); // "Hello World"
|
|
25
|
+
"hello world".reverseWords(); // "world hello"
|
|
26
|
+
"test@email.com".isEmail(); // true
|
|
27
|
+
"hello world".words(); // ["hello", "world"]
|
|
28
|
+
|
|
29
|
+
// Array utilities
|
|
30
|
+
[1, 2, 2, 3].unique(); // [1, 2, 3]
|
|
31
|
+
[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
|
+
|
|
37
|
+
// Number utilities
|
|
38
|
+
(42).toOrdinal(); // "42nd"
|
|
39
|
+
(0.75).toPercent(); // "75.00%"
|
|
40
|
+
(3.14159).toFixedNumber(2); // 3.14
|
|
41
|
+
(5).randomUpTo(); // 0..5 random number
|
|
42
|
+
(5).times(i => console.log(i)); // 0..4
|
|
43
|
+
|
|
44
|
+
// Object utilities
|
|
45
|
+
({ a: 1, b: 2 }).pick(['a']); // { a: 1 }
|
|
46
|
+
({ a: 1, b: 2 }).mapValues(v => v * 2); // { a: 2, b: 4 }
|
|
47
|
+
({ a: 1, b: 2 }).mapKeys(k => k + "_"); // { a_: 1, b_: 2 }
|
|
48
|
+
({}).isEmpty(); // true
|
|
49
|
+
({ user: { name: "John" } }).getPath('user.name'); // "John"
|
|
50
|
+
({ a: 1, b: 2 }).filterKeys(k => k === 'a'); // { a: 1 }
|
|
51
|
+
({ a: 1, b: 2 }).filterValues(v => v === 1); // { a: 1 }
|
|
52
|
+
Error Handling & Validation
|
|
53
|
+
All methods include strict runtime validation and throw clear native errors.
|
|
54
|
+
|
|
55
|
+
"hello".count(123);
|
|
56
|
+
// TypeError: count: substring must be a string, got number
|
|
57
|
+
|
|
58
|
+
"hello".count("");
|
|
59
|
+
// TypeError: count: substring cannot be empty
|
|
60
|
+
|
|
61
|
+
[].sum();
|
|
62
|
+
// TypeError: sum: array must contain at least one number
|
|
63
|
+
|
|
64
|
+
({ a: 1 }).pick(null as any);
|
|
65
|
+
// TypeError: pick: keys must be an array, got object
|
|
66
|
+
|
|
67
|
+
(3.14).round(-1);
|
|
68
|
+
// TypeError: round: decimals must be a non-negative integer, got -1
|
|
69
|
+
Error Types Used
|
|
70
|
+
|
|
71
|
+
TypeError – invalid type or missing argument
|
|
72
|
+
|
|
73
|
+
RangeError – out-of-range numeric values (e.g. toRoman)
|
|
74
|
+
|
|
75
|
+
✅ No custom error classes are exposed — only standard JS errors.
|
|
76
|
+
|
|
77
|
+
Configuration
|
|
78
|
+
Selective Extension Loading
|
|
79
|
+
import { initializeExtensions, extend } from '@naman_deep_singh/utils';
|
|
80
|
+
|
|
81
|
+
// Enable only specific prototypes
|
|
82
|
+
initializeExtensions({
|
|
83
|
+
string: true,
|
|
84
|
+
array: true,
|
|
85
|
+
object: false,
|
|
86
|
+
number: false,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Or initialize individually
|
|
90
|
+
extend.string();
|
|
91
|
+
extend.array();
|
|
92
|
+
Performance Configuration
|
|
93
|
+
Caching System
|
|
94
|
+
import { setPerformanceConfig } from '@naman_deep_singh/utils';
|
|
95
|
+
|
|
96
|
+
setPerformanceConfig({
|
|
97
|
+
enableCaching: true,
|
|
98
|
+
maxCacheSize: 200,
|
|
99
|
+
});
|
|
100
|
+
Notes:
|
|
101
|
+
|
|
102
|
+
Validation cannot be disabled
|
|
103
|
+
|
|
104
|
+
Caching is optional
|
|
105
|
+
|
|
106
|
+
Uses a simple LRU cache
|
|
107
|
+
|
|
108
|
+
String Extensions (21 methods)
|
|
109
|
+
Case Conversion
|
|
110
|
+
|
|
111
|
+
"hello world".toCapitalize(); // "Hello world"
|
|
112
|
+
"hello world".capitalizeWords(); // "Hello World"
|
|
113
|
+
"hello-world".toCamelCase(); // "helloWorld"
|
|
114
|
+
"HelloWorld".toKebabCase(); // "hello-world"
|
|
115
|
+
"Hello World".toSnakeCase(); // "hello_world"
|
|
116
|
+
"hello world".toTitleCase(); // "Hello World"
|
|
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"]
|
|
134
|
+
Array Extensions (23 methods)
|
|
135
|
+
Core Utilities
|
|
136
|
+
|
|
137
|
+
[1, 2, 2, 3].unique(); // [1,2,3]
|
|
138
|
+
[{id:1},{id:2},{id:1}].uniqueBy(x => x.id); // [{id:1},{id:2}]
|
|
139
|
+
[1,2,3,4,5].shuffle(); // random order
|
|
140
|
+
[1,2,3,4,5].chunk(2); // [[1,2],[3,4],[5]]
|
|
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');
|
|
172
|
+
Number Extensions (19 methods)
|
|
173
|
+
(0.75).toPercent(); // "75.00%"
|
|
174
|
+
(3.14159).toFixedNumber(2); // 3.14
|
|
175
|
+
(1234.56).toCurrency(); // "$1,234.56"
|
|
176
|
+
(1234.56).toCurrency('EUR','de-DE'); // "1.234,56 €"
|
|
177
|
+
(5).clamp(1,10); // 5
|
|
178
|
+
(5).inRange(1,10); // true
|
|
179
|
+
(7).isPrime(); // true
|
|
180
|
+
(5).factorial(); // 120
|
|
181
|
+
(5).randomUpTo(); // 0..5
|
|
182
|
+
(3).times(i => console.log(i)); // 0,1,2
|
|
183
|
+
(42).toOrdinal(); // "42nd"
|
|
184
|
+
(123).sign(); // 1
|
|
185
|
+
Caching Details
|
|
186
|
+
Cached Methods (when enabled):
|
|
187
|
+
|
|
188
|
+
Number.isPrime()
|
|
189
|
+
|
|
190
|
+
Number.factorial()
|
|
191
|
+
|
|
192
|
+
Number.toRoman()
|
|
193
|
+
|
|
194
|
+
Custom Caching:
|
|
195
|
+
|
|
196
|
+
import { withCache } from '@naman_deep_singh/utils';
|
|
197
|
+
const result = withCache('key', () => expensiveFn());
|
|
198
|
+
TypeScript Support
|
|
199
|
+
Full global augmentation
|
|
200
|
+
|
|
201
|
+
Strict typing for all methods
|
|
202
|
+
|
|
203
|
+
Zero any leakage in public APIs
|
|
204
|
+
|
|
205
|
+
const x: number[] = [1,2,2].unique();
|
|
206
|
+
const y: string = "hello".toCapitalize();
|
|
207
|
+
Package Stats
|
|
208
|
+
~70 utility methods
|
|
209
|
+
|
|
210
|
+
Zero dependencies
|
|
211
|
+
|
|
212
|
+
Node.js + Browser
|
|
213
|
+
|
|
214
|
+
TypeScript-first
|
|
215
|
+
|
|
216
|
+
Optional LRU caching
|
|
217
|
+
|
|
218
|
+
Strict runtime validation
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function extendArray(): void;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendArray = extendArray;
|
|
4
|
+
const utils_1 = require("src/utils");
|
|
5
|
+
let arrayExtended = false;
|
|
6
|
+
function extendArray() {
|
|
7
|
+
if (arrayExtended)
|
|
8
|
+
return;
|
|
9
|
+
arrayExtended = true;
|
|
10
|
+
(0, utils_1.defineExtension)(Array.prototype, 'unique', function () {
|
|
11
|
+
return [...new Set(this)];
|
|
12
|
+
});
|
|
13
|
+
(0, utils_1.defineExtension)(Array.prototype, 'shuffle', function () {
|
|
14
|
+
const arr = [...this];
|
|
15
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
16
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
17
|
+
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
18
|
+
}
|
|
19
|
+
return arr;
|
|
20
|
+
});
|
|
21
|
+
(0, utils_1.defineExtension)(Array.prototype, 'chunk', function (size) {
|
|
22
|
+
if (!Number.isInteger(size) || size <= 0) {
|
|
23
|
+
throw new TypeError(`chunk: size must be a positive integer, got ${size}`);
|
|
24
|
+
}
|
|
25
|
+
const chunks = [];
|
|
26
|
+
for (let i = 0; i < this.length; i += size) {
|
|
27
|
+
chunks.push(this.slice(i, i + size));
|
|
28
|
+
}
|
|
29
|
+
return chunks;
|
|
30
|
+
});
|
|
31
|
+
(0, utils_1.defineExtension)(Array.prototype, 'groupBy', function (keyFn) {
|
|
32
|
+
if (typeof keyFn !== 'function') {
|
|
33
|
+
throw new TypeError(`groupBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
34
|
+
}
|
|
35
|
+
return this.reduce((groups, item) => {
|
|
36
|
+
const key = keyFn(item);
|
|
37
|
+
if (!groups[key])
|
|
38
|
+
groups[key] = [];
|
|
39
|
+
groups[key].push(item);
|
|
40
|
+
return groups;
|
|
41
|
+
}, {});
|
|
42
|
+
});
|
|
43
|
+
(0, utils_1.defineExtension)(Array.prototype, 'sum', function () {
|
|
44
|
+
if (this.length === 0) {
|
|
45
|
+
throw new TypeError('sum: array must contain at least one number');
|
|
46
|
+
}
|
|
47
|
+
return this.reduce((sum, num) => sum + num, 0);
|
|
48
|
+
});
|
|
49
|
+
(0, utils_1.defineExtension)(Array.prototype, 'average', function () {
|
|
50
|
+
if (this.length === 0) {
|
|
51
|
+
throw new TypeError('average: array must contain at least one number');
|
|
52
|
+
}
|
|
53
|
+
return this.reduce((sum, num) => sum + num, 0) / this.length;
|
|
54
|
+
});
|
|
55
|
+
(0, utils_1.defineExtension)(Array.prototype, 'compact', function () {
|
|
56
|
+
return this.filter((item) => item !== null && item !== undefined && item !== '');
|
|
57
|
+
});
|
|
58
|
+
(0, utils_1.defineExtension)(Array.prototype, 'compactTruthy', function () {
|
|
59
|
+
return this.filter(Boolean);
|
|
60
|
+
});
|
|
61
|
+
(0, utils_1.defineExtension)(Array.prototype, 'pluck', function (key) {
|
|
62
|
+
return this.map((item) => item[key]).filter((val) => val !== undefined);
|
|
63
|
+
});
|
|
64
|
+
(0, utils_1.defineExtension)(Array.prototype, 'findLast', function (predicate) {
|
|
65
|
+
if (typeof predicate !== 'function') {
|
|
66
|
+
throw new TypeError(`findLast: predicate must be a function, got ${typeof predicate}`);
|
|
67
|
+
}
|
|
68
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
69
|
+
if (predicate(this[i]))
|
|
70
|
+
return this[i];
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
});
|
|
74
|
+
(0, utils_1.defineExtension)(Array.prototype, 'partition', function (predicate) {
|
|
75
|
+
if (typeof predicate !== 'function') {
|
|
76
|
+
throw new TypeError(`partition: predicate must be a function, got ${typeof predicate}`);
|
|
77
|
+
}
|
|
78
|
+
const truthy = [];
|
|
79
|
+
const falsy = [];
|
|
80
|
+
this.forEach((item) => predicate(item) ? truthy.push(item) : falsy.push(item));
|
|
81
|
+
return [truthy, falsy];
|
|
82
|
+
});
|
|
83
|
+
(0, utils_1.defineExtension)(Array.prototype, 'flatten', function (depth = 1) {
|
|
84
|
+
return depth > 0
|
|
85
|
+
? this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.flatten(depth - 1) : val), [])
|
|
86
|
+
: this.slice();
|
|
87
|
+
});
|
|
88
|
+
(0, utils_1.defineExtension)(Array.prototype, 'deepFlatten', function () {
|
|
89
|
+
return this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.deepFlatten() : val), []);
|
|
90
|
+
});
|
|
91
|
+
(0, utils_1.defineExtension)(Array.prototype, 'difference', function (other) {
|
|
92
|
+
if (!Array.isArray(other)) {
|
|
93
|
+
throw new TypeError(`difference: other must be an array, got ${typeof other}`);
|
|
94
|
+
}
|
|
95
|
+
return this.filter((item) => !other.includes(item));
|
|
96
|
+
});
|
|
97
|
+
(0, utils_1.defineExtension)(Array.prototype, 'intersection', function (other) {
|
|
98
|
+
if (!Array.isArray(other)) {
|
|
99
|
+
throw new TypeError(`intersection: other must be an array, got ${typeof other}`);
|
|
100
|
+
}
|
|
101
|
+
return this.filter((item) => other.includes(item));
|
|
102
|
+
});
|
|
103
|
+
(0, utils_1.defineExtension)(Array.prototype, 'union', function (other) {
|
|
104
|
+
if (!Array.isArray(other)) {
|
|
105
|
+
throw new TypeError(`union: other must be an array, got ${typeof other}`);
|
|
106
|
+
}
|
|
107
|
+
return [...new Set([...this, ...other])];
|
|
108
|
+
});
|
|
109
|
+
(0, utils_1.defineExtension)(Array.prototype, 'sample', function () {
|
|
110
|
+
return this.length > 0
|
|
111
|
+
? this[Math.floor(Math.random() * this.length)]
|
|
112
|
+
: undefined;
|
|
113
|
+
});
|
|
114
|
+
(0, utils_1.defineExtension)(Array.prototype, 'take', function (count) {
|
|
115
|
+
return this.slice(0, Math.max(0, count));
|
|
116
|
+
});
|
|
117
|
+
(0, utils_1.defineExtension)(Array.prototype, 'drop', function (count) {
|
|
118
|
+
return this.slice(Math.max(0, count));
|
|
119
|
+
});
|
|
120
|
+
(0, utils_1.defineExtension)(Array.prototype, 'uniqueBy', function (keyFn) {
|
|
121
|
+
if (typeof keyFn !== 'function') {
|
|
122
|
+
throw new TypeError(`uniqueBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
123
|
+
}
|
|
124
|
+
const seen = new Set();
|
|
125
|
+
const result = [];
|
|
126
|
+
for (const item of this) {
|
|
127
|
+
const key = keyFn(item);
|
|
128
|
+
if (!seen.has(key)) {
|
|
129
|
+
seen.add(key);
|
|
130
|
+
result.push(item);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
});
|
|
135
|
+
(0, utils_1.defineExtension)(Array.prototype, 'sortBy', function (keyFn) {
|
|
136
|
+
if (typeof keyFn !== 'function') {
|
|
137
|
+
throw new TypeError(`sortBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
138
|
+
}
|
|
139
|
+
return [...this].sort((a, b) => {
|
|
140
|
+
const aVal = keyFn(a);
|
|
141
|
+
const bVal = keyFn(b);
|
|
142
|
+
if (aVal < bVal)
|
|
143
|
+
return -1;
|
|
144
|
+
if (aVal > bVal)
|
|
145
|
+
return 1;
|
|
146
|
+
return 0;
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
(0, utils_1.defineExtension)(Array.prototype, 'last', function () {
|
|
150
|
+
return this.length > 0 ? this[this.length - 1] : undefined;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extendArray } from './array-extensions';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendArray = void 0;
|
|
4
|
+
var array_extensions_1 = require("./array-extensions");
|
|
5
|
+
Object.defineProperty(exports, "extendArray", { enumerable: true, get: function () { return array_extensions_1.extendArray; } });
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// Re-export core utilities
|
|
18
|
+
__exportStar(require("./validation"), exports);
|
|
19
|
+
__exportStar(require("./performance"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface PerformanceConfig {
|
|
2
|
+
enableCaching?: boolean;
|
|
3
|
+
maxCacheSize?: number;
|
|
4
|
+
enableValidation?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function setPerformanceConfig(newConfig: Partial<PerformanceConfig>): void;
|
|
7
|
+
export declare function getPerformanceConfig(): PerformanceConfig;
|
|
8
|
+
export declare class LRUCache<K, V> {
|
|
9
|
+
private cache;
|
|
10
|
+
private maxSize;
|
|
11
|
+
constructor(maxSize?: number);
|
|
12
|
+
get(key: K): V | undefined;
|
|
13
|
+
set(key: K, value: V): void;
|
|
14
|
+
clear(): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function makeInternalCacheKey(domain: string, key: string | number): string;
|
|
17
|
+
export declare function withCache<T>(key: string, fn: () => T): T;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LRUCache = void 0;
|
|
4
|
+
exports.setPerformanceConfig = setPerformanceConfig;
|
|
5
|
+
exports.getPerformanceConfig = getPerformanceConfig;
|
|
6
|
+
exports.makeInternalCacheKey = makeInternalCacheKey;
|
|
7
|
+
exports.withCache = withCache;
|
|
8
|
+
const version_1 = require("./version");
|
|
9
|
+
const defaultConfig = {
|
|
10
|
+
enableCaching: false,
|
|
11
|
+
maxCacheSize: 100,
|
|
12
|
+
enableValidation: true,
|
|
13
|
+
};
|
|
14
|
+
let config = { ...defaultConfig };
|
|
15
|
+
function setPerformanceConfig(newConfig) {
|
|
16
|
+
const oldMaxSize = config.maxCacheSize;
|
|
17
|
+
config = { ...config, ...newConfig };
|
|
18
|
+
// Recreate cache if maxCacheSize changed
|
|
19
|
+
if (cache && oldMaxSize !== config.maxCacheSize) {
|
|
20
|
+
cache = new LRUCache(config.maxCacheSize);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function getPerformanceConfig() {
|
|
24
|
+
return { ...config };
|
|
25
|
+
}
|
|
26
|
+
// Simple LRU cache for expensive operations
|
|
27
|
+
class LRUCache {
|
|
28
|
+
constructor(maxSize = 100) {
|
|
29
|
+
this.cache = new Map();
|
|
30
|
+
this.maxSize = maxSize;
|
|
31
|
+
}
|
|
32
|
+
get(key) {
|
|
33
|
+
const value = this.cache.get(key);
|
|
34
|
+
if (value !== undefined) {
|
|
35
|
+
// Move to end (most recently used)
|
|
36
|
+
this.cache.delete(key);
|
|
37
|
+
this.cache.set(key, value);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
set(key, value) {
|
|
42
|
+
if (this.cache.has(key)) {
|
|
43
|
+
this.cache.delete(key);
|
|
44
|
+
}
|
|
45
|
+
else if (this.cache.size >= this.maxSize) {
|
|
46
|
+
// Remove least recently used (first item)
|
|
47
|
+
const firstKey = this.cache.keys().next().value;
|
|
48
|
+
if (firstKey !== undefined) {
|
|
49
|
+
this.cache.delete(firstKey);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this.cache.set(key, value);
|
|
53
|
+
}
|
|
54
|
+
clear() {
|
|
55
|
+
this.cache.clear();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.LRUCache = LRUCache;
|
|
59
|
+
let cache = null;
|
|
60
|
+
function getOrCreateCache() {
|
|
61
|
+
if (!cache) {
|
|
62
|
+
cache = new LRUCache(config.maxCacheSize);
|
|
63
|
+
}
|
|
64
|
+
return cache;
|
|
65
|
+
}
|
|
66
|
+
function makeInternalCacheKey(domain, key) {
|
|
67
|
+
const INTERNAL_CACHE_PREFIX = '@js-ext' + (0, version_1.getPackageVersion)();
|
|
68
|
+
return `${INTERNAL_CACHE_PREFIX}:${domain}:${key}`;
|
|
69
|
+
}
|
|
70
|
+
function withCache(key, fn) {
|
|
71
|
+
if (!config.enableCaching) {
|
|
72
|
+
return fn();
|
|
73
|
+
}
|
|
74
|
+
const currentCache = getOrCreateCache();
|
|
75
|
+
const cached = currentCache.get(key);
|
|
76
|
+
if (cached !== undefined) {
|
|
77
|
+
return cached;
|
|
78
|
+
}
|
|
79
|
+
const result = fn();
|
|
80
|
+
currentCache.set(key, result);
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function validateExtensionInput(value: any, type: string, methodName: string): void;
|
|
2
|
+
export declare function validateArrayInput<T>(arr: T[], methodName: string): void;
|
|
3
|
+
export declare function validateNumberRange(num: number, min: number, max: number, methodName: string): void;
|
|
4
|
+
export declare function validatePositiveInteger(num: number, methodName: string): void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateExtensionInput = validateExtensionInput;
|
|
4
|
+
exports.validateArrayInput = validateArrayInput;
|
|
5
|
+
exports.validateNumberRange = validateNumberRange;
|
|
6
|
+
exports.validatePositiveInteger = validatePositiveInteger;
|
|
7
|
+
// Validation utilities for extensions
|
|
8
|
+
function validateExtensionInput(value, type, methodName) {
|
|
9
|
+
if (value == null) {
|
|
10
|
+
throw new TypeError(`Cannot call ${methodName} on ${value}`);
|
|
11
|
+
}
|
|
12
|
+
const actualType = typeof value;
|
|
13
|
+
if (actualType !== type) {
|
|
14
|
+
throw new TypeError(`${methodName} can only be called on ${type} values, got ${actualType}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function validateArrayInput(arr, methodName) {
|
|
18
|
+
if (!Array.isArray(arr)) {
|
|
19
|
+
throw new TypeError(`${methodName} can only be called on arrays`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function validateNumberRange(num, min, max, methodName) {
|
|
23
|
+
if (num < min || num > max) {
|
|
24
|
+
throw new RangeError(`${methodName}: number must be between ${min} and ${max}, got ${num}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function validatePositiveInteger(num, methodName) {
|
|
28
|
+
if (!Number.isInteger(num) || num < 0) {
|
|
29
|
+
throw new TypeError(`${methodName}: expected positive integer, got ${num}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getPackageVersion: () => string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPackageVersion = void 0;
|
|
4
|
+
const node_module_1 = require("node:module");
|
|
5
|
+
const requireFn = (0, node_module_1.createRequire)('../../package.json');
|
|
6
|
+
const getPackageVersion = () => {
|
|
7
|
+
return requireFn('../package.json').version;
|
|
8
|
+
};
|
|
9
|
+
exports.getPackageVersion = getPackageVersion;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// Re-export all extensions
|
|
18
|
+
__exportStar(require("../string"), exports);
|
|
19
|
+
__exportStar(require("../array"), exports);
|
|
20
|
+
__exportStar(require("../object"), exports);
|
|
21
|
+
__exportStar(require("../number"), exports);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import '../types/global-augmentations';
|
|
2
|
+
import { extendArray } from './array';
|
|
3
|
+
import { extendNumber } from './number';
|
|
4
|
+
import { extendObject } from './object';
|
|
5
|
+
import { extendString } from './string';
|
|
6
|
+
import { LRUCache, getPerformanceConfig, setPerformanceConfig, withCache } from './core/performance';
|
|
7
|
+
import { validateArrayInput, validateExtensionInput, validateNumberRange, validatePositiveInteger } from './core/validation';
|
|
8
|
+
import { mergeConfigs, validateConfig, validatePerformanceSettings } from './utils/config';
|
|
9
|
+
import { ensurePositiveInteger, getPathSegments, hasOwnProperty, isValidArrayIndex, safeClone } from './utils/helpers';
|
|
10
|
+
import { ExtensionOptions, PerformanceConfig } from './types/extension-types';
|
|
11
|
+
export { ExtensionOptions, PerformanceConfig };
|
|
12
|
+
export { withCache };
|
|
13
|
+
import { extendAll } from './init/initializer';
|
|
14
|
+
/**
|
|
15
|
+
* Initialize JavaScript prototype extensions
|
|
16
|
+
* @param options - Configure which extensions to enable (default: all enabled)
|
|
17
|
+
*/
|
|
18
|
+
export declare function initializeExtensions(options?: ExtensionOptions): void;
|
|
19
|
+
/**
|
|
20
|
+
* Initialize all extensions (convenience function)
|
|
21
|
+
*/
|
|
22
|
+
export { extendAll };
|
|
23
|
+
/**
|
|
24
|
+
* Selective prototype extension helpers
|
|
25
|
+
* Initialize only specific extensions
|
|
26
|
+
*/
|
|
27
|
+
export declare const extend: {
|
|
28
|
+
string: typeof extendString;
|
|
29
|
+
array: typeof extendArray;
|
|
30
|
+
object: typeof extendObject;
|
|
31
|
+
number: typeof extendNumber;
|
|
32
|
+
};
|
|
33
|
+
export { validateExtensionInput, validateArrayInput, validateNumberRange, validatePositiveInteger, };
|
|
34
|
+
export { validateConfig, validatePerformanceSettings, mergeConfigs, isValidArrayIndex, ensurePositiveInteger, safeClone, getPathSegments, hasOwnProperty, };
|
|
35
|
+
export { LRUCache, setPerformanceConfig, getPerformanceConfig };
|
|
36
|
+
declare const _default: {
|
|
37
|
+
initializeExtensions: typeof initializeExtensions;
|
|
38
|
+
extendAll: typeof extendAll;
|
|
39
|
+
extend: {
|
|
40
|
+
string: typeof extendString;
|
|
41
|
+
array: typeof extendArray;
|
|
42
|
+
object: typeof extendObject;
|
|
43
|
+
number: typeof extendNumber;
|
|
44
|
+
};
|
|
45
|
+
LRUCache: typeof LRUCache;
|
|
46
|
+
setPerformanceConfig: typeof setPerformanceConfig;
|
|
47
|
+
getPerformanceConfig: typeof getPerformanceConfig;
|
|
48
|
+
validateExtensionInput: typeof validateExtensionInput;
|
|
49
|
+
validateArrayInput: typeof validateArrayInput;
|
|
50
|
+
validateNumberRange: typeof validateNumberRange;
|
|
51
|
+
validatePositiveInteger: typeof validatePositiveInteger;
|
|
52
|
+
validateConfig: typeof validateConfig;
|
|
53
|
+
validatePerformanceSettings: typeof validatePerformanceSettings;
|
|
54
|
+
mergeConfigs: typeof mergeConfigs;
|
|
55
|
+
isValidArrayIndex: typeof isValidArrayIndex;
|
|
56
|
+
ensurePositiveInteger: typeof ensurePositiveInteger;
|
|
57
|
+
safeClone: typeof safeClone;
|
|
58
|
+
getPathSegments: typeof getPathSegments;
|
|
59
|
+
hasOwnProperty: typeof hasOwnProperty;
|
|
60
|
+
};
|
|
61
|
+
export default _default;
|