Last active
April 3, 2020 14:17
-
-
Save aakashlpin/90ec509d4b2520388e52f27a94b832aa to your computer and use it in GitHub Desktop.
run gist from npx
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
{ | |
"name": "js-from-terminal-with-npx", | |
"version": "1.0.0", | |
"bin": "./unique_id.js", | |
"dependencies": { | |
"yargs": "^15.3.1" | |
} | |
} |
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 | |
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