@domql/utils 2.5.142 → 2.5.144

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.
@@ -38,6 +38,7 @@ __export(object_exports, {
38
38
  exec: () => exec,
39
39
  flattenRecursive: () => flattenRecursive,
40
40
  hasOwnProperty: () => hasOwnProperty,
41
+ isCyclic: () => isCyclic,
41
42
  isEmpty: () => isEmpty,
42
43
  isEmptyObject: () => isEmptyObject,
43
44
  isEqualDeep: () => isEqualDeep,
@@ -163,20 +164,28 @@ const deepClone = (obj, exclude = [], cleanUndefined = false, visited = /* @__PU
163
164
  }
164
165
  return clone2;
165
166
  };
166
- const deepCloneWithExtend = (obj, excludeFrom = ["node"], options = {}) => {
167
+ const deepCloneWithExtend = (obj, excludeFrom = ["node"], options = {}, visited = /* @__PURE__ */ new WeakSet()) => {
168
+ if ((0, import_types.isObjectLike)(obj)) {
169
+ if (visited.has(obj)) {
170
+ return obj;
171
+ }
172
+ visited.add(obj);
173
+ }
167
174
  const o = options.window ? (0, import_types.isArray)(obj) ? new options.window.Array([]) : new options.window.Object({}) : (0, import_types.isArray)(obj) ? [] : {};
168
175
  for (const prop in obj) {
169
176
  if (!Object.prototype.hasOwnProperty.call(obj, prop))
170
177
  continue;
171
178
  const objProp = obj[prop];
172
- if (excludeFrom.includes(prop) || prop.startsWith("__") || options.cleanUndefined && (0, import_types.isUndefined)(objProp) || options.cleanNull && (0, import_types.isNull)(objProp))
179
+ if (excludeFrom.includes(prop) || prop.startsWith("__") || options.cleanUndefined && (0, import_types.isUndefined)(objProp) || options.cleanNull && (0, import_types.isNull)(objProp)) {
173
180
  continue;
181
+ }
174
182
  if ((0, import_types.isObjectLike)(objProp)) {
175
- o[prop] = deepCloneWithExtend(objProp, excludeFrom, options);
183
+ o[prop] = deepCloneWithExtend(objProp, excludeFrom, options, visited);
176
184
  } else if ((0, import_types.isFunction)(objProp) && options.window) {
177
185
  o[prop] = (options.window || import_globals.window).eval("(" + objProp.toString() + ")");
178
- } else
186
+ } else {
179
187
  o[prop] = objProp;
188
+ }
180
189
  }
181
190
  return o;
182
191
  };
@@ -635,3 +644,22 @@ const detectInfiniteLoop = (arr) => {
635
644
  }
636
645
  }
637
646
  };
647
+ const isCyclic = (obj) => {
648
+ const seenObjects = [];
649
+ function detect(obj2) {
650
+ if (obj2 && typeof obj2 === "object") {
651
+ if (seenObjects.indexOf(obj2) !== -1) {
652
+ return true;
653
+ }
654
+ seenObjects.push(obj2);
655
+ for (const key in obj2) {
656
+ if (Object.hasOwnProperty.call(obj2, key) && detect(obj2[key])) {
657
+ console.log(obj2, "cycle at " + key);
658
+ return true;
659
+ }
660
+ }
661
+ }
662
+ return false;
663
+ }
664
+ return detect(obj);
665
+ };
package/object.js CHANGED
@@ -169,30 +169,46 @@ export const deepClone = (obj, exclude = [], cleanUndefined = false, visited = n
169
169
  /**
170
170
  * Deep cloning of object
171
171
  */
172
- export const deepCloneWithExtend = (obj, excludeFrom = ['node'], options = {}) => {
172
+ export const deepCloneWithExtend = (obj, excludeFrom = ['node'], options = {}, visited = new WeakSet()) => {
173
+ // Check if the value is object-like before trying to track it in visited
174
+ if (isObjectLike(obj)) {
175
+ if (visited.has(obj)) {
176
+ return obj // Return the object if it was already cloned
177
+ }
178
+ visited.add(obj) // Add to visited set only if it's an object
179
+ }
180
+
173
181
  const o = options.window
174
- ? isArray(obj) ? new options.window.Array([]) : new options.window.Object({})
175
- : isArray(obj) ? [] : {}
182
+ ? isArray(obj)
183
+ ? new options.window.Array([])
184
+ : new options.window.Object({})
185
+ : isArray(obj)
186
+ ? []
187
+ : {}
188
+
176
189
  for (const prop in obj) {
177
190
  if (!Object.prototype.hasOwnProperty.call(obj, prop)) continue
178
- // if (prop === 'node' || prop === 'parent' || prop === 'root' || prop === '__element') {
179
- // console.warn('recursive clonning is called', obj)
180
- // continue
181
- // }
191
+
182
192
  const objProp = obj[prop]
193
+
183
194
  if (
184
- excludeFrom.includes(prop) || prop.startsWith('__') ||
195
+ excludeFrom.includes(prop) ||
196
+ prop.startsWith('__') ||
185
197
  (options.cleanUndefined && isUndefined(objProp)) ||
186
198
  (options.cleanNull && isNull(objProp))
187
- ) continue
199
+ ) {
200
+ continue
201
+ }
202
+
188
203
  if (isObjectLike(objProp)) {
189
- // queueMicrotask(() => {
190
- o[prop] = deepCloneWithExtend(objProp, excludeFrom, options)
191
- // })
204
+ o[prop] = deepCloneWithExtend(objProp, excludeFrom, options, visited)
192
205
  } else if (isFunction(objProp) && options.window) {
193
206
  o[prop] = (options.window || window).eval('(' + objProp.toString() + ')')
194
- } else o[prop] = objProp
207
+ } else {
208
+ o[prop] = objProp
209
+ }
195
210
  }
211
+
196
212
  return o
197
213
  }
198
214
 
@@ -779,3 +795,25 @@ export const detectInfiniteLoop = arr => {
779
795
  }
780
796
  }
781
797
  }
798
+
799
+ export const isCyclic = (obj) => {
800
+ const seenObjects = []
801
+
802
+ function detect (obj) {
803
+ if (obj && typeof obj === 'object') {
804
+ if (seenObjects.indexOf(obj) !== -1) {
805
+ return true
806
+ }
807
+ seenObjects.push(obj)
808
+ for (const key in obj) {
809
+ if (Object.hasOwnProperty.call(obj, key) && detect(obj[key])) {
810
+ console.log(obj, 'cycle at ' + key)
811
+ return true
812
+ }
813
+ }
814
+ }
815
+ return false
816
+ }
817
+
818
+ return detect(obj)
819
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.5.142",
3
+ "version": "2.5.144",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -25,7 +25,7 @@
25
25
  "build": "yarn build:cjs",
26
26
  "prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
27
27
  },
28
- "gitHead": "65537c9b9a818948e1835b75a5c8a3e79bffdecd",
28
+ "gitHead": "9026efab1932521ec5eec3e88261eadb81f2e3cb",
29
29
  "devDependencies": {
30
30
  "@babel/core": "^7.12.0"
31
31
  }