Created
November 30, 2023 10:20
-
-
Save 4lun/cafe916f77a1b1b150badffe35a80a0d to your computer and use it in GitHub Desktop.
Generate a quick and dirty type "blueprint" of a JS object/array/variable
This file contains 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 toBlueprint(obj) { | |
if (!obj || typeof obj !== 'object') { | |
return obj; | |
} | |
if(Array.isArray(obj)) { | |
return obj.map(toBlueprint) // nit: won't unify common props | |
} | |
return Object.keys(obj).reduce((acc, key) => { | |
let blueprint = typeof obj[key]; | |
if (blueprint === "object" && blueprint) { | |
blueprint = toBlueprint(obj[key]) | |
} | |
acc[key] = blueprint; | |
return acc; | |
}, | |
{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment