@naman_deep_singh/js-extensions 1.1.0 → 1.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @naman_deep_singh/js-extensions
2
2
 
3
- **Version:** 1.1.0
3
+ **Version:** 1.2.0
4
4
 
5
5
  Universal JavaScript prototype extensions for common development utilities. Works in both Node.js and browser environments with 50+ utility methods.
6
6
 
@@ -25,18 +25,31 @@ function extendObject() {
25
25
  return result;
26
26
  };
27
27
  Object.prototype.deepClone = function () {
28
- const objStr = JSON.stringify(this);
29
- return (0, performance_1.withCache)(`clone_${objStr}`, () => {
28
+ return (0, performance_1.withCache)(`clone_${JSON.stringify(this)}`, () => {
30
29
  if (this === null || typeof this !== 'object')
31
30
  return this;
31
+ // Handle Date objects
32
32
  if (this instanceof Date)
33
33
  return new Date(this.getTime());
34
- if (this instanceof Array)
35
- return this.map(item => item?.deepClone?.() || item);
34
+ // Handle Array objects
35
+ if (Array.isArray(this)) {
36
+ return this.map(item => {
37
+ if (item && typeof item === 'object' && typeof item.deepClone === 'function') {
38
+ return item.deepClone();
39
+ }
40
+ return item;
41
+ });
42
+ }
43
+ // Handle regular objects
36
44
  const cloned = {};
37
45
  Object.keys(this).forEach(key => {
38
46
  const value = this[key];
39
- cloned[key] = value?.deepClone?.() || value;
47
+ if (value && typeof value === 'object' && typeof value.deepClone === 'function') {
48
+ cloned[key] = value.deepClone();
49
+ }
50
+ else {
51
+ cloned[key] = value;
52
+ }
40
53
  });
41
54
  return cloned;
42
55
  });
@@ -22,18 +22,31 @@ export function extendObject() {
22
22
  return result;
23
23
  };
24
24
  Object.prototype.deepClone = function () {
25
- const objStr = JSON.stringify(this);
26
- return withCache(`clone_${objStr}`, () => {
25
+ return withCache(`clone_${JSON.stringify(this)}`, () => {
27
26
  if (this === null || typeof this !== 'object')
28
27
  return this;
28
+ // Handle Date objects
29
29
  if (this instanceof Date)
30
30
  return new Date(this.getTime());
31
- if (this instanceof Array)
32
- return this.map(item => item?.deepClone?.() || item);
31
+ // Handle Array objects
32
+ if (Array.isArray(this)) {
33
+ return this.map(item => {
34
+ if (item && typeof item === 'object' && typeof item.deepClone === 'function') {
35
+ return item.deepClone();
36
+ }
37
+ return item;
38
+ });
39
+ }
40
+ // Handle regular objects
33
41
  const cloned = {};
34
42
  Object.keys(this).forEach(key => {
35
43
  const value = this[key];
36
- cloned[key] = value?.deepClone?.() || value;
44
+ if (value && typeof value === 'object' && typeof value.deepClone === 'function') {
45
+ cloned[key] = value.deepClone();
46
+ }
47
+ else {
48
+ cloned[key] = value;
49
+ }
37
50
  });
38
51
  return cloned;
39
52
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naman_deep_singh/js-extensions",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Universal JavaScript prototype extensions for common development utilities",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",