Skip to content

Instantly share code, notes, and snippets.

@kf6kjg
Created July 30, 2020 19:56
Show Gist options
  • Save kf6kjg/3638a83b8efb34cee88210334eb187e5 to your computer and use it in GitHub Desktop.
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.
/**
* 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