Created
July 30, 2020 19:56
-
-
Save kf6kjg/3638a83b8efb34cee88210334eb187e5 to your computer and use it in GitHub Desktop.
Recursively converts the given value to a similar structure but with the values replaced by their types.
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
/** | |
* Recursively converts the given value to a similar structure but with the values replaced by their types. | |
* | |
* Useful for debugging datastructures you can't dig into via a debugger and that you don't already know the types of. | |
*/ | |
function objToTypeReadout(key: string, value: any): { [key: string]: any } { | |
if (Array.isArray(value)) { | |
return { | |
[key]: typeof value, | |
value: value.map((v, i) => objToTypeReadout(i.toFixed(0), v)), | |
}; | |
} | |
if (typeof value === "object") { | |
if (value === null) { | |
return { [key]: null }; | |
} | |
return { | |
[key]: typeof value, | |
value: Object.entries(value).map(([k, v]) => objToTypeReadout(k, v)), | |
}; | |
} | |
return { [key]: typeof value }; | |
} | |
console.log("mySql.query", JSON.stringify(objToTypeReadout("results", results), undefined, "\t")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment