Skip to content

Instantly share code, notes, and snippets.

@lap00zza
Last active October 6, 2018 17:52
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 lap00zza/28a72ec5496914a756fac3479a7a8a03 to your computer and use it in GitHub Desktop.
Save lap00zza/28a72ec5496914a756fac3479a7a8a03 to your computer and use it in GitHub Desktop.
Quick 'n' dirty id generator
const generateId = (len=16) => {
const allowedRanges = [[48, 57], [65,90], [97, 122]];
let str = "";
let expand = allowedRanges.reduce((acc, each) => {
let [start, end] = each;
let nums = [];
for(let i = start; i <= end; i++) {
nums.push(i);
};
return acc.concat(nums);
}, []);
for (let i = 0; i < len; i++) {
str += String.fromCharCode(
expand[Math.floor(Math.random() * expand.length)]
);
}
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment