Created
October 18, 2023 16:13
-
-
Save caugner/c8981f60458a9f7a9efa5991f14fbb62 to your computer and use it in GitHub Desktop.
Record types of variables (e.g. for untyped function parameters)
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 recordVariableTypes(name: string, variables: Record<string, any>) { | |
const types = Object.entries(variables).reduce( | |
(obj, [key, value]) => ({ ...obj, [key]: typeOf(value) }), | |
{} | |
); | |
recordTypes(name, types); | |
} | |
function recordTypes(name: string, types: Record<string, string>) { | |
const path = `types_${name}.json`; | |
let current; | |
if (fs.existsSync(path)) { | |
current = JSON.parse(fs.readFileSync(path, "utf-8")); | |
} else { | |
current = {}; | |
} | |
Object.entries(types).forEach(([key, value]) => { | |
current[key] ??= {}; | |
current[key][value] ??= 0; | |
current[key][value] += 1; | |
}); | |
fs.writeFileSync(path, JSON.stringify(current)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment