Last active
September 30, 2018 18:00
-
-
Save tchon/f1e74a240f57b99af01555650deb1b3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findUids(node) { | |
let depth = 0; | |
const MAX_DEPTH = 10; | |
const uids = []; | |
const table = {}; | |
(function traverse(node) { | |
if (typeof node !== "object") { return; } // only permit objects | |
if (depth > MAX_DEPTH) { | |
console.error(new Error(`Exception: Tree to Deep! depth: ${depth}`)); | |
return; | |
} | |
depth++; | |
if (Array.isArray(node)) { // traverse array entries first | |
for (let i = 0; i < node.length; i++) { | |
traverse(node[i]), depth--; | |
} | |
return; | |
} | |
const keys = Object.keys(node); | |
for (let i = 0; i < keys.length; i++) { | |
const key = keys[i]; | |
const val = node[key]; | |
if (key === "uid" && typeof val === "string" && val.length) { | |
uids.push(val); | |
table[val] = node; | |
} | |
if (Array.isArray(val) || key === "configs") { | |
traverse(val), depth--; | |
} | |
} | |
})(node); | |
return { uids, table }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment