-
-
Save erhhung/59e3cd7a600fd3a5f860fb1d47a30cb2 to your computer and use it in GitHub Desktop.
Make 30-char typed object ID
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
#!/usr/bin/env node | |
// | |
// mkid - Make Object ID | |
// | |
// Usage: mkid [type|""] | |
// type: 3-char type | |
// | |
// Requires global NPM modules: | |
// | |
// uuid^8.3.2 | |
// uuid-encoder^1.2.0 | |
// | |
// Author: Erhhung Yuan <erhhung.yuan@intel.com> | |
const path = require('path'); | |
const uuid = require('uuid').v4(); | |
const UuidEncoder = require('uuid-encoder'); | |
const uuidEncoder = new UuidEncoder('base32'); | |
const [, bin, type] = process.argv; | |
if (type === undefined) { | |
console.log(`Usage: ${path.basename(bin)} [type|""]`); | |
console.log(' type: 3-char type'); | |
process.exit(); | |
} | |
if (type && !/^[a-z\d]{3}$/.test(type)) { | |
console.error('Invalid type!'); | |
process.exit(1); | |
} | |
let id = uuidEncoder.encode(uuid); | |
if (id.length < 26) { | |
id += id.slice(0, 26-id.length); | |
} | |
if (type) { | |
id += `.${type}`; | |
} | |
console.log(id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment