@naman_deep_singh/js-extensions 1.2.0 → 1.3.1
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 +48 -8
- package/dist/cjs/{array-extensions.js → array/array-extensions.js} +33 -2
- 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/{performance.d.ts → core/performance.d.ts} +1 -3
- package/dist/cjs/{performance.js → core/performance.js} +17 -4
- package/dist/cjs/extensions/index.d.ts +4 -0
- package/dist/cjs/extensions/index.js +21 -0
- package/dist/cjs/index.d.ts +13 -17
- package/dist/cjs/index.js +24 -35
- 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 +27 -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-extensions.js → number/number-extensions.js} +23 -3
- package/dist/cjs/object/index.d.ts +1 -0
- package/dist/cjs/object/index.js +5 -0
- package/dist/cjs/object/object-extensions.js +146 -0
- package/dist/cjs/string/index.d.ts +1 -0
- package/dist/cjs/string/index.js +5 -0
- package/dist/cjs/{string-extensions.js → string/string-extensions.js} +27 -0
- package/dist/cjs/types/extension-types.d.ts +9 -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/helpers.d.ts +5 -0
- package/dist/cjs/utils/helpers.js +37 -0
- package/dist/cjs/utils/index.d.ts +2 -0
- package/dist/cjs/utils/index.js +19 -0
- package/dist/esm/{array-extensions.js → array/array-extensions.js} +33 -2
- 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/{performance.d.ts → core/performance.d.ts} +1 -3
- package/dist/esm/{performance.js → core/performance.js} +16 -4
- package/dist/esm/extensions/index.d.ts +4 -0
- package/dist/esm/extensions/index.js +5 -0
- package/dist/esm/index.d.ts +13 -17
- package/dist/esm/index.js +16 -23
- 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 +23 -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-extensions.js → number/number-extensions.js} +23 -3
- package/dist/esm/object/index.d.ts +1 -0
- package/dist/esm/object/index.js +1 -0
- package/dist/esm/object/object-extensions.js +143 -0
- package/dist/esm/string/index.d.ts +1 -0
- package/dist/esm/string/index.js +1 -0
- package/dist/esm/{string-extensions.js → string/string-extensions.js} +27 -0
- package/dist/esm/types/extension-types.d.ts +9 -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/helpers.d.ts +5 -0
- package/dist/esm/utils/helpers.js +30 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.js +3 -0
- package/dist/types/array/index.d.ts +1 -0
- package/dist/types/core/index.d.ts +2 -0
- package/dist/types/{performance.d.ts → core/performance.d.ts} +1 -3
- package/dist/types/extensions/index.d.ts +4 -0
- package/dist/types/index.d.ts +13 -17
- 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/object/index.d.ts +1 -0
- package/dist/types/string/index.d.ts +1 -0
- package/dist/types/types/extension-types.d.ts +9 -0
- package/dist/types/types/index.d.ts +2 -0
- package/dist/types/utils/config.d.ts +4 -0
- package/dist/types/utils/helpers.d.ts +5 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/package.json +1 -1
- package/dist/cjs/object-extensions.js +0 -103
- package/dist/esm/object-extensions.js +0 -100
- /package/dist/cjs/{array-extensions.d.ts → array/array-extensions.d.ts} +0 -0
- /package/dist/cjs/{validation.d.ts → core/validation.d.ts} +0 -0
- /package/dist/cjs/{validation.js → core/validation.js} +0 -0
- /package/dist/cjs/{number-extensions.d.ts → number/number-extensions.d.ts} +0 -0
- /package/dist/cjs/{object-extensions.d.ts → object/object-extensions.d.ts} +0 -0
- /package/dist/cjs/{string-extensions.d.ts → string/string-extensions.d.ts} +0 -0
- /package/dist/cjs/{types.js → types/extension-types.js} +0 -0
- /package/dist/cjs/{types.d.ts → types/global-augmentations.d.ts} +0 -0
- /package/dist/esm/{array-extensions.d.ts → array/array-extensions.d.ts} +0 -0
- /package/dist/esm/{validation.d.ts → core/validation.d.ts} +0 -0
- /package/dist/esm/{validation.js → core/validation.js} +0 -0
- /package/dist/esm/{number-extensions.d.ts → number/number-extensions.d.ts} +0 -0
- /package/dist/esm/{object-extensions.d.ts → object/object-extensions.d.ts} +0 -0
- /package/dist/esm/{string-extensions.d.ts → string/string-extensions.d.ts} +0 -0
- /package/dist/esm/{types.js → types/extension-types.js} +0 -0
- /package/dist/esm/{types.d.ts → types/global-augmentations.d.ts} +0 -0
- /package/dist/types/{array-extensions.d.ts → array/array-extensions.d.ts} +0 -0
- /package/dist/types/{validation.d.ts → core/validation.d.ts} +0 -0
- /package/dist/types/{number-extensions.d.ts → number/number-extensions.d.ts} +0 -0
- /package/dist/types/{object-extensions.d.ts → object/object-extensions.d.ts} +0 -0
- /package/dist/types/{string-extensions.d.ts → string/string-extensions.d.ts} +0 -0
- /package/dist/types/{types.d.ts → types/global-augmentations.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
|
|
1
2
|
# @naman_deep_singh/js-extensions
|
|
2
3
|
|
|
3
|
-
**Version:** 1.
|
|
4
|
+
**Version:** 1.3.1
|
|
4
5
|
|
|
5
6
|
Universal JavaScript prototype extensions for common development utilities. Works in both Node.js and browser environments with 50+ utility methods.
|
|
6
7
|
|
|
@@ -15,10 +16,11 @@ pnpm add @naman_deep_singh/js-extensions
|
|
|
15
16
|
## Quick Start
|
|
16
17
|
|
|
17
18
|
```typescript
|
|
18
|
-
|
|
19
|
+
|
|
20
|
+
import { initializeExtensions } from '@naman_deep_singh/js-extensions';
|
|
19
21
|
|
|
20
22
|
// Initialize all extensions
|
|
21
|
-
|
|
23
|
+
initializeExtensions();
|
|
22
24
|
|
|
23
25
|
// String utilities
|
|
24
26
|
"hello world".toCapitalize(); // "Hello world"
|
|
@@ -41,15 +43,49 @@ initExtensions();
|
|
|
41
43
|
({user: {name: "John"}}).getPath('user.name'); // "John"
|
|
42
44
|
```
|
|
43
45
|
|
|
46
|
+
|
|
47
|
+
## Error Handling & Validation
|
|
48
|
+
|
|
49
|
+
All methods now include comprehensive input validation to ensure reliability and prevent runtime errors.
|
|
50
|
+
|
|
51
|
+
### Input Validation
|
|
52
|
+
```typescript
|
|
53
|
+
// Invalid inputs now throw clear TypeError messages
|
|
54
|
+
"hello".count(123); // TypeError: count: substring must be a string, got number
|
|
55
|
+
"hello".count(""); // TypeError: count: substring cannot be empty
|
|
56
|
+
[].sum(); // TypeError: sum: array must contain at least one number
|
|
57
|
+
({a: 1}).pick(null); // TypeError: pick: keys must be an array, got object
|
|
58
|
+
(3.14).round(-1); // TypeError: round: decimals must be a non-negative integer, got -1
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Consistent Error Types
|
|
62
|
+
- **`TypeError`** - Invalid parameter types or missing required values
|
|
63
|
+
- **`RangeError`** - Out-of-range values (e.g., `toRoman` number limits)
|
|
64
|
+
|
|
65
|
+
### Performance vs Validation Trade-off
|
|
66
|
+
```typescript
|
|
67
|
+
import { setPerformanceConfig } from '@naman_deep_singh/js-extensions';
|
|
68
|
+
|
|
69
|
+
// Validation is enabled by default for reliability
|
|
70
|
+
setPerformanceConfig({
|
|
71
|
+
enableCaching: true, // Cache expensive operations
|
|
72
|
+
enableValidation: true // Input validation enabled (default)
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Note: Currently validation cannot be disabled as it's built into the methods
|
|
76
|
+
// Future versions may add an option to disable validation for maximum performance
|
|
77
|
+
```
|
|
78
|
+
|
|
44
79
|
## Configuration
|
|
45
80
|
|
|
46
81
|
### Selective Extensions
|
|
47
82
|
|
|
48
83
|
```typescript
|
|
49
|
-
|
|
84
|
+
|
|
85
|
+
import { initializeExtensions, extend } from '@naman_deep_singh/js-extensions';
|
|
50
86
|
|
|
51
87
|
// Only specific types
|
|
52
|
-
|
|
88
|
+
initializeExtensions({
|
|
53
89
|
string: true,
|
|
54
90
|
array: true,
|
|
55
91
|
object: false,
|
|
@@ -64,10 +100,11 @@ extend.array(); // Only array methods
|
|
|
64
100
|
### Performance Configuration
|
|
65
101
|
|
|
66
102
|
```typescript
|
|
67
|
-
|
|
103
|
+
|
|
104
|
+
import { initializeExtensions, setPerformanceConfig } from '@naman_deep_singh/js-extensions';
|
|
68
105
|
|
|
69
106
|
// Configure performance options
|
|
70
|
-
|
|
107
|
+
initializeExtensions({
|
|
71
108
|
performance: {
|
|
72
109
|
enableCaching: true, // Cache expensive operations
|
|
73
110
|
maxCacheSize: 200, // LRU cache size
|
|
@@ -336,6 +373,7 @@ const config: PerformanceConfig = {
|
|
|
336
373
|
};
|
|
337
374
|
```
|
|
338
375
|
|
|
376
|
+
|
|
339
377
|
## Package Stats
|
|
340
378
|
|
|
341
379
|
- **60 utility methods** across 4 JavaScript types
|
|
@@ -343,13 +381,15 @@ const config: PerformanceConfig = {
|
|
|
343
381
|
- **Universal compatibility** - Node.js and browser
|
|
344
382
|
- **TypeScript native** - complete type definitions
|
|
345
383
|
- **Performance optimized** - optional caching system
|
|
384
|
+
- **Production-ready** - comprehensive input validation
|
|
346
385
|
- **Tree-shakable** - selective imports supported
|
|
347
386
|
|
|
348
387
|
## Complete API Reference
|
|
349
388
|
|
|
350
389
|
### Core Functions
|
|
351
390
|
```typescript
|
|
352
|
-
|
|
391
|
+
|
|
392
|
+
initializeExtensions(options?: ExtensionOptions): void
|
|
353
393
|
extendAll(): void
|
|
354
394
|
setPerformanceConfig(config: Partial<PerformanceConfig>): void
|
|
355
395
|
getPerformanceConfig(): PerformanceConfig
|
|
@@ -15,6 +15,9 @@ function extendArray() {
|
|
|
15
15
|
return arr;
|
|
16
16
|
};
|
|
17
17
|
Array.prototype.chunk = function (size) {
|
|
18
|
+
if (!Number.isInteger(size) || size <= 0) {
|
|
19
|
+
throw new TypeError(`chunk: size must be a positive integer, got ${size}`);
|
|
20
|
+
}
|
|
18
21
|
const chunks = [];
|
|
19
22
|
for (let i = 0; i < this.length; i += size) {
|
|
20
23
|
chunks.push(this.slice(i, i + size));
|
|
@@ -22,6 +25,9 @@ function extendArray() {
|
|
|
22
25
|
return chunks;
|
|
23
26
|
};
|
|
24
27
|
Array.prototype.groupBy = function (keyFn) {
|
|
28
|
+
if (typeof keyFn !== 'function') {
|
|
29
|
+
throw new TypeError(`groupBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
30
|
+
}
|
|
25
31
|
return this.reduce((groups, item) => {
|
|
26
32
|
const key = keyFn(item);
|
|
27
33
|
if (!groups[key])
|
|
@@ -31,19 +37,32 @@ function extendArray() {
|
|
|
31
37
|
}, {});
|
|
32
38
|
};
|
|
33
39
|
Array.prototype.sum = function () {
|
|
34
|
-
|
|
40
|
+
const numbers = this.filter(item => typeof item === 'number');
|
|
41
|
+
if (numbers.length === 0) {
|
|
42
|
+
throw new TypeError('sum: array must contain at least one number');
|
|
43
|
+
}
|
|
44
|
+
return numbers.reduce((sum, num) => sum + num, 0);
|
|
35
45
|
};
|
|
36
46
|
Array.prototype.average = function () {
|
|
37
47
|
const numbers = this.filter(item => typeof item === 'number');
|
|
38
|
-
|
|
48
|
+
if (numbers.length === 0) {
|
|
49
|
+
throw new TypeError('average: array must contain at least one number');
|
|
50
|
+
}
|
|
51
|
+
return numbers.reduce((sum, num) => sum + num, 0) / numbers.length;
|
|
39
52
|
};
|
|
40
53
|
Array.prototype.compact = function () {
|
|
41
54
|
return this.filter(item => item != null && item !== '' && item !== false);
|
|
42
55
|
};
|
|
43
56
|
Array.prototype.pluck = function (key) {
|
|
57
|
+
if (typeof key !== 'string' && typeof key !== 'number' && typeof key !== 'symbol') {
|
|
58
|
+
throw new TypeError(`pluck: key must be a string, number, or symbol, got ${typeof key}`);
|
|
59
|
+
}
|
|
44
60
|
return this.map(item => item && typeof item === 'object' ? item[key] : undefined).filter(val => val !== undefined);
|
|
45
61
|
};
|
|
46
62
|
Array.prototype.findLast = function (predicate) {
|
|
63
|
+
if (typeof predicate !== 'function') {
|
|
64
|
+
throw new TypeError(`findLast: predicate must be a function, got ${typeof predicate}`);
|
|
65
|
+
}
|
|
47
66
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
48
67
|
if (predicate(this[i]))
|
|
49
68
|
return this[i];
|
|
@@ -51,6 +70,9 @@ function extendArray() {
|
|
|
51
70
|
return undefined;
|
|
52
71
|
};
|
|
53
72
|
Array.prototype.partition = function (predicate) {
|
|
73
|
+
if (typeof predicate !== 'function') {
|
|
74
|
+
throw new TypeError(`partition: predicate must be a function, got ${typeof predicate}`);
|
|
75
|
+
}
|
|
54
76
|
const truthy = [];
|
|
55
77
|
const falsy = [];
|
|
56
78
|
this.forEach(item => predicate(item) ? truthy.push(item) : falsy.push(item));
|
|
@@ -63,12 +85,21 @@ function extendArray() {
|
|
|
63
85
|
return this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.deepFlatten() : val), []);
|
|
64
86
|
};
|
|
65
87
|
Array.prototype.difference = function (other) {
|
|
88
|
+
if (!Array.isArray(other)) {
|
|
89
|
+
throw new TypeError(`difference: other must be an array, got ${typeof other}`);
|
|
90
|
+
}
|
|
66
91
|
return this.filter(item => !other.includes(item));
|
|
67
92
|
};
|
|
68
93
|
Array.prototype.intersection = function (other) {
|
|
94
|
+
if (!Array.isArray(other)) {
|
|
95
|
+
throw new TypeError(`intersection: other must be an array, got ${typeof other}`);
|
|
96
|
+
}
|
|
69
97
|
return this.filter(item => other.includes(item));
|
|
70
98
|
};
|
|
71
99
|
Array.prototype.union = function (other) {
|
|
100
|
+
if (!Array.isArray(other)) {
|
|
101
|
+
throw new TypeError(`union: other must be an array, got ${typeof other}`);
|
|
102
|
+
}
|
|
72
103
|
return [...new Set([...this, ...other])];
|
|
73
104
|
};
|
|
74
105
|
Array.prototype.sample = function () {
|
|
@@ -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);
|
|
@@ -5,7 +5,7 @@ export interface PerformanceConfig {
|
|
|
5
5
|
}
|
|
6
6
|
export declare function setPerformanceConfig(newConfig: Partial<PerformanceConfig>): void;
|
|
7
7
|
export declare function getPerformanceConfig(): PerformanceConfig;
|
|
8
|
-
declare class LRUCache<K, V> {
|
|
8
|
+
export declare class LRUCache<K, V> {
|
|
9
9
|
private cache;
|
|
10
10
|
private maxSize;
|
|
11
11
|
constructor(maxSize?: number);
|
|
@@ -13,6 +13,4 @@ declare class LRUCache<K, V> {
|
|
|
13
13
|
set(key: K, value: V): void;
|
|
14
14
|
clear(): void;
|
|
15
15
|
}
|
|
16
|
-
export declare const cache: LRUCache<unknown, unknown>;
|
|
17
16
|
export declare function withCache<T>(key: string, fn: () => T): T;
|
|
18
|
-
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.LRUCache = void 0;
|
|
4
4
|
exports.setPerformanceConfig = setPerformanceConfig;
|
|
5
5
|
exports.getPerformanceConfig = getPerformanceConfig;
|
|
6
6
|
exports.withCache = withCache;
|
|
@@ -11,7 +11,12 @@ const defaultConfig = {
|
|
|
11
11
|
};
|
|
12
12
|
let config = { ...defaultConfig };
|
|
13
13
|
function setPerformanceConfig(newConfig) {
|
|
14
|
+
const oldMaxSize = config.maxCacheSize;
|
|
14
15
|
config = { ...config, ...newConfig };
|
|
16
|
+
// Recreate cache if maxCacheSize changed
|
|
17
|
+
if (cache && oldMaxSize !== config.maxCacheSize) {
|
|
18
|
+
cache = new LRUCache(config.maxCacheSize);
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
function getPerformanceConfig() {
|
|
17
22
|
return { ...config };
|
|
@@ -48,16 +53,24 @@ class LRUCache {
|
|
|
48
53
|
this.cache.clear();
|
|
49
54
|
}
|
|
50
55
|
}
|
|
51
|
-
exports.
|
|
56
|
+
exports.LRUCache = LRUCache;
|
|
57
|
+
let cache = null;
|
|
58
|
+
function getOrCreateCache() {
|
|
59
|
+
if (!cache) {
|
|
60
|
+
cache = new LRUCache(config.maxCacheSize);
|
|
61
|
+
}
|
|
62
|
+
return cache;
|
|
63
|
+
}
|
|
52
64
|
function withCache(key, fn) {
|
|
53
65
|
if (!config.enableCaching) {
|
|
54
66
|
return fn();
|
|
55
67
|
}
|
|
56
|
-
const
|
|
68
|
+
const currentCache = getOrCreateCache();
|
|
69
|
+
const cached = currentCache.get(key);
|
|
57
70
|
if (cached !== undefined) {
|
|
58
71
|
return cached;
|
|
59
72
|
}
|
|
60
73
|
const result = fn();
|
|
61
|
-
|
|
74
|
+
currentCache.set(key, result);
|
|
62
75
|
return result;
|
|
63
76
|
}
|
|
@@ -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);
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,26 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import './
|
|
1
|
+
import '../types/global-augmentations';
|
|
2
|
+
import { extendString } from './string';
|
|
3
|
+
import { extendArray } from './array';
|
|
4
|
+
import { extendObject } from './object';
|
|
5
|
+
import { extendNumber } from './number';
|
|
6
|
+
import { withCache, setPerformanceConfig, getPerformanceConfig } from './core/performance';
|
|
7
|
+
import { ExtensionOptions, PerformanceConfig } from './types/extension-types';
|
|
8
|
+
export { ExtensionOptions, PerformanceConfig };
|
|
7
9
|
export { withCache };
|
|
8
|
-
|
|
9
|
-
string?: boolean;
|
|
10
|
-
array?: boolean;
|
|
11
|
-
object?: boolean;
|
|
12
|
-
number?: boolean;
|
|
13
|
-
performance?: PerformanceConfig;
|
|
14
|
-
}
|
|
10
|
+
import { extendAll } from './init/initializer';
|
|
15
11
|
/**
|
|
16
12
|
* Initialize JavaScript prototype extensions
|
|
17
13
|
* @param options - Configure which extensions to enable (default: all enabled)
|
|
18
14
|
*/
|
|
19
|
-
export declare function
|
|
15
|
+
export declare function initializeExtensions(options?: ExtensionOptions): void;
|
|
20
16
|
/**
|
|
21
17
|
* Initialize all extensions (convenience function)
|
|
22
18
|
*/
|
|
23
|
-
export
|
|
19
|
+
export { extendAll };
|
|
24
20
|
/**
|
|
25
21
|
* Initialize only specific extensions
|
|
26
22
|
*/
|
|
@@ -30,9 +26,9 @@ export declare const extend: {
|
|
|
30
26
|
object: typeof extendObject;
|
|
31
27
|
number: typeof extendNumber;
|
|
32
28
|
};
|
|
33
|
-
export { setPerformanceConfig, getPerformanceConfig
|
|
29
|
+
export { setPerformanceConfig, getPerformanceConfig };
|
|
34
30
|
declare const _default: {
|
|
35
|
-
|
|
31
|
+
initializeExtensions: typeof initializeExtensions;
|
|
36
32
|
extendAll: typeof extendAll;
|
|
37
33
|
extend: {
|
|
38
34
|
string: typeof extendString;
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,53 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPerformanceConfig = exports.setPerformanceConfig = exports.extend = exports.withCache = void 0;
|
|
4
|
-
exports.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
3
|
+
exports.getPerformanceConfig = exports.setPerformanceConfig = exports.extend = exports.extendAll = exports.withCache = void 0;
|
|
4
|
+
exports.initializeExtensions = initializeExtensions;
|
|
5
|
+
// Import types first
|
|
6
|
+
require("../types/global-augmentations");
|
|
7
|
+
// Import extension modules
|
|
8
|
+
const string_1 = require("./string");
|
|
9
|
+
const array_1 = require("./array");
|
|
10
|
+
const object_1 = require("./object");
|
|
11
|
+
const number_1 = require("./number");
|
|
12
|
+
// Import core utilities
|
|
13
|
+
const performance_1 = require("./core/performance");
|
|
14
|
+
Object.defineProperty(exports, "withCache", { enumerable: true, get: function () { return performance_1.withCache; } });
|
|
11
15
|
Object.defineProperty(exports, "setPerformanceConfig", { enumerable: true, get: function () { return performance_1.setPerformanceConfig; } });
|
|
12
16
|
Object.defineProperty(exports, "getPerformanceConfig", { enumerable: true, get: function () { return performance_1.getPerformanceConfig; } });
|
|
13
|
-
|
|
14
|
-
require("./
|
|
17
|
+
// Import initialization functions
|
|
18
|
+
const initializer_1 = require("./init/initializer");
|
|
19
|
+
Object.defineProperty(exports, "extendAll", { enumerable: true, get: function () { return initializer_1.extendAll; } });
|
|
15
20
|
/**
|
|
16
21
|
* Initialize JavaScript prototype extensions
|
|
17
22
|
* @param options - Configure which extensions to enable (default: all enabled)
|
|
18
23
|
*/
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
if (performance) {
|
|
22
|
-
(0, performance_1.setPerformanceConfig)(performance);
|
|
23
|
-
}
|
|
24
|
-
if (string)
|
|
25
|
-
(0, string_extensions_1.extendString)();
|
|
26
|
-
if (array)
|
|
27
|
-
(0, array_extensions_1.extendArray)();
|
|
28
|
-
if (object)
|
|
29
|
-
(0, object_extensions_1.extendObject)();
|
|
30
|
-
if (number)
|
|
31
|
-
(0, number_extensions_1.extendNumber)();
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Initialize all extensions (convenience function)
|
|
35
|
-
*/
|
|
36
|
-
function extendAll() {
|
|
37
|
-
initExtensions();
|
|
24
|
+
function initializeExtensions(options = {}) {
|
|
25
|
+
(0, initializer_1.initExtensions)(options);
|
|
38
26
|
}
|
|
39
27
|
/**
|
|
40
28
|
* Initialize only specific extensions
|
|
41
29
|
*/
|
|
42
30
|
exports.extend = {
|
|
43
|
-
string:
|
|
44
|
-
array:
|
|
45
|
-
object:
|
|
46
|
-
number:
|
|
31
|
+
string: string_1.extendString,
|
|
32
|
+
array: array_1.extendArray,
|
|
33
|
+
object: object_1.extendObject,
|
|
34
|
+
number: number_1.extendNumber
|
|
47
35
|
};
|
|
36
|
+
// Default export
|
|
48
37
|
exports.default = {
|
|
49
|
-
|
|
50
|
-
extendAll,
|
|
38
|
+
initializeExtensions,
|
|
39
|
+
extendAll: initializer_1.extendAll,
|
|
51
40
|
extend: exports.extend,
|
|
52
41
|
setPerformanceConfig: performance_1.setPerformanceConfig,
|
|
53
42
|
getPerformanceConfig: performance_1.getPerformanceConfig
|
|
@@ -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 initialization logic
|
|
18
|
+
__exportStar(require("./initializer"), exports);
|
|
19
|
+
__exportStar(require("./options"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initExtensions = initExtensions;
|
|
4
|
+
exports.extendAll = extendAll;
|
|
5
|
+
const string_1 = require("../string");
|
|
6
|
+
const array_1 = require("../array");
|
|
7
|
+
const object_1 = require("../object");
|
|
8
|
+
const number_1 = require("../number");
|
|
9
|
+
const performance_1 = require("../core/performance");
|
|
10
|
+
function initExtensions(options = {}) {
|
|
11
|
+
const { string = true, array = true, object = true, number = true, performance } = options;
|
|
12
|
+
if (performance) {
|
|
13
|
+
// Set performance config if provided
|
|
14
|
+
(0, performance_1.setPerformanceConfig)(performance);
|
|
15
|
+
}
|
|
16
|
+
if (string)
|
|
17
|
+
(0, string_1.extendString)();
|
|
18
|
+
if (array)
|
|
19
|
+
(0, array_1.extendArray)();
|
|
20
|
+
if (object)
|
|
21
|
+
(0, object_1.extendObject)();
|
|
22
|
+
if (number)
|
|
23
|
+
(0, number_1.extendNumber)();
|
|
24
|
+
}
|
|
25
|
+
function extendAll() {
|
|
26
|
+
initExtensions();
|
|
27
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ExtensionOptions } from '../types/extension-types';
|
|
2
|
+
export declare function validateExtensionOptions(options: Partial<ExtensionOptions>): ExtensionOptions;
|
|
3
|
+
export declare function createExtensionOptions(enableString?: boolean, enableArray?: boolean, enableObject?: boolean, enableNumber?: boolean, performanceConfig?: any): ExtensionOptions;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateExtensionOptions = validateExtensionOptions;
|
|
4
|
+
exports.createExtensionOptions = createExtensionOptions;
|
|
5
|
+
function validateExtensionOptions(options) {
|
|
6
|
+
const validated = {
|
|
7
|
+
string: options.string !== false,
|
|
8
|
+
array: options.array !== false,
|
|
9
|
+
object: options.object !== false,
|
|
10
|
+
number: options.number !== false,
|
|
11
|
+
performance: options.performance
|
|
12
|
+
};
|
|
13
|
+
return validated;
|
|
14
|
+
}
|
|
15
|
+
function createExtensionOptions(enableString = true, enableArray = true, enableObject = true, enableNumber = true, performanceConfig) {
|
|
16
|
+
return {
|
|
17
|
+
string: enableString,
|
|
18
|
+
array: enableArray,
|
|
19
|
+
object: enableObject,
|
|
20
|
+
number: enableNumber,
|
|
21
|
+
performance: performanceConfig
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extendNumber } from './number-extensions';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendNumber = void 0;
|
|
4
|
+
var number_extensions_1 = require("./number-extensions");
|
|
5
|
+
Object.defineProperty(exports, "extendNumber", { enumerable: true, get: function () { return number_extensions_1.extendNumber; } });
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extendNumber = extendNumber;
|
|
4
4
|
// Number prototype extensions
|
|
5
|
-
const performance_1 = require("
|
|
5
|
+
const performance_1 = require("../core/performance");
|
|
6
6
|
function extendNumber() {
|
|
7
7
|
Number.prototype.toPercent = function (decimals = 2) {
|
|
8
8
|
return (this.valueOf() * 100).toFixed(decimals) + '%';
|
|
@@ -14,6 +14,9 @@ function extendNumber() {
|
|
|
14
14
|
}).format(this.valueOf());
|
|
15
15
|
};
|
|
16
16
|
Number.prototype.clamp = function (min, max) {
|
|
17
|
+
if (min > max) {
|
|
18
|
+
throw new RangeError(`clamp: min (${min}) cannot be greater than max (${max})`);
|
|
19
|
+
}
|
|
17
20
|
return Math.min(Math.max(this.valueOf(), min), max);
|
|
18
21
|
};
|
|
19
22
|
Number.prototype.isEven = function () {
|
|
@@ -56,9 +59,14 @@ function extendNumber() {
|
|
|
56
59
|
};
|
|
57
60
|
Number.prototype.toRoman = function () {
|
|
58
61
|
const num = Math.floor(this.valueOf());
|
|
62
|
+
// Better validation for roman numerals
|
|
63
|
+
if (num <= 0) {
|
|
64
|
+
throw new RangeError('toRoman: number must be positive');
|
|
65
|
+
}
|
|
66
|
+
if (num >= 4000) {
|
|
67
|
+
throw new RangeError('toRoman: number must be less than 4000');
|
|
68
|
+
}
|
|
59
69
|
return (0, performance_1.withCache)(`roman_${num}`, () => {
|
|
60
|
-
if (num <= 0 || num >= 4000)
|
|
61
|
-
return num.toString();
|
|
62
70
|
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
|
|
63
71
|
const symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
|
|
64
72
|
let result = '';
|
|
@@ -77,14 +85,23 @@ function extendNumber() {
|
|
|
77
85
|
return num >= min && num <= max;
|
|
78
86
|
};
|
|
79
87
|
Number.prototype.round = function (decimals = 0) {
|
|
88
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
89
|
+
throw new TypeError(`round: decimals must be a non-negative integer, got ${decimals}`);
|
|
90
|
+
}
|
|
80
91
|
const factor = Math.pow(10, decimals);
|
|
81
92
|
return Math.round(this.valueOf() * factor) / factor;
|
|
82
93
|
};
|
|
83
94
|
Number.prototype.ceil = function (decimals = 0) {
|
|
95
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
96
|
+
throw new TypeError(`ceil: decimals must be a non-negative integer, got ${decimals}`);
|
|
97
|
+
}
|
|
84
98
|
const factor = Math.pow(10, decimals);
|
|
85
99
|
return Math.ceil(this.valueOf() * factor) / factor;
|
|
86
100
|
};
|
|
87
101
|
Number.prototype.floor = function (decimals = 0) {
|
|
102
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
103
|
+
throw new TypeError(`floor: decimals must be a non-negative integer, got ${decimals}`);
|
|
104
|
+
}
|
|
88
105
|
const factor = Math.pow(10, decimals);
|
|
89
106
|
return Math.floor(this.valueOf() * factor) / factor;
|
|
90
107
|
};
|
|
@@ -95,6 +112,9 @@ function extendNumber() {
|
|
|
95
112
|
return Math.sign(this.valueOf());
|
|
96
113
|
};
|
|
97
114
|
Number.prototype.times = function (callback) {
|
|
115
|
+
if (typeof callback !== 'function') {
|
|
116
|
+
throw new TypeError(`times: callback must be a function, got ${typeof callback}`);
|
|
117
|
+
}
|
|
98
118
|
const num = Math.floor(this.valueOf());
|
|
99
119
|
for (let i = 0; i < num; i++) {
|
|
100
120
|
callback(i);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extendObject } from './object-extensions';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendObject = void 0;
|
|
4
|
+
var object_extensions_1 = require("./object-extensions");
|
|
5
|
+
Object.defineProperty(exports, "extendObject", { enumerable: true, get: function () { return object_extensions_1.extendObject; } });
|