Skip to content

Instantly share code, notes, and snippets.

@pombadev
Last active December 23, 2017 16:46
Show Gist options
  • Save pombadev/310d61358173601b86490c23f9c10514 to your computer and use it in GitHub Desktop.
Save pombadev/310d61358173601b86490c23f9c10514 to your computer and use it in GitHub Desktop.
basic non-uuid uuid generator without going crazy!
// with map
[...Array(5)].map((elm, idx) => {
switch(idx) {
case 0:
return Math.random().toString(36).slice(2, 8)
case 4:
return Math.random().toString(36).slice(2, 14)
default:
return Math.random().toString(36).slice(2, 6)
}
}).join('-')
// with reduce
[...Array(5)].reduce((total, elm, idx) => {
if (idx === 0) {
total = Math.random().toString(36).slice(2, 8) + '-'
} else if (idx === 4) {
total += Math.random().toString(36).slice(2, 14)
} else {
total += Math.random().toString(36).slice(2, 6) + '-'
}
return total
}, '')
// returns "ahxiz7-xzec-xn2c-8di9-btlq6e3k9wg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment