@naman_deep_singh/js-extensions 1.1.0 → 1.3.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.
Files changed (114) hide show
  1. package/README.md +48 -8
  2. package/dist/cjs/{array-extensions.js → array/array-extensions.js} +33 -2
  3. package/dist/cjs/array/index.d.ts +1 -0
  4. package/dist/cjs/array/index.js +5 -0
  5. package/dist/cjs/core/index.d.ts +2 -0
  6. package/dist/cjs/core/index.js +19 -0
  7. package/dist/cjs/{performance.d.ts → core/performance.d.ts} +0 -10
  8. package/dist/cjs/{performance.js → core/performance.js} +15 -4
  9. package/dist/cjs/extensions/index.d.ts +4 -0
  10. package/dist/cjs/extensions/index.js +21 -0
  11. package/dist/cjs/index.d.ts +13 -17
  12. package/dist/cjs/index.js +24 -35
  13. package/dist/cjs/init/index.d.ts +2 -0
  14. package/dist/cjs/init/index.js +19 -0
  15. package/dist/cjs/init/initializer.d.ts +3 -0
  16. package/dist/cjs/init/initializer.js +27 -0
  17. package/dist/cjs/init/options.d.ts +3 -0
  18. package/dist/cjs/init/options.js +23 -0
  19. package/dist/cjs/number/index.d.ts +1 -0
  20. package/dist/cjs/number/index.js +5 -0
  21. package/dist/cjs/{number-extensions.js → number/number-extensions.js} +23 -3
  22. package/dist/cjs/object/index.d.ts +1 -0
  23. package/dist/cjs/object/index.js +5 -0
  24. package/dist/cjs/object/object-extensions.js +146 -0
  25. package/dist/cjs/string/index.d.ts +1 -0
  26. package/dist/cjs/string/index.js +5 -0
  27. package/dist/cjs/{string-extensions.js → string/string-extensions.js} +27 -0
  28. package/dist/cjs/types/extension-types.d.ts +9 -0
  29. package/dist/cjs/types/global-augmentations.js +2 -0
  30. package/dist/cjs/types/index.d.ts +2 -0
  31. package/dist/cjs/types/index.js +19 -0
  32. package/dist/cjs/utils/config.d.ts +4 -0
  33. package/dist/cjs/utils/config.js +25 -0
  34. package/dist/cjs/utils/helpers.d.ts +5 -0
  35. package/dist/cjs/utils/helpers.js +37 -0
  36. package/dist/cjs/utils/index.d.ts +2 -0
  37. package/dist/cjs/utils/index.js +19 -0
  38. package/dist/esm/{array-extensions.js → array/array-extensions.js} +33 -2
  39. package/dist/esm/array/index.d.ts +1 -0
  40. package/dist/esm/array/index.js +1 -0
  41. package/dist/esm/core/index.d.ts +2 -0
  42. package/dist/esm/core/index.js +3 -0
  43. package/dist/esm/{performance.d.ts → core/performance.d.ts} +0 -10
  44. package/dist/esm/{performance.js → core/performance.js} +15 -3
  45. package/dist/esm/extensions/index.d.ts +4 -0
  46. package/dist/esm/extensions/index.js +5 -0
  47. package/dist/esm/index.d.ts +13 -17
  48. package/dist/esm/index.js +16 -23
  49. package/dist/esm/init/index.d.ts +2 -0
  50. package/dist/esm/init/index.js +3 -0
  51. package/dist/esm/init/initializer.d.ts +3 -0
  52. package/dist/esm/init/initializer.js +23 -0
  53. package/dist/esm/init/options.d.ts +3 -0
  54. package/dist/esm/init/options.js +19 -0
  55. package/dist/esm/number/index.d.ts +1 -0
  56. package/dist/esm/number/index.js +1 -0
  57. package/dist/esm/{number-extensions.js → number/number-extensions.js} +23 -3
  58. package/dist/esm/object/index.d.ts +1 -0
  59. package/dist/esm/object/index.js +1 -0
  60. package/dist/esm/object/object-extensions.js +143 -0
  61. package/dist/esm/string/index.d.ts +1 -0
  62. package/dist/esm/string/index.js +1 -0
  63. package/dist/esm/{string-extensions.js → string/string-extensions.js} +27 -0
  64. package/dist/esm/types/extension-types.d.ts +9 -0
  65. package/dist/esm/types/global-augmentations.js +1 -0
  66. package/dist/esm/types/index.d.ts +2 -0
  67. package/dist/esm/types/index.js +3 -0
  68. package/dist/esm/utils/config.d.ts +4 -0
  69. package/dist/esm/utils/config.js +20 -0
  70. package/dist/esm/utils/helpers.d.ts +5 -0
  71. package/dist/esm/utils/helpers.js +30 -0
  72. package/dist/esm/utils/index.d.ts +2 -0
  73. package/dist/esm/utils/index.js +3 -0
  74. package/dist/types/array/index.d.ts +1 -0
  75. package/dist/types/core/index.d.ts +2 -0
  76. package/dist/types/{performance.d.ts → core/performance.d.ts} +0 -10
  77. package/dist/types/extensions/index.d.ts +4 -0
  78. package/dist/types/index.d.ts +13 -17
  79. package/dist/types/init/index.d.ts +2 -0
  80. package/dist/types/init/initializer.d.ts +3 -0
  81. package/dist/types/init/options.d.ts +3 -0
  82. package/dist/types/number/index.d.ts +1 -0
  83. package/dist/types/object/index.d.ts +1 -0
  84. package/dist/types/string/index.d.ts +1 -0
  85. package/dist/types/types/extension-types.d.ts +9 -0
  86. package/dist/types/types/index.d.ts +2 -0
  87. package/dist/types/utils/config.d.ts +4 -0
  88. package/dist/types/utils/helpers.d.ts +5 -0
  89. package/dist/types/utils/index.d.ts +2 -0
  90. package/package.json +1 -1
  91. package/dist/cjs/object-extensions.js +0 -90
  92. package/dist/esm/object-extensions.js +0 -87
  93. /package/dist/cjs/{array-extensions.d.ts → array/array-extensions.d.ts} +0 -0
  94. /package/dist/cjs/{validation.d.ts → core/validation.d.ts} +0 -0
  95. /package/dist/cjs/{validation.js → core/validation.js} +0 -0
  96. /package/dist/cjs/{number-extensions.d.ts → number/number-extensions.d.ts} +0 -0
  97. /package/dist/cjs/{object-extensions.d.ts → object/object-extensions.d.ts} +0 -0
  98. /package/dist/cjs/{string-extensions.d.ts → string/string-extensions.d.ts} +0 -0
  99. /package/dist/cjs/{types.js → types/extension-types.js} +0 -0
  100. /package/dist/cjs/{types.d.ts → types/global-augmentations.d.ts} +0 -0
  101. /package/dist/esm/{array-extensions.d.ts → array/array-extensions.d.ts} +0 -0
  102. /package/dist/esm/{validation.d.ts → core/validation.d.ts} +0 -0
  103. /package/dist/esm/{validation.js → core/validation.js} +0 -0
  104. /package/dist/esm/{number-extensions.d.ts → number/number-extensions.d.ts} +0 -0
  105. /package/dist/esm/{object-extensions.d.ts → object/object-extensions.d.ts} +0 -0
  106. /package/dist/esm/{string-extensions.d.ts → string/string-extensions.d.ts} +0 -0
  107. /package/dist/esm/{types.js → types/extension-types.js} +0 -0
  108. /package/dist/esm/{types.d.ts → types/global-augmentations.d.ts} +0 -0
  109. /package/dist/types/{array-extensions.d.ts → array/array-extensions.d.ts} +0 -0
  110. /package/dist/types/{validation.d.ts → core/validation.d.ts} +0 -0
  111. /package/dist/types/{number-extensions.d.ts → number/number-extensions.d.ts} +0 -0
  112. /package/dist/types/{object-extensions.d.ts → object/object-extensions.d.ts} +0 -0
  113. /package/dist/types/{string-extensions.d.ts → string/string-extensions.d.ts} +0 -0
  114. /package/dist/types/{types.d.ts → types/global-augmentations.d.ts} +0 -0
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extendObject = extendObject;
4
+ function extendObject() {
5
+ Object.prototype.isEmpty = function () {
6
+ return Object.keys(this).length === 0;
7
+ };
8
+ Object.prototype.pick = function (keys) {
9
+ if (this === null || this === undefined) {
10
+ throw new TypeError('pick: cannot be called on null or undefined');
11
+ }
12
+ if (!Array.isArray(keys)) {
13
+ throw new TypeError(`pick: keys must be an array, got ${typeof keys}`);
14
+ }
15
+ if (keys.length === 0) {
16
+ throw new TypeError('pick: keys array cannot be empty');
17
+ }
18
+ const result = {};
19
+ const obj = this;
20
+ keys.forEach(key => {
21
+ if (key in obj) {
22
+ result[key] = obj[key];
23
+ }
24
+ });
25
+ return result;
26
+ };
27
+ Object.prototype.omit = function (keys) {
28
+ if (this === null || this === undefined) {
29
+ throw new TypeError('omit: cannot be called on null or undefined');
30
+ }
31
+ if (!Array.isArray(keys)) {
32
+ throw new TypeError(`omit: keys must be an array, got ${typeof keys}`);
33
+ }
34
+ if (keys.length === 0) {
35
+ throw new TypeError('omit: keys array cannot be empty');
36
+ }
37
+ const result = { ...this };
38
+ keys.forEach(key => {
39
+ delete result[key];
40
+ });
41
+ return result;
42
+ };
43
+ Object.prototype.deepClone = function () {
44
+ // Create a more robust cache key using WeakMap for cycle detection
45
+ const cloneId = Symbol('clone');
46
+ // Simple cycle detection without caching key generation
47
+ if (this === null || typeof this !== 'object')
48
+ return this;
49
+ // Handle Date objects
50
+ if (this instanceof Date)
51
+ return new Date(this.getTime());
52
+ // Handle Array objects
53
+ if (Array.isArray(this)) {
54
+ return this.map(item => {
55
+ if (item && typeof item === 'object' && typeof item.deepClone === 'function') {
56
+ return item.deepClone();
57
+ }
58
+ return item;
59
+ });
60
+ }
61
+ // Handle regular objects with better cycle detection
62
+ const visited = new WeakSet();
63
+ function deepCloneSafe(obj) {
64
+ if (obj === null || typeof obj !== 'object')
65
+ return obj;
66
+ if (visited.has(obj)) {
67
+ throw new Error('Circular reference detected in deepClone');
68
+ }
69
+ visited.add(obj);
70
+ if (obj instanceof Date)
71
+ return new Date(obj.getTime());
72
+ if (Array.isArray(obj))
73
+ return obj.map(item => deepCloneSafe(item));
74
+ const cloned = {};
75
+ Object.keys(obj).forEach(key => {
76
+ cloned[key] = deepCloneSafe(obj[key]);
77
+ });
78
+ return cloned;
79
+ }
80
+ return deepCloneSafe(this);
81
+ };
82
+ Object.prototype.merge = function (other) {
83
+ return { ...this, ...other };
84
+ };
85
+ Object.prototype.deepFreeze = function () {
86
+ const propNames = Object.getOwnPropertyNames(this);
87
+ for (const name of propNames) {
88
+ const value = this[name];
89
+ if (value && typeof value === 'object') {
90
+ value.deepFreeze();
91
+ }
92
+ }
93
+ return Object.freeze(this);
94
+ };
95
+ Object.prototype.hasPath = function (path) {
96
+ if (typeof path !== 'string') {
97
+ throw new TypeError(`hasPath: path must be a string, got ${typeof path}`);
98
+ }
99
+ if (path.trim() === '') {
100
+ throw new TypeError('hasPath: path cannot be empty or whitespace');
101
+ }
102
+ const keys = path.split('.');
103
+ let current = this;
104
+ for (const key of keys) {
105
+ if (current == null || !(key in current))
106
+ return false;
107
+ current = current[key];
108
+ }
109
+ return true;
110
+ };
111
+ Object.prototype.getPath = function (path, defaultValue) {
112
+ if (typeof path !== 'string') {
113
+ throw new TypeError(`getPath: path must be a string, got ${typeof path}`);
114
+ }
115
+ if (path.trim() === '') {
116
+ throw new TypeError('getPath: path cannot be empty or whitespace');
117
+ }
118
+ const keys = path.split('.');
119
+ let current = this;
120
+ for (const key of keys) {
121
+ if (current == null || !(key in current))
122
+ return defaultValue;
123
+ current = current[key];
124
+ }
125
+ return current;
126
+ };
127
+ Object.prototype.setPath = function (path, value) {
128
+ if (typeof path !== 'string') {
129
+ throw new TypeError(`setPath: path must be a string, got ${typeof path}`);
130
+ }
131
+ if (path.trim() === '') {
132
+ throw new TypeError('setPath: path cannot be empty or whitespace');
133
+ }
134
+ const keys = path.split('.');
135
+ let current = this;
136
+ for (let i = 0; i < keys.length - 1; i++) {
137
+ const key = keys[i];
138
+ if (!(key in current) || typeof current[key] !== 'object') {
139
+ current[key] = {};
140
+ }
141
+ current = current[key];
142
+ }
143
+ current[keys[keys.length - 1]] = value;
144
+ return this;
145
+ };
146
+ }
@@ -0,0 +1 @@
1
+ export { extendString } from './string-extensions';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extendString = void 0;
4
+ var string_extensions_1 = require("./string-extensions");
5
+ Object.defineProperty(exports, "extendString", { enumerable: true, get: function () { return string_extensions_1.extendString; } });
@@ -20,6 +20,9 @@ function extendString() {
20
20
  .toLowerCase();
21
21
  };
