Skip to content

Commit

Permalink
Make uuid v4 rfc4122 compliant (denoland/std#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 authored and ry committed Sep 11, 2019
1 parent 8d35590 commit 08087e9
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions uuid/v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ export function validate(id: string): boolean {
}

export default function generate(): string {
return "00000000-0000-4000-8000-000000000000".replace(
/[0]/g,
(): string =>
// random integer from 0 to 15 as a hex digit.
(crypto.getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
const rnds = crypto.getRandomValues(new Uint8Array(16));

rnds[6] = (rnds[6] & 0x0f) | 0x40; // Version 4
rnds[8] = (rnds[8] & 0x3f) | 0x80; // Variant 10

const bits: string[] = [...rnds].map(
(bit): string => {
const s: string = bit.toString(16);
return bit < 0x10 ? "0" + s : s;
}
);
return [
...bits.slice(0, 4),
"-",
...bits.slice(4, 6),
"-",
...bits.slice(6, 8),
"-",
...bits.slice(8, 10),
"-",
...bits.slice(10)
].join("");
}

0 comments on commit 08087e9

Please sign in to comment.