Skip to content

Instantly share code, notes, and snippets.

@psqq
Last active March 29, 2020 09:42
Show Gist options
  • Save psqq/14b0e71326d41620ca3957e850f4b05f to your computer and use it in GitHub Desktop.
Save psqq/14b0e71326d41620ca3957e850f4b05f to your computer and use it in GitHub Desktop.
Id generator

Id generator.

Install

yarn add gist:14b0e71326d41620ca3957e850f4b05f

Use

import id from '14b0e71326d41620ca3957e850f4b05f';

console.log(id(), id()); // k8cut1lh k8cut1lH-1r
var prevTime = 0;
var counter = 0;
function id() {
var t = Date.now();
var s = t.toString(36);
if (t == prevTime) {
counter += Math.floor(Math.random() * 100) + 1;
s += '-' + counter.toString(36)
} else {
counter = 0;
}
prevTime = t;
var a = [];
for (let i = 0; i < s.length; i++) {
if (Math.random() < 0.5) {
a.push(s[i].toLowerCase());
} else {
a.push(s[i].toUpperCase());
}
}
return a.join('');
}
export default id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment