Created
August 25, 2012 23:07
-
-
Save amsul/3472010 to your computer and use it in GitHub Desktop.
create a unique id with coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniqueId = (length=8) -> | |
id = "" | |
id += Math.random().toString(36).substr(2) while id.length < length | |
id.substr 0, length | |
uniqueId() # => n5yjla3b | |
uniqueId(2) # => 0d | |
uniqueId(20) # => ox9eo7rt3ej0pb9kqlke | |
uniqueId(40) # => xu2vo4xjn4g0t3xr74zmndshrqlivn291d584alj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
taken from: http://coffeescriptcookbook.com/chapters/strings/generating-a-unique-id