@domql/utils
Advanced tools
Comparing version 2.5.185 to 2.5.187
@@ -40,2 +40,3 @@ "use strict"; | ||
getInObjectByPath: () => getInObjectByPath, | ||
hasFunction: () => hasFunction, | ||
hasOwnProperty: () => hasOwnProperty, | ||
@@ -302,2 +303,26 @@ isCyclic: () => isCyclic, | ||
}; | ||
const hasFunction = (str) => { | ||
if (!str) | ||
return false; | ||
const trimmed = str.trim().replace(/\n\s*/g, " ").trim(); | ||
if (trimmed === "") | ||
return false; | ||
if (trimmed === "{}") | ||
return false; | ||
if (trimmed === "[]") | ||
return false; | ||
const patterns = [ | ||
/^\(\s*\{[^}]*\}\s*\)\s*=>/, | ||
/^(\([^)]*\)|[^=]*)\s*=>/, | ||
/^function[\s(]/, | ||
/^async\s+/, | ||
/^\(\s*function/, | ||
/^[a-zA-Z_$][a-zA-Z0-9_$]*\s*=>/ | ||
]; | ||
const isFunction2 = patterns.some((pattern) => pattern.test(trimmed)); | ||
const isObjectLiteral = trimmed.startsWith("{") && !trimmed.includes("=>"); | ||
const isArrayLiteral = trimmed.startsWith("["); | ||
const isJSONLike = /^["[{]/.test(trimmed) && !trimmed.includes("=>"); | ||
return isFunction2 && !isObjectLiteral && !isArrayLiteral && !isJSONLike; | ||
}; | ||
const deepDestringify = (obj, destringified = {}) => { | ||
@@ -310,3 +335,3 @@ for (const prop in obj) { | ||
if ((0, import_types.isString)(objProp)) { | ||
if ((objProp.includes("(){") || objProp.includes("() {") || objProp.includes("=>") || objProp.startsWith("()") || objProp.startsWith("async") || objProp.startsWith("function") || objProp.startsWith("(")) && !objProp.startsWith("{") && !objProp.startsWith("[")) { | ||
if (hasFunction(objProp)) { | ||
try { | ||
@@ -326,3 +351,3 @@ const evalProp = import_globals.window.eval(`(${objProp})`); | ||
if ((0, import_types.isString)(arrProp)) { | ||
if (arrProp.includes("=>") || arrProp.includes("function") || arrProp.startsWith("(")) { | ||
if (hasFunction(arrProp)) { | ||
try { | ||
@@ -329,0 +354,0 @@ const evalProp = import_globals.window.eval(`(${arrProp})`); |
@@ -270,2 +270,26 @@ var __defProp = Object.defineProperty; | ||
}; | ||
const hasFunction = (str) => { | ||
if (!str) | ||
return false; | ||
const trimmed = str.trim().replace(/\n\s*/g, " ").trim(); | ||
if (trimmed === "") | ||
return false; | ||
if (trimmed === "{}") | ||
return false; | ||
if (trimmed === "[]") | ||
return false; | ||
const patterns = [ | ||
/^\(\s*\{[^}]*\}\s*\)\s*=>/, | ||
/^(\([^)]*\)|[^=]*)\s*=>/, | ||
/^function[\s(]/, | ||
/^async\s+/, | ||
/^\(\s*function/, | ||
/^[a-zA-Z_$][a-zA-Z0-9_$]*\s*=>/ | ||
]; | ||
const isFunction2 = patterns.some((pattern) => pattern.test(trimmed)); | ||
const isObjectLiteral = trimmed.startsWith("{") && !trimmed.includes("=>"); | ||
const isArrayLiteral = trimmed.startsWith("["); | ||
const isJSONLike = /^["[{]/.test(trimmed) && !trimmed.includes("=>"); | ||
return isFunction2 && !isObjectLiteral && !isArrayLiteral && !isJSONLike; | ||
}; | ||
const deepDestringify = (obj, destringified = {}) => { | ||
@@ -278,3 +302,3 @@ for (const prop in obj) { | ||
if (isString(objProp)) { | ||
if ((objProp.includes("(){") || objProp.includes("() {") || objProp.includes("=>") || objProp.startsWith("()") || objProp.startsWith("async") || objProp.startsWith("function") || objProp.startsWith("(")) && !objProp.startsWith("{") && !objProp.startsWith("[")) { | ||
if (hasFunction(objProp)) { | ||
try { | ||
@@ -294,3 +318,3 @@ const evalProp = window.eval(`(${objProp})`); | ||
if (isString(arrProp)) { | ||
if (arrProp.includes("=>") || arrProp.includes("function") || arrProp.startsWith("(")) { | ||
if (hasFunction(arrProp)) { | ||
try { | ||
@@ -686,2 +710,3 @@ const evalProp = window.eval(`(${arrProp})`); | ||
getInObjectByPath, | ||
hasFunction, | ||
hasOwnProperty, | ||
@@ -688,0 +713,0 @@ isCyclic, |
@@ -314,5 +314,28 @@ 'use strict' | ||
/** | ||
* Detringify object | ||
*/ | ||
export const hasFunction = (str) => { | ||
if (!str) return false | ||
const trimmed = str.trim().replace(/\n\s*/g, ' ').trim() | ||
if (trimmed === '') return false | ||
if (trimmed === '{}') return false | ||
if (trimmed === '[]') return false | ||
const patterns = [ | ||
/^\(\s*\{[^}]*\}\s*\)\s*=>/, | ||
/^(\([^)]*\)|[^=]*)\s*=>/, | ||
/^function[\s(]/, | ||
/^async\s+/, | ||
/^\(\s*function/, | ||
/^[a-zA-Z_$][a-zA-Z0-9_$]*\s*=>/ | ||
] | ||
const isFunction = patterns.some(pattern => pattern.test(trimmed)) | ||
const isObjectLiteral = trimmed.startsWith('{') && !trimmed.includes('=>') | ||
const isArrayLiteral = trimmed.startsWith('[') | ||
const isJSONLike = /^["[{]/.test(trimmed) && !trimmed.includes('=>') | ||
return isFunction && !isObjectLiteral && !isArrayLiteral && !isJSONLike | ||
} | ||
export const deepDestringify = (obj, destringified = {}) => { | ||
@@ -322,19 +345,13 @@ for (const prop in obj) { | ||
if (!hasOwnProperty) continue | ||
const objProp = obj[prop] | ||
if (isString(objProp)) { | ||
if (( | ||
objProp.includes('(){') || | ||
objProp.includes('() {') || | ||
objProp.includes('=>') || | ||
objProp.startsWith('()') || | ||
objProp.startsWith('async') || | ||
objProp.startsWith('function') || | ||
objProp.startsWith('(') | ||
) && | ||
!objProp.startsWith('{') && !objProp.startsWith('[') | ||
) { | ||
if (hasFunction(objProp)) { | ||
try { | ||
const evalProp = window.eval(`(${objProp})`) | ||
destringified[prop] = evalProp | ||
} catch (e) { if (e) destringified[prop] = objProp } | ||
} catch (e) { | ||
if (e) destringified[prop] = objProp | ||
} | ||
} else { | ||
@@ -347,7 +364,9 @@ destringified[prop] = objProp | ||
if (isString(arrProp)) { | ||
if (arrProp.includes('=>') || arrProp.includes('function') || arrProp.startsWith('(')) { | ||
if (hasFunction(arrProp)) { | ||
try { | ||
const evalProp = window.eval(`(${arrProp})`) // use parentheses to convert string to function expression | ||
const evalProp = window.eval(`(${arrProp})`) | ||
destringified[prop].push(evalProp) | ||
} catch (e) { if (e) destringified[prop].push(arrProp) } | ||
} catch (e) { | ||
if (e) destringified[prop].push(arrProp) | ||
} | ||
} else { | ||
@@ -354,0 +373,0 @@ destringified[prop].push(arrProp) |
{ | ||
"name": "@domql/utils", | ||
"version": "2.5.185", | ||
"version": "2.5.187", | ||
"license": "MIT", | ||
@@ -27,3 +27,3 @@ "type": "module", | ||
}, | ||
"gitHead": "ccab034f188b6a1f55f741c0b29a9d3b0c9bee6f", | ||
"gitHead": "c150bfbbdd51b19d25c93f10334d54175cea9d1d", | ||
"devDependencies": { | ||
@@ -30,0 +30,0 @@ "@babel/core": "^7.12.0" |
160194
5112