Last active
August 19, 2021 16:30
-
-
Save su8ru/7239f46039fca945139d03087421b536 to your computer and use it in GitHub Desktop.
generate guest ids
This file contains hidden or 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
import yargs from "yargs"; | |
import crypto from "crypto"; | |
import fs from "fs"; | |
const argv = yargs(process.argv) | |
.options({ | |
count: { | |
type: "number", | |
demandOption: true, | |
description: "Number of IDs to be generated", | |
}, | |
prefix: { | |
type: "string", | |
demandOption: true, | |
description: "Prefix of ID (ex. GB", | |
}, | |
length: { | |
type: "number", | |
default: 4, | |
description: "Random string length (without prefix)", | |
}, | |
spare: { | |
type: "boolean", | |
default: false, | |
description: "Whether to generate spares for 00-99", | |
}, | |
}) | |
.parseSync(); | |
const generateRandomString = (length: number): string => | |
crypto.randomBytes(length / 2).toString("hex"); | |
const addCheckSum = (str: string): string => { | |
const digits: number[] = []; | |
for (let i = 0; i < str.length; i++) { | |
digits.push(parseInt(str[i], 16)); | |
} | |
const sum = digits.reduce( | |
(prev, curr, index) => prev + curr * (index % 2 ? 3 : 1), | |
0 | |
); | |
return str + (sum % 0x10).toString(16); | |
}; | |
const set = new Set<string>(); | |
const spareArr = Array<string>(100).fill(""); | |
do { | |
const str = addCheckSum(generateRandomString(argv.length)); | |
if (str.charAt(0) !== "f") set.add(str); | |
} while (set.size < argv.count); | |
const text = | |
Array.from(set).reduce((prev, curr) => { | |
return prev + `${argv.prefix}-${curr}\n`; | |
}, "") + | |
(argv.spare | |
? spareArr.reduce( | |
(prev, _, index) => | |
prev + | |
`${argv.prefix}-${addCheckSum( | |
`ff${index.toString().padStart(2, "0")}` | |
)}\n`, | |
"" | |
) | |
: ""); | |
fs.writeFile("out.txt", text, (err) => { | |
if (err) console.error(err); | |
else console.log("finish!"); | |
}); |
This file contains hidden or 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
{ | |
"name": "scripts", | |
"version": "1.0.0", | |
"license": "MIT", | |
"private": true, | |
"devDependencies": { | |
"@types/node": "^16.6.2", | |
"@types/yargs": "^17.0.2", | |
"prettier": "^2.3.2", | |
"ts-node": "^10.2.1", | |
"typescript": "^4.3.5", | |
"yargs": "^17.1.1" | |
} | |
} |
This file contains hidden or 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
import fs from "fs"; | |
const addCheckSum = (str: string): string => { | |
const digits: number[] = []; | |
for (let i = 0; i < str.length; i++) { | |
digits.push(parseInt(str[i], 16)); | |
} | |
const sum = digits.reduce( | |
(prev, curr, index) => prev + curr * (index % 2 ? 3 : 1), | |
0 | |
); | |
return str + (sum % 0x10).toString(16); | |
}; | |
const yearArr = Array<string>(6).fill(""); | |
const classArr = Array<string>(7).fill(""); | |
const numArr = Array<string>(50).fill(""); | |
const text = yearArr.reduce( | |
(yprev, _, _year) => | |
yprev + | |
classArr.reduce( | |
(cprev, _, _class) => | |
cprev + | |
numArr.reduce( | |
(nprev, _, _num) => | |
nprev + | |
`SG-${addCheckSum( | |
`${_year + 1}${_class + 1}${(_num + 1) | |
.toString() | |
.padStart(2, "0")}` | |
)}\n`, | |
"" | |
), | |
"" | |
), | |
"" | |
); | |
fs.writeFile("out.txt", text, (err) => { | |
if (err) console.error(err); | |
else console.log("finish!"); | |
}); |
This file contains hidden or 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
Show hidden characters
{ | |
"compilerOptions": { | |
"baseUrl": ".", | |
"target": "es6", | |
"lib": [ | |
"dom", | |
"dom.iterable", | |
"esnext" | |
], | |
"module": "CommonJS", | |
"allowJs": true, | |
"skipLibCheck": true, | |
"strict": true, | |
"forceConsistentCasingInFileNames": true, | |
"noEmit": true, | |
"esModuleInterop": true, | |
"moduleResolution": "node", | |
"resolveJsonModule": true, | |
"isolatedModules": true, | |
"jsx": "react-jsx", | |
"allowSyntheticDefaultImports": true, | |
"noFallthroughCasesInSwitch": true | |
}, | |
"exclude": [ | |
"node_modules" | |
], | |
"include": [ | |
"**/*.ts", | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment