Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove empty array elements to reduce scope transfer size #570

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading