Simple UID generation with CoffeeScript
This is an overly simple function to generate unique ID's using CoffeeScript based on the current time.
generateId = ->
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
today = new Date()
result = today.valueOf().toString 16
result += chars.substr Math.floor(Math.random() * chars.length), 1
result += chars.substr Math.floor(Math.random() * chars.length), 1
return result
The two last random characters are appended to minimize collisions.
I'm using it on Counting.to and on the Zalando System App.
Written by Igor Ramadas
Related protips
2 Responses
There are some code-golf snippets for UUIDs in JavaScript that you might find interesting:
https://gist.github.com/jed/982883
https://gist.github.com/LeverOne/1308368
over 1 year ago
·
Hi Andrew. I knew these ones, but on my specific case I needed something shorter and that could be easily associated to a timestamp. But thanks anyways :-)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Id
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#