Last Updated: February 15, 2019
·
1.576K
· igoramadas

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.

2 Responses
Add your response

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 ·