Skip to content

Instantly share code, notes, and snippets.

@erhhung
Last active May 27, 2021 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erhhung/59e3cd7a600fd3a5f860fb1d47a30cb2 to your computer and use it in GitHub Desktop.
Save erhhung/59e3cd7a600fd3a5f860fb1d47a30cb2 to your computer and use it in GitHub Desktop.
Make 30-char typed object ID
#!/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