22
22
  String.prototype.truncate = function (length, suffix = '...') {
23
+ if (!Number.isInteger(length) || length < 0) {
24
+ throw new TypeError(`truncate: length must be a non-negative integer, got ${length}`);
25
+ }
23
26
  return this.length > length ? this.substring(0, length) + suffix : this.toString();
24
27
  };
25
28
  String.prototype.isEmail = function () {
@@ -52,12 +55,36 @@ function extendString() {
52
55
  return this.replace(/<[^>]*>/g, '');
53
56
  };
54
57
  String.prototype.padStart = function (targetLength, padString = ' ') {
58
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
59
+ throw new TypeError(`padStart: targetLength must be a non-negative integer, got ${targetLength}`);
60
+ }
61
+ if (typeof padString !== 'string') {
62
+ throw new TypeError(`padStart: padString must be a string, got ${typeof padString}`);
63
+ }
64
+ if (padString.length === 0) {
65
+ throw new TypeError('padStart: padString cannot be empty');
66
+ }
55
67
  return this.toString().padStart(targetLength, padString);
56
68
  };
57
69
  String.prototype.padEnd = function (targetLength, padString = ' ') {
70
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
71
+ throw new TypeError(`padEnd: targetLength must be a non-negative integer, got ${targetLength}`);
72
+ }
73
+ if (typeof padString !== 'string') {
74
+ throw new TypeError(`padEnd: padString must be a string, got ${typeof padString}`);
75
+ }
76
+ if (padString.length === 0) {
77
+ throw new TypeError('padEnd: padString cannot be empty');
78
+ }
58
79
  return this.toString().padEnd(targetLength, padString);
