Skip to content

Instantly share code, notes, and snippets.

@RuNpiXelruN
Created August 12, 2022 02:40
Show Gist options
  • Save RuNpiXelruN/96402ef2c807529e2df7cd19e7a2d11a to your computer and use it in GitHub Desktop.
Save RuNpiXelruN/96402ef2c807529e2df7cd19e7a2d11a to your computer and use it in GitHub Desktop.
Depth first search
export function getActionBox(boxes: any[], id: number) {
let found = null
let i = boxes.length
while (i--) {
if (boxes[i].id.self === id) {
found = boxes[i]
return found
}
let j = boxes[i].children.length
while (j--) {
found = depthFirstBoxSearch(boxes[i].children[j], id)
}
}
return found
}
function depthFirstBoxSearch(boxObj: AutomationBox, id: number): AutomationBox | null {
if (boxObj.id.self === id) {
return boxObj
}
if (!!boxObj.children.length) {
let i = boxObj.children.length
while (i--) {
let found = depthFirstBoxSearch(boxObj.children[i], id)
if (found) return found
}
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment