Skip to content

Commit

Permalink
fix: remove empty array elements to reduce to reduce scope transfer s…
Browse files Browse the repository at this point in the history
…ize (#570)

Happy 🎄 everyone!
  • Loading branch information
Siolto committed Dec 21, 2023
1 parent d302620 commit f2843c1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
7 changes: 5 additions & 2 deletions client-side-js/executeControlMethod.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ async function executeControlMethod(webElement, methodName, browserInstance, arg
const collapsedAndNonCyclic = JSON.parse(
JSON.stringify(collapsed, window.wdi5.getCircularReplacer())
)
// remove all empty Array elements, inlcuding private keys (starting with "_")
const semanticCleanedElements = window.wdi5.removeEmptyElements(collapsedAndNonCyclic)

done({
status: 0,
object: collapsedAndNonCyclic,
object: semanticCleanedElements,
returnType: "object",
aProtoFunctions: aProtoFunctions,
uuid: uuid,
nonCircularResultObject: collapsedAndNonCyclic
nonCircularResultObject: semanticCleanedElements
})
} else if (
typeof result === "object" &&
Expand Down
4 changes: 3 additions & 1 deletion client-side-js/executeObjectMethod.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ async function clientSide_executeObjectMethod(uuid, methodName, args) {
const collapsedAndNonCyclic = JSON.parse(
JSON.stringify(result, window.wdi5.getCircularReplacer())
)
// remove all empty Array elements, inlcuding private keys (starting with "_")
const semanticCleanedElements = window.wdi5.removeEmptyElements(collapsedAndNonCyclic)

done({
status: 0,
object: collapsedAndNonCyclic,
object: semanticCleanedElements,
uuid: uuid,
returnType: "object",
aProtoFunctions: aProtoFunctions
Expand Down
5 changes: 4 additions & 1 deletion client-side-js/getObject.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ async function clientSide_getObject(uuid) {

const collapsedAndNonCyclic = JSON.parse(JSON.stringify(object, window.wdi5.getCircularReplacer()))

// remove all empty Array elements, inlcuding private keys (starting with "_")
const semanticCleanedElements = window.wdi5.removeEmptyElements(collapsedAndNonCyclic)

done({
status: 0,
uuid: uuid,
aProtoFunctions: aProtoFunctions,
className: className,
object: collapsedAndNonCyclic
object: semanticCleanedElements
})
},
window.wdi5.errorHandling.bind(this, done)
Expand Down
30 changes: 30 additions & 0 deletions client-side-js/injectUI5.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,36 @@ async function clientSide_injectUI5(config, waitForUI5Timeout, browserInstance)
}
}

/**
* removes all empty collection members from an object,
* e.g. empty, null, or undefined array elements
*
* @param {object} obj
* @returns {object} obj without empty collection members
*/
window.wdi5.removeEmptyElements = (obj, i = 0) => {
for (let key in obj) {
if (obj[key] === null || key.startsWith("_")) {
delete obj[key]
} else if (Array.isArray(obj[key])) {
obj[key] = obj[key].filter(
(element) =>
element !== null &&
element !== undefined &&
element !== "" &&
Object.keys(element).length > 0
)
if (obj[key].length > 0) {
i++
window.wdi5.removeEmptyElements(obj[key], i)
}
} else if (typeof obj[key] === "object") {
i++
window.wdi5.removeEmptyElements(obj[key], i)
}
}
return obj
}
/**
* if parameter is JS primitive type
* returns {boolean}
Expand Down

0 comments on commit f2843c1

Please sign in to comment.