Skip to content

Instantly share code, notes, and snippets.

@jussi-kalliokoski
Forked from 140bytes/LICENSE.txt
Last active March 8, 2018 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jussi-kalliokoski/5874116 to your computer and use it in GitHub Desktop.
Save jussi-kalliokoski/5874116 to your computer and use it in GitHub Desktop.
A 140byt.es UUIDv4 generator.

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function (
n // this is declared here to avoid a comma in the replacing function,
// although now that we're using arrow functions we couldn't have it
// any other way anyway
) // note the single expression function body (Firefox only)
"xxxxxxxx-xxxx-3xxx-1xxx-xxxxxxxxxxxx" // we use a simple template to generate the UUIDv4
.replace(/x|1/g, // replace x's and 1s in the template
x=> // ES6 arrow function! note the single argument, so we don't need parenthesis
( // wrap everything in parenthesis so that we can avoid using return and curlies
// without the parenthesis, the part after the comma would be treated as a third
// argument for replace() of course
(n=Math.random()) // assign a random number to a variable so that we
// don't need to type it twice
, // comma instead of semicolon to keep it as one expression
(~~( // bitwise floor
+x ? // if converting the character to a number yields a truthy value, i.e. 1
n * 4 + 8 : // get number where 12 > number >= 8
n * 16 // get a number where 16 > number >= 0
))
.toString(16) // Get a hexadecimal representation of the number
)
)
function(n)"xxxxxxxx-xxxx-3xxx-1xxx-xxxxxxxxxxxx".replace(/x|1/g,x=>((n=Math.random()),(~~(+x?n*4+8:n*16)).toString(16)))
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jussi Kalliokoski <jussi.kalliokoski@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "ShortUuidV4",
"description": "A short UUIDv4 generator.",
"keywords": [
"uuid",
"crypto"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>An UUIDv4</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(n)"xxxxxxxx-xxxx-3xxx-1xxx-xxxxxxxxxxxx".replace(/x|1/g,x=>((n=Math.random()),(~~(+x?n*4+8:n*16)).toString(16)));
document.getElementById( "ret" ).innerHTML = myFunction()
</script>
@jussi-kalliokoski
Copy link
Author

An even shorter version, based on prior work: https://gist.github.com/jed/982883#comment-852657

@atk
Copy link

atk commented Jun 27, 2013

Alas, it lacks IE compatibility.

@jussi-kalliokoski
Copy link
Author

Alas, it lacks IE compatibility.

You don't say? :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment