The Daily WTF
Most of us, when generating a UUID, will reach for a library to do it. Even a UUIDv4, which is just a random number, presents challenges: doing randomness correctly is hard, and certain bits within the UUID are reserved for metadata about what kind of UUID we’re generating.
But Gretchen‘s co-worker didn’t reach for a library. What they did reach for was… regular expressions?
function uuidv4() { return “xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx”.replace(/[xy]/g, function (c) { var r = (Math.random() * 16) | 0, v = c == “x” ? r : (r & 0x3) | 0x8; return v.toString(16); }); }
At a glance, this appears to be a riff on common answers on Stack Overflow. I won’t pick on this code for not using crypto.randomUUID, the browser function for doing this, as that function only started showing up in browsers in 2021. But using a format string and filling it with random
To read the full article click on the 'post' link at the top.