59
80
  };
60
81
  String.prototype.count = function (substring) {
82
+ if (typeof substring !== 'string') {
83
+ throw new TypeError(`count: substring must be a string, got ${typeof substring}`);
84
+ }
85
+ if (substring === '') {
86
+ throw new TypeError('count: substring cannot be empty');
87
+ }
61
88
  return (this.match(new RegExp(substring, 'g')) || []).length;
62
89
  };
63
90
  String.prototype.words = function () {
@@ -0,0 +1,9 @@
1
+ import { PerformanceConfig } from '../core/performance';
2
+ export interface ExtensionOptions {
3
+ string?: boolean;
4
+ array?: boolean;
5
+ object?: boolean;
6
+ number?: boolean;
7
+ performance?: PerformanceConfig;
8
+ }
9
+ export { PerformanceConfig };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from './extension-types';
2
+ export * from './global-augmentations';
@@ -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 all types
18
+ __exportStar(require("./extension-types"), exports);
19
+ __exportStar(require("./global-augmentations"), exports);
@@ -0,0 +1,4 @@
1
+ import { PerformanceConfig } from '../core/performance';
2
+ export declare function validateConfig(config: Partial<PerformanceConfig>, defaultConfig: PerformanceConfig): PerformanceConfig;
3
+ export declare function validatePerformanceSettings(settings: Partial<PerformanceConfig>): PerformanceConfig;
4
+ export declare function mergeConfigs<T extends Record<string, any>>(base: T, override: Partial<T>): T;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateConfig = validateConfig;
4
+ exports.validatePerformanceSettings = validatePerformanceSettings;
5
+ exports.mergeConfigs = mergeConfigs;
6
+ function validateConfig(config, defaultConfig) {
7
+ return { ...defaultConfig, ...config };
8
+ }
9
+ function validatePerformanceSettings(settings) {
10
+ const validated = {
11
+ enableCaching: settings.enableCaching ?? false,
12
+ maxCacheSize: Math.max(1, settings.maxCacheSize ?? 100),
13
+ enableValidation: settings.enableValidation ?? true
14
+ };
15
+ return validated;
16
+ }
17
+ function mergeConfigs(base, override) {
18
+ const result = { ...base };
19
+ for (const key in override) {
20
+ if (override[key] !== undefined) {
21
+ result[key] = override[key];
22
+ }
23
+ }
24
+ return result;
25
+ }
@@ -0,0 +1,5 @@
1
+ export declare function isValidArrayIndex(index: number): boolean;
2
+ export declare function ensurePositiveInteger(value: number, name: string): void;
3
+ export declare function safeClone<T>(obj: T): T;
4
+ export declare function getPathSegments(path: string): string[];
5
+ export declare function hasOwnProperty(obj: any, key: string): boolean;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ // Shared helper functions for extensions
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isValidArrayIndex = isValidArrayIndex;
5
+ exports.ensurePositiveInteger = ensurePositiveInteger;
6
+ exports.safeClone = safeClone;
7
+ exports.getPathSegments = getPathSegments;
8
+ exports.hasOwnProperty = hasOwnProperty;
9
+ function isValidArrayIndex(index) {
10
+ return Number.isInteger(index) && index >= 0;
11
+ }
12
+ function ensurePositiveInteger(value, name) {
13
+ if (!Number.isInteger(value) || value < 0) {
14
+ throw new TypeError(`${name} must be a positive integer`);
15
+ }
16
+ }
17
+ function safeClone(obj) {
18
+ if (obj === null || typeof obj !== 'object')
19
+ return obj;
20
+ if (obj instanceof Date)
21
+ return new Date(obj.getTime());
22
+ if (Array.isArray(obj))
23
+ return obj.map(item => safeClone(item));
24
+ const cloned = {};
25
+ for (const key in obj) {
26
+ if (obj.hasOwnProperty(key)) {
27
+ cloned[key] = safeClone(obj[key]);
28
+ }
29
+ }
30
+ return cloned;
31
+ }
32
+ function getPathSegments(path) {
33
+ return path.split('.').filter(segment => segment.length > 0);
34
+ }
35
+ function hasOwnProperty(obj, key) {
36
+ return Object.prototype.hasOwnProperty.call(obj, key);
37
+ }
@@ -0,0 +1,2 @@
1
+ export * from './config';
2
+ export * from './helpers';
@@ -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 all utilities
18
+ __exportStar(require("./config"), exports);
19
+ __exportStar(require("./helpers"), exports);
@@ -12,6 +12,9 @@ export function extendArray() {
12
12
  return arr;
13
13
  };
14
14
  Array.prototype.chunk = function (size) {
15
+ if (!Number.isInteger(size) || size <= 0) {
16
+ throw new TypeError(`chunk: size must be a positive integer, got ${size}`);
17
+ }
15
18
  const chunks = [];
16
19
  for (let i = 0; i < this.length; i += size) {
17
20
  chunks.push(this.slice(i, i + size));
@@ -19,6 +22,9 @@ export function extendArray() {
19
22
  return chunks;
20
23
  };
21
24
  Array.prototype.groupBy = function (keyFn) {
25
+ if (typeof keyFn !== 'function') {
26
+ throw new TypeError(`groupBy: keyFn must be a function, got ${typeof keyFn}`);
27
+ }
22
28
  return this.reduce((groups, item) => {
23
29
  const key = keyFn(item);
24
30
  if (!groups[key])
@@ -28,19 +34,32 @@ export function extendArray() {
28
34
  }, {});
29
35
  };
30
36
  Array.prototype.sum = function () {
31
- return this.reduce((sum, num) => sum + (typeof num === 'number' ? num : 0), 0);
37
+ const numbers = this.filter(item => typeof item === 'number');
38
+ if (numbers.length === 0) {
39
+ throw new TypeError('sum: array must contain at least one number');
40
+ }
41
+ return numbers.reduce((sum, num) => sum + num, 0);
32
42
  };
33
43
  Array.prototype.average = function () {
34
44
  const numbers = this.filter(item => typeof item === 'number');
35
- return numbers.length > 0 ? numbers.reduce((sum, num) => sum + num, 0) / numbers.length : 0;
45
+ if (numbers.length === 0) {
46
+ throw new TypeError('average: array must contain at least one number');
47
+ }
48
+ return numbers.reduce((sum, num) => sum + num, 0) / numbers.length;
36
49
  };
37
50
  Array.prototype.compact = function () {
38
51
  return this.filter(item => item != null && item !== '' && item !== false);
39
52
  };
40
53
  Array.prototype.pluck = function (key) {
54
+ if (typeof key !== 'string' && typeof key !== 'number' && typeof key !== 'symbol') {
55
+ throw new TypeError(`pluck: key must be a string, number, or symbol, got ${typeof key}`);
56
+ }
41
57
  return this.map(item => item && typeof item === 'object' ? item[key] : undefined).filter(val => val !== undefined);
42
58
  };
43
59
  Array.prototype.findLast = function (predicate) {
60
+ if (typeof predicate !== 'function') {
61
+ throw new TypeError(`findLast: predicate must be a function, got ${typeof predicate}`);
62
+ }
44
63
  for (let i = this.length - 1; i >= 0; i--) {
45
64
  if (predicate(this[i]))
46
65
  return this[i];
@@ -48,6 +67,9 @@ export function extendArray() {
48
67
  return undefined;
49
68
  };
50
69
  Array.prototype.partition = function (predicate) {
70
+ if (typeof predicate !== 'function') {
71
+ throw new TypeError(`partition: predicate must be a function, got ${typeof predicate}`);
72
+ }
51
73
  const truthy = [];
52
74
  const falsy = [];
53
75
  this.forEach(item => predicate(item) ? truthy.push(item) : falsy.push(item));
@@ -60,12 +82,21 @@ export function extendArray() {
60
82
  return this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.deepFlatten() : val), []);
61
83
  };
62
84
  Array.prototype.difference = function (other) {
85
+ if (!Array.isArray(other)) {
86
+ throw new TypeError(`difference: other must be an array, got ${typeof other}`);
87
+ }
63
88
  return this.filter(item => !other.includes(item));
64
89
  };
65
90
  Array.prototype.intersection = function (other) {
91
+ if (!Array.isArray(other)) {
92
+ throw new TypeError(`intersection: other must be an array, got ${typeof other}`);
93
+ }
66
94
  return this.filter(item => other.includes(item));
67
95
  };
68
96
  Array.prototype.union = function (other) {
97
+ if (!Array.isArray(other)) {
98
+ throw new TypeError(`union: other must be an array, got ${typeof other}`);
99
+ }
69
100
  return [...new Set([...this, ...other])];
70
101
  };
71
102
  Array.prototype.sample = function () {
@@ -0,0 +1 @@
1
+ export { extendArray } from './array-extensions';
@@ -0,0 +1 @@
1
+ export { extendArray } from './array-extensions';
@@ -0,0 +1,2 @@
1
+ export * from './validation';
2
+ export * from './performance';
@@ -0,0 +1,3 @@
1
+ // Re-export core utilities
2
+ export * from './validation';
3
+ export * from './performance';
@@ -5,14 +5,4 @@ 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> {
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 const cache: LRUCache<unknown, unknown>;
17
8
  export declare function withCache<T>(key: string, fn: () => T): T;
18
- export {};
@@ -5,7 +5,12 @@ const defaultConfig = {
5
5
  };
6
6
  let config = { ...defaultConfig };
7
7
  export function setPerformanceConfig(newConfig) {
8
+ const oldMaxSize = config.maxCacheSize;
8
9
  config = { ...config, ...newConfig };
10
+ // Recreate cache if maxCacheSize changed
11
+ if (cache && oldMaxSize !== config.maxCacheSize) {
12
+ cache = new LRUCache(config.maxCacheSize);
13
+ }
9
14
  }
10
15
  export function getPerformanceConfig() {
11
16
  return { ...config };
@@ -42,16 +47,23 @@ class LRUCache {
42
47
  this.cache.clear();
43
48
  }
44
49
  }
45
- export const cache = new LRUCache(config.maxCacheSize);
50
+ let cache = null;
51
+ function getOrCreateCache() {
52
+ if (!cache) {
53
+ cache = new LRUCache(config.maxCacheSize);
54
+ }
55
+ return cache;
56
+ }
46
57
  export function withCache(key, fn) {
47
58
  if (!config.enableCaching) {
48
59
  return fn();
49
60
  }
50
- const cached = cache.get(key);
61
+ const currentCache = getOrCreateCache();
62
+ const cached = currentCache.get(key);
51
63
  if (cached !== undefined) {
52
64
  return cached;
53
65
  }
54
66
  const result = fn();
55
- cache.set(key, result);
67
+ currentCache.set(key, result);
56
68
  return result;
57
69
  }
@@ -0,0 +1,4 @@
1
+ export * from '../string';
2
+ export * from '../array';
3
+ export * from '../object';
4
+ export * from '../number';
@@ -0,0 +1,5 @@
1
+ // Re-export all extensions
2
+ export * from '../string';
3
+ export * from '../array';
4
+ export * from '../object';
5
+ export * from '../number';
@@ -1,26 +1,22 @@
1
- import { extendString } from './string-extensions';
2
- import { extendArray } from './array-extensions';
3
- import { extendObject } from './object-extensions';
4
- import { extendNumber } from './number-extensions';
5
- import { setPerformanceConfig, getPerformanceConfig, type PerformanceConfig, withCache } from './performance';
6
- import './types';
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
- export interface ExtensionOptions {
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 initExtensions(options?: ExtensionOptions): void;
15
+ export declare function initializeExtensions(options?: ExtensionOptions): void;
20
16
  /**
21
17
  * Initialize all extensions (convenience function)
22
18
  */
23
- export declare function extendAll(): void;
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, type PerformanceConfig };
29
+ export { setPerformanceConfig, getPerformanceConfig };
34
30
  declare const _default: {
35
- initExtensions: typeof initExtensions;
31
+ initializeExtensions: typeof initializeExtensions;
36
32
  extendAll: typeof extendAll;
37
33
  extend: {
38
34
  string: typeof extendString;
package/dist/esm/index.js CHANGED
@@ -1,35 +1,27 @@
1
- import { extendString } from './string-extensions';
2
- import { extendArray } from './array-extensions';
3
- import { extendObject } from './object-extensions';
4
- import { extendNumber } from './number-extensions';
5
- import { setPerformanceConfig, getPerformanceConfig, withCache } from './performance';
6
- import './types';
1
+ // Import types first
2
+ import '../types/global-augmentations';
3
+ // Import extension modules
4
+ import { extendString } from './string';
5
+ import { extendArray } from './array';
6
+ import { extendObject } from './object';
7
+ import { extendNumber } from './number';
8
+ // Import core utilities
9
+ import { withCache, setPerformanceConfig, getPerformanceConfig } from './core/performance';
7
10
  // Export withCache for use in extensions
8
11
  export { withCache };
12
+ // Import initialization functions
13
+ import { initExtensions, extendAll } from './init/initializer';
9
14
  /**
10
15
  * Initialize JavaScript prototype extensions
11
16
  * @param options - Configure which extensions to enable (default: all enabled)
12
17
  */
13
- export function initExtensions(options = {}) {
14
- const { string = true, array = true, object = true, number = true, performance } = options;
15
- if (performance) {
16
- setPerformanceConfig(performance);
17
- }
18
- if (string)
19
- extendString();
20
- if (array)
21
- extendArray();
22
- if (object)
23
- extendObject();
24
- if (number)
25
- extendNumber();
18
+ export function initializeExtensions(options = {}) {
19
+ initExtensions(options);
26
20
  }
27
21
  /**
28
22
  * Initialize all extensions (convenience function)
29
23
  */
30
- export function extendAll() {
31
- initExtensions();
32
- }
24
+ export { extendAll };
33
25
  /**
34
26
  * Initialize only specific extensions
35
27
  */
@@ -41,8 +33,9 @@ export const extend = {
41
33
  };
42
34
  // Export performance utilities
43
35
  export { setPerformanceConfig, getPerformanceConfig };
36
+ // Default export
44
37
  export default {
45
- initExtensions,
38
+ initializeExtensions,
46
39
  extendAll,
47
40
  extend,
48
41
  setPerformanceConfig,
@@ -0,0 +1,2 @@
1
+ export * from './initializer';
2
+ export * from './options';
@@ -0,0 +1,3 @@
1
+ // Re-export initialization logic
2
+ export * from './initializer';
3
+ export * from './options';
@@ -0,0 +1,3 @@
1
+ import { ExtensionOptions } from '../types/extension-types';
2
+ export declare function initExtensions(options?: ExtensionOptions): void;
3
+ export declare function extendAll(): void;