Skip to content

Instantly share code, notes, and snippets.

@lcaballero
Created September 30, 2014 04:56
Show Gist options
  • Save lcaballero/be998c27207f68d74834 to your computer and use it in GitHub Desktop.
Save lcaballero/be998c27207f68d74834 to your computer and use it in GitHub Desktop.
A uuid mixin for Lodash.
_.mixin
# Better than random number, this should not allow for collisions.
# Code was minimally modified from
# [stackoverflow](http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript),
# demonstrated at [jsfiddle](http://jsfiddle.net/briguy37/2MVFd/)
uuid : ->
now = Date.now()
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace /[xy]/g, (c) ->
r = (now + Math.random() * 16) % 16 | 0
now = Math.floor now / 16
((if c is "x" then r else (r & 0x7 | 0x8))).toString 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment