Skip to content

Instantly share code, notes, and snippets.

@caugner
Created October 18, 2023 16:13
Show Gist options
  • Save caugner/c8981f60458a9f7a9efa5991f14fbb62 to your computer and use it in GitHub Desktop.
Save caugner/c8981f60458a9f7a9efa5991f14fbb62 to your computer and use it in GitHub Desktop.
Record types of variables (e.g. for untyped function parameters)
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