Skip to content

Instantly share code, notes, and snippets.

@bsitruk
Last active August 25, 2022 08:09
Show Gist options
  • Save bsitruk/1a6858cb5e90e79103cd24d37830321d to your computer and use it in GitHub Desktop.
Save bsitruk/1a6858cb5e90e79103cd24d37830321d to your computer and use it in GitHub Desktop.
ZX Script to build TypeScript Pick type.
#!/usr/bin/env zx
# usage
#./keys-to-enum.mjs "{ 220ms  Thu Aug 25 11:04:52 2022
# attributeName,
# fieldName,
# fullyQualifiedName,
# attributeType,
# scannerTypeGroup,
# }: GetRecordFindingsDto"
# Output: {attributeName,fieldName,fullyQualifiedName,attributeType,scannerTypeGroup,}: Pick<GetRecordFindingsDto, 'attributeName'|'fieldName'|'fullyQualifiedName'|'attributeType'|'scannerTypeGroup'>
let content = process.argv.at(-1);
let cleanedContent = content.replace(/\n\s+/g, "");
let [keys, type] = cleanedContent.split(":");
type = type.trim();
let union = keys
.slice(1, -1)
.replace(/,$/, "")
.split(",")
.map((s) => `'${s}'`)
.join("|");
let pick = `Pick<${type}, ${union}>`;
let final = `${keys}: ${pick}`;
echo`${final}`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment