Skip to content

Instantly share code, notes, and snippets.

@aakashlpin
Last active April 3, 2020 14:17
Show Gist options
  • Save aakashlpin/90ec509d4b2520388e52f27a94b832aa to your computer and use it in GitHub Desktop.
Save aakashlpin/90ec509d4b2520388e52f27a94b832aa to your computer and use it in GitHub Desktop.
run gist from npx
{
"name": "js-from-terminal-with-npx",
"version": "1.0.0",
"bin": "./unique_id.js",
"dependencies": {
"yargs": "^15.3.1"
}
}
#!/usr/bin/env node
const args = require('yargs').argv;
function createUniqueID() {
let dt = new Date().getTime();
const uuid = 'xxyxxxxxxyxxxxxyxxxx'.replace(/[xy]/g, (c) => {
const r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
return uuid;
}
function generateUniqueId(prefix) {
return `${prefix}${createUniqueID()}`;
}
console.log(generateUniqueId(args.prefix));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment