@domql/utils
Advanced tools
Comparing version 2.5.159 to 2.5.161
'use strict' | ||
import { deepCloneWithExtend, deepMerge } from './object' | ||
import { deepClone, deepMerge } from './object' | ||
import { isArray, isNumber, isString } from './types' | ||
@@ -42,4 +42,4 @@ | ||
*/ | ||
export const mergeArray = (arr, excludeFrom = []) => { | ||
return arr.reduce((a, c) => deepMerge(a, deepCloneWithExtend(c, excludeFrom), excludeFrom), {}) | ||
export const mergeArray = (arr, exclude = []) => { | ||
return arr.reduce((a, c) => deepMerge(a, deepClone(c, { exclude }), exclude), {}) | ||
} | ||
@@ -51,3 +51,3 @@ | ||
export const mergeAndCloneIfArray = obj => { | ||
return isArray(obj) ? mergeArray(obj) : deepCloneWithExtend(obj) | ||
return isArray(obj) ? mergeArray(obj) : deepClone(obj) | ||
} | ||
@@ -54,0 +54,0 @@ |
'use strict' | ||
import { | ||
deepCloneWithExtend, | ||
deepClone, | ||
exec, | ||
@@ -119,3 +119,3 @@ isArray, | ||
if (newChild === null) assignChild(null) | ||
else if (!childElem) assignChild(deepCloneWithExtend(newChild)) | ||
else if (!childElem) assignChild(deepClone(newChild)) | ||
else { | ||
@@ -122,0 +122,0 @@ const isSugarChildElem = checkIfSugar(childElem, parent, key) |
@@ -70,7 +70,7 @@ "use strict"; | ||
}; | ||
const mergeArray = (arr, excludeFrom = []) => { | ||
return arr.reduce((a, c) => (0, import_object.deepMerge)(a, (0, import_object.deepCloneWithExtend)(c, excludeFrom), excludeFrom), {}); | ||
const mergeArray = (arr, exclude = []) => { | ||
return arr.reduce((a, c) => (0, import_object.deepMerge)(a, (0, import_object.deepClone)(c, { exclude }), exclude), {}); | ||
}; | ||
const mergeAndCloneIfArray = (obj) => { | ||
return (0, import_types.isArray)(obj) ? mergeArray(obj) : (0, import_object.deepCloneWithExtend)(obj); | ||
return (0, import_types.isArray)(obj) ? mergeArray(obj) : (0, import_object.deepClone)(obj); | ||
}; | ||
@@ -77,0 +77,0 @@ const cutArrayBeforeValue = (arr, value) => { |
@@ -138,3 +138,3 @@ "use strict"; | ||
else if (!childElem) | ||
assignChild((0, import__.deepCloneWithExtend)(newChild)); | ||
assignChild((0, import__.deepClone)(newChild)); | ||
else { | ||
@@ -141,0 +141,0 @@ const isSugarChildElem = checkIfSugar(childElem, parent, key); |
@@ -25,4 +25,2 @@ "use strict"; | ||
deepClone: () => deepClone, | ||
deepCloneExclude: () => deepCloneExclude, | ||
deepCloneWithExtend: () => deepCloneWithExtend, | ||
deepContains: () => deepContains, | ||
@@ -121,74 +119,52 @@ deepDestringify: () => deepDestringify, | ||
}; | ||
const deepCloneExclude = (obj, excludeFrom = []) => { | ||
if ((0, import_types.isArray)(obj)) { | ||
return obj.map((x) => deepCloneExclude(x, excludeFrom)); | ||
} | ||
const o = {}; | ||
for (const k in obj) { | ||
const hasOwnProperty2 = Object.prototype.hasOwnProperty.call(obj, k); | ||
if (!hasOwnProperty2 || excludeFrom.includes(k) || k.startsWith("__")) | ||
continue; | ||
let v = obj[k]; | ||
if (k === "extend" && (0, import_types.isArray)(v)) { | ||
v = mergeArrayExclude(v, excludeFrom); | ||
} | ||
if ((0, import_types.isArray)(v)) { | ||
o[k] = v.map((x) => deepCloneExclude(x, excludeFrom)); | ||
} else if ((0, import_types.isObject)(v)) { | ||
o[k] = deepCloneExclude(v, excludeFrom); | ||
} else | ||
o[k] = v; | ||
} | ||
return o; | ||
const mergeArrayExclude = (arr, exclude = []) => { | ||
return arr.reduce((acc, curr) => deepMerge(acc, deepClone(curr, { exclude })), {}); | ||
}; | ||
const mergeArrayExclude = (arr, excl = []) => { | ||
return arr.reduce((acc, curr) => deepMerge(acc, deepCloneExclude(curr, excl)), {}); | ||
}; | ||
const deepClone = (obj, exclude = [], cleanUndefined = false, visited = /* @__PURE__ */ new WeakMap()) => { | ||
if (!(0, import_types.isObjectLike)(obj) || (0, import_node.isDOMNode)(obj)) | ||
const deepClone = (obj, options = {}) => { | ||
const { | ||
exclude = [], | ||
cleanUndefined = false, | ||
cleanNull = false, | ||
window: targetWindow, | ||
visited = /* @__PURE__ */ new WeakMap(), | ||
handleExtend = false | ||
} = options; | ||
if (!(0, import_types.isObjectLike)(obj) || (0, import_node.isDOMNode)(obj)) { | ||
return obj; | ||
if (visited.has(obj)) | ||
} | ||
if (visited.has(obj)) { | ||
return visited.get(obj); | ||
const clone2 = (0, import_types.isArray)(obj) ? [] : {}; | ||
} | ||
const clone2 = targetWindow ? (0, import_types.isArray)(obj) ? new targetWindow.Array() : new targetWindow.Object() : (0, import_types.isArray)(obj) ? [] : {}; | ||
visited.set(obj, clone2); | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key) && !exclude.includes(key)) { | ||
const value = obj[key]; | ||
if ((0, import_node.isDOMNode)(value)) { | ||
clone2[key] = value; | ||
} else if (key === "extend" && (0, import_types.isArray)(value)) { | ||
clone2[key] = (0, import_array.mergeArray)(value, exclude); | ||
} else if ((0, import_types.isObjectLike)(value)) { | ||
clone2[key] = deepClone(value, exclude, cleanUndefined, visited); | ||
} else { | ||
clone2[key] = value; | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) | ||
continue; | ||
if (exclude.includes(key) || key.startsWith("__") || key === "__proto__") | ||
continue; | ||
const value = obj[key]; | ||
if (cleanUndefined && (0, import_types.isUndefined)(value) || cleanNull && (0, import_types.isNull)(value)) | ||
continue; | ||
if ((0, import_node.isDOMNode)(value)) { | ||
clone2[key] = value; | ||
continue; | ||
} | ||
} | ||
return clone2; | ||
}; | ||
const deepCloneWithExtend = (obj, excludeFrom = ["node"], options = {}, visited = /* @__PURE__ */ new WeakSet()) => { | ||
if ((0, import_types.isObjectLike)(obj)) { | ||
if (visited.has(obj)) { | ||
return obj; | ||
if (handleExtend && key === "extend" && (0, import_types.isArray)(value)) { | ||
clone2[key] = (0, import_array.mergeArray)(value, exclude); | ||
continue; | ||
} | ||
visited.add(obj); | ||
} | ||
const o = options.window ? (0, import_types.isArray)(obj) ? new options.window.Array([]) : new options.window.Object({}) : (0, import_types.isArray)(obj) ? [] : {}; | ||
for (const prop in obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, prop)) | ||
if ((0, import_types.isFunction)(value) && targetWindow) { | ||
clone2[key] = targetWindow.eval("(" + value.toString() + ")"); | ||
continue; | ||
const objProp = obj[prop]; | ||
if (excludeFrom.includes(prop) || prop.startsWith("__") || options.cleanUndefined && (0, import_types.isUndefined)(objProp) || options.cleanNull && (0, import_types.isNull)(objProp)) { | ||
continue; | ||
} | ||
if ((0, import_types.isObjectLike)(objProp)) { | ||
o[prop] = deepCloneWithExtend(objProp, excludeFrom, options, visited); | ||
} else if ((0, import_types.isFunction)(objProp) && options.window) { | ||
o[prop] = (options.window || import_globals.window).eval("(" + objProp.toString() + ")"); | ||
if ((0, import_types.isObjectLike)(value)) { | ||
clone2[key] = deepClone(value, { | ||
...options, | ||
visited | ||
}); | ||
} else { | ||
o[prop] = objProp; | ||
clone2[key] = value; | ||
} | ||
} | ||
return o; | ||
return clone2; | ||
}; | ||
@@ -195,0 +171,0 @@ const deepStringify = (obj, stringified = {}) => { |
@@ -1,2 +0,2 @@ | ||
import { deepCloneWithExtend, deepMerge } from "./object"; | ||
import { deepClone, deepMerge } from "./object"; | ||
import { isArray, isNumber, isString } from "./types"; | ||
@@ -32,7 +32,7 @@ const arrayContainsOtherArray = (arr1, arr2) => { | ||
}; | ||
const mergeArray = (arr, excludeFrom = []) => { | ||
return arr.reduce((a, c) => deepMerge(a, deepCloneWithExtend(c, excludeFrom), excludeFrom), {}); | ||
const mergeArray = (arr, exclude = []) => { | ||
return arr.reduce((a, c) => deepMerge(a, deepClone(c, { exclude }), exclude), {}); | ||
}; | ||
const mergeAndCloneIfArray = (obj) => { | ||
return isArray(obj) ? mergeArray(obj) : deepCloneWithExtend(obj); | ||
return isArray(obj) ? mergeArray(obj) : deepClone(obj); | ||
}; | ||
@@ -39,0 +39,0 @@ const cutArrayBeforeValue = (arr, value) => { |
@@ -21,3 +21,3 @@ var __defProp = Object.defineProperty; | ||
import { | ||
deepCloneWithExtend, | ||
deepClone, | ||
exec, | ||
@@ -129,3 +129,3 @@ isArray, | ||
else if (!childElem) | ||
assignChild(deepCloneWithExtend(newChild)); | ||
assignChild(deepClone(newChild)); | ||
else { | ||
@@ -132,0 +132,0 @@ const isSugarChildElem = checkIfSugar(childElem, parent, key); |
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
@@ -17,2 +19,3 @@ var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
import { window } from "./globals.js"; | ||
@@ -88,74 +91,51 @@ import { | ||
}; | ||
const deepCloneExclude = (obj, excludeFrom = []) => { | ||
if (isArray(obj)) { | ||
return obj.map((x) => deepCloneExclude(x, excludeFrom)); | ||
} | ||
const o = {}; | ||
for (const k in obj) { | ||
const hasOwnProperty2 = Object.prototype.hasOwnProperty.call(obj, k); | ||
if (!hasOwnProperty2 || excludeFrom.includes(k) || k.startsWith("__")) | ||
continue; | ||
let v = obj[k]; | ||
if (k === "extend" && isArray(v)) { | ||
v = mergeArrayExclude(v, excludeFrom); | ||
} | ||
if (isArray(v)) { | ||
o[k] = v.map((x) => deepCloneExclude(x, excludeFrom)); | ||
} else if (isObject(v)) { | ||
o[k] = deepCloneExclude(v, excludeFrom); | ||
} else | ||
o[k] = v; | ||
} | ||
return o; | ||
const mergeArrayExclude = (arr, exclude = []) => { | ||
return arr.reduce((acc, curr) => deepMerge(acc, deepClone(curr, { exclude })), {}); | ||
}; | ||
const mergeArrayExclude = (arr, excl = []) => { | ||
return arr.reduce((acc, curr) => deepMerge(acc, deepCloneExclude(curr, excl)), {}); | ||
}; | ||
const deepClone = (obj, exclude = [], cleanUndefined = false, visited = /* @__PURE__ */ new WeakMap()) => { | ||
if (!isObjectLike(obj) || isDOMNode(obj)) | ||
const deepClone = (obj, options = {}) => { | ||
const { | ||
exclude = [], | ||
cleanUndefined = false, | ||
cleanNull = false, | ||
window: targetWindow, | ||
visited = /* @__PURE__ */ new WeakMap(), | ||
handleExtend = false | ||
} = options; | ||
if (!isObjectLike(obj) || isDOMNode(obj)) { | ||
return obj; | ||
if (visited.has(obj)) | ||
} | ||
if (visited.has(obj)) { | ||
return visited.get(obj); | ||
const clone2 = isArray(obj) ? [] : {}; | ||
} | ||
const clone2 = targetWindow ? isArray(obj) ? new targetWindow.Array() : new targetWindow.Object() : isArray(obj) ? [] : {}; | ||
visited.set(obj, clone2); | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key) && !exclude.includes(key)) { | ||
const value = obj[key]; | ||
if (isDOMNode(value)) { | ||
clone2[key] = value; | ||
} else if (key === "extend" && isArray(value)) { | ||
clone2[key] = mergeArray(value, exclude); | ||
} else if (isObjectLike(value)) { | ||
clone2[key] = deepClone(value, exclude, cleanUndefined, visited); | ||
} else { | ||
clone2[key] = value; | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) | ||
continue; | ||
if (exclude.includes(key) || key.startsWith("__") || key === "__proto__") | ||
continue; | ||
const value = obj[key]; | ||
if (cleanUndefined && isUndefined(value) || cleanNull && isNull(value)) | ||
continue; | ||
if (isDOMNode(value)) { | ||
clone2[key] = value; | ||
continue; | ||
} | ||
} | ||
return clone2; | ||
}; | ||
const deepCloneWithExtend = (obj, excludeFrom = ["node"], options = {}, visited = /* @__PURE__ */ new WeakSet()) => { | ||
if (isObjectLike(obj)) { | ||
if (visited.has(obj)) { | ||
return obj; | ||
if (handleExtend && key === "extend" && isArray(value)) { | ||
clone2[key] = mergeArray(value, exclude); | ||
continue; | ||
} | ||
visited.add(obj); | ||
} | ||
const o = options.window ? isArray(obj) ? new options.window.Array([]) : new options.window.Object({}) : isArray(obj) ? [] : {}; | ||
for (const prop in obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, prop)) | ||
if (isFunction(value) && targetWindow) { | ||
clone2[key] = targetWindow.eval("(" + value.toString() + ")"); | ||
continue; | ||
const objProp = obj[prop]; | ||
if (excludeFrom.includes(prop) || prop.startsWith("__") || options.cleanUndefined && isUndefined(objProp) || options.cleanNull && isNull(objProp)) { | ||
continue; | ||
} | ||
if (isObjectLike(objProp)) { | ||
o[prop] = deepCloneWithExtend(objProp, excludeFrom, options, visited); | ||
} else if (isFunction(objProp) && options.window) { | ||
o[prop] = (options.window || window).eval("(" + objProp.toString() + ")"); | ||
if (isObjectLike(value)) { | ||
clone2[key] = deepClone(value, __spreadProps(__spreadValues({}, options), { | ||
visited | ||
})); | ||
} else { | ||
o[prop] = objProp; | ||
clone2[key] = value; | ||
} | ||
} | ||
return o; | ||
return clone2; | ||
}; | ||
@@ -656,4 +636,2 @@ const deepStringify = (obj, stringified = {}) => { | ||
deepClone, | ||
deepCloneExclude, | ||
deepCloneWithExtend, | ||
deepContains, | ||
@@ -660,0 +638,0 @@ deepDestringify, |
180
object.js
@@ -77,110 +77,43 @@ 'use strict' | ||
// Clone anything deeply but excludeFrom keys given in 'excludeFrom' | ||
export const deepCloneExclude = (obj, excludeFrom = []) => { | ||
if (isArray(obj)) { | ||
return obj.map(x => deepCloneExclude(x, excludeFrom)) | ||
} | ||
const o = {} | ||
for (const k in obj) { | ||
const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, k) | ||
if (!hasOwnProperty || excludeFrom.includes(k) || k.startsWith('__')) continue | ||
let v = obj[k] | ||
if (k === 'extend' && isArray(v)) { | ||
v = mergeArrayExclude(v, excludeFrom) | ||
} | ||
if (isArray(v)) { | ||
o[k] = v.map(x => deepCloneExclude(x, excludeFrom)) | ||
} else if (isObject(v)) { | ||
o[k] = deepCloneExclude(v, excludeFrom) | ||
} else o[k] = v | ||
} | ||
return o | ||
} | ||
// Merge array, but exclude keys listed in 'excl'z | ||
export const mergeArrayExclude = (arr, excl = []) => { | ||
return arr.reduce((acc, curr) => deepMerge(acc, deepCloneExclude(curr, excl)), {}) | ||
export const mergeArrayExclude = (arr, exclude = []) => { | ||
return arr.reduce((acc, curr) => deepMerge(acc, deepClone(curr, { exclude })), {}) | ||
} | ||
/** | ||
* Deep cloning of object | ||
* Enhanced deep clone function that combines features from multiple implementations | ||
* @param {any} obj - Object to clone | ||
* @param {Object} options - Configuration options | ||
* @param {string[]} options.exclude - Properties to exclude from cloning | ||
* @param {boolean} options.cleanUndefined - Remove undefined values | ||
* @param {boolean} options.cleanNull - Remove null values | ||
* @param {Window} options.window - Window object for cross-frame cloning | ||
* @param {WeakMap} options.visited - WeakMap for tracking circular references | ||
* @param {boolean} options.handleExtend - Whether to handle 'extend' arrays specially | ||
* @returns {any} Cloned object | ||
*/ | ||
export const deepClone = (obj, exclude = [], cleanUndefined = false, visited = new WeakMap()) => { | ||
// Handle non-object types, null, and ignored types | ||
if (!isObjectLike(obj) || isDOMNode(obj)) return obj | ||
export const deepClone = (obj, options = {}) => { | ||
const { | ||
exclude = [], | ||
cleanUndefined = false, | ||
cleanNull = false, | ||
window: targetWindow, | ||
visited = new WeakMap(), | ||
handleExtend = false | ||
} = options | ||
// Check for circular references | ||
if (visited.has(obj)) return visited.get(obj) | ||
// Create a new object or array | ||
const clone = isArray(obj) ? [] : {} | ||
// Store the clone in the WeakMap to handle circular references | ||
visited.set(obj, clone) | ||
// Iterate over the properties of the object | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key) && !exclude.includes(key)) { | ||
const value = obj[key] | ||
if (isDOMNode(value)) { | ||
// Skip cloning for DOM nodes | ||
clone[key] = value | ||
} else if (key === 'extend' && isArray(value)) { | ||
clone[key] = mergeArray(value, exclude) | ||
} else if (isObjectLike(value)) { | ||
clone[key] = deepClone(value, exclude, cleanUndefined, visited) | ||
} else { | ||
clone[key] = value | ||
} | ||
} | ||
// Handle non-object types and special cases | ||
if (!isObjectLike(obj) || isDOMNode(obj)) { | ||
return obj | ||
} | ||
return clone | ||
} | ||
// export const deepClone = (obj, excludeFrom = [], cleanUndefined = false) => { | ||
// const o = isArray(obj) ? [] : {} | ||
// for (const prop in obj) { | ||
// if (!Object.prototype.hasOwnProperty.call(obj, prop)) continue | ||
// // if (prop === 'node' || prop === 'parent' || prop === 'root' || prop === '__element') { | ||
// // console.warn('recursive clonning is called', obj) | ||
// // continue | ||
// // } | ||
// if (prop === '__proto__') continue | ||
// if (excludeFrom.includes(prop) || prop.startsWith('__')) continue | ||
// let objProp = obj[prop] | ||
// if (cleanUndefined && isUndefined(objProp)) continue | ||
// if (prop === 'extend' && isArray(objProp)) { | ||
// objProp = mergeArray(objProp) | ||
// } | ||
// if (isObjectLike(objProp)) { | ||
// // queueMicrotask(() => { | ||
// o[prop] = deepClone(objProp, excludeFrom, cleanUndefined) | ||
// // }) | ||
// } else o[prop] = objProp | ||
// } | ||
// return o | ||
// } | ||
/** | ||
* Deep cloning of object | ||
*/ | ||
export const deepCloneWithExtend = (obj, excludeFrom = ['node'], options = {}, visited = new WeakSet()) => { | ||
// Check if the value is object-like before trying to track it in visited | ||
if (isObjectLike(obj)) { | ||
if (visited.has(obj)) { | ||
return obj // Return the object if it was already cloned | ||
} | ||
visited.add(obj) // Add to visited set only if it's an object | ||
// Handle circular references | ||
if (visited.has(obj)) { | ||
return visited.get(obj) | ||
} | ||
const o = options.window | ||
// Create appropriate container based on type and window context | ||
const clone = targetWindow | ||
? isArray(obj) | ||
? new options.window.Array([]) | ||
: new options.window.Object({}) | ||
? new targetWindow.Array() | ||
: new targetWindow.Object() | ||
: isArray(obj) | ||
@@ -190,26 +123,47 @@ ? [] | ||
for (const prop in obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, prop)) continue | ||
// Store the clone to handle circular references | ||
visited.set(obj, clone) | ||
const objProp = obj[prop] | ||
// Clone properties | ||
for (const key in obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue | ||
if ( | ||
excludeFrom.includes(prop) || | ||
prop.startsWith('__') || | ||
(options.cleanUndefined && isUndefined(objProp)) || | ||
(options.cleanNull && isNull(objProp)) | ||
) { | ||
// Skip excluded properties | ||
if (exclude.includes(key) || key.startsWith('__') || key === '__proto__') continue | ||
const value = obj[key] | ||
// Skip based on cleanup options | ||
if ((cleanUndefined && isUndefined(value)) || (cleanNull && isNull(value))) continue | ||
// Handle special cases | ||
if (isDOMNode(value)) { | ||
clone[key] = value | ||
continue | ||
} | ||
if (isObjectLike(objProp)) { | ||
o[prop] = deepCloneWithExtend(objProp, excludeFrom, options, visited) | ||
} else if (isFunction(objProp) && options.window) { | ||
o[prop] = (options.window || window).eval('(' + objProp.toString() + ')') | ||
// Handle 'extend' array if enabled | ||
if (handleExtend && key === 'extend' && isArray(value)) { | ||
clone[key] = mergeArray(value, exclude) | ||
continue | ||
} | ||
// Handle functions in cross-frame scenario | ||
if (isFunction(value) && targetWindow) { | ||
clone[key] = targetWindow.eval('(' + value.toString() + ')') | ||
continue | ||
} | ||
// Recursively clone objects | ||
if (isObjectLike(value)) { | ||
clone[key] = deepClone(value, { | ||
...options, | ||
visited | ||
}) | ||
} else { | ||
o[prop] = objProp | ||
clone[key] = value | ||
} | ||
} | ||
return o | ||
return clone | ||
} | ||
@@ -216,0 +170,0 @@ |
{ | ||
"name": "@domql/utils", | ||
"version": "2.5.159", | ||
"version": "2.5.161", | ||
"license": "MIT", | ||
@@ -28,3 +28,3 @@ "type": "module", | ||
}, | ||
"gitHead": "7595b1077d3096c3ee7b1da5eef02aa9248f2ed6", | ||
"gitHead": "39a7af3d77c6b0257d5e1a0d6110850926d83c19", | ||
"devDependencies": { | ||
@@ -31,0 +31,0 @@ "@babel/core": "^7.12.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
153251
4875