A universally unique identifier, or UUID, is a 128-bit number,
usually represented as 32 hexidecimal digits, in the format
123e4567-e89b-42d3-a456-426655440000
. This package provides a UUID type, and
functions for reading, creating, randomly generating, inspecting and formatting
UUIDs.
There are several versions and variants of UUID. This package supports the reading of all variant 1 UUIDs (versions 1-5, those outlined in RFC 4122), and the creation of versions 3, 4 and 5. This covers the vast majority of UUIDs in use today.
import UUID exposing (UUID)
import Random
Random.initialSeed 12345
|> Random.step (Random.list 3 UUID.generator)
|> Tuple.first
|> List.map UUID.toString
--> [ "88c973e3-f83f-4360-a320-d8844c365130"
--> , "78bc3402-e662-4d59-bac5-914be6425299"
--> , "5b58931d-bb69-406d-81a9-7746c300838c"
--> ]
appID : UUID
appID = UUID.forName "myapplication.com" UUID.dnsNamespace
UUID.toString appID
--> "2ed74b2b-10eb-5b44-93be-69aa8952caac"
- More tests:
- test fromBytes explicitly
- fewer/simpler doc tests?
- test against set of known v3/v5 UUIDs
- split into smaller files?
Short answer: for most cases where you want to quickly generate a UUID on the client, probably.
Random.Seed
has either 32 or 54 bits of randomness, depending on your system
Version 4 UUIDs should have 122 bits of randomness. You should consider how
best to generate your Seed. Although the API is a little messier
than using UUID.Generator
, it is recommended to try using UUID.step
, so
UUID
s can be generated using four independent seeds. One way of generating the
seeds would be to use Crypto.getRandomValues()
to create
four 32-bit integers, and passing them in as flags or via ports.
You can check what variant/version you are using by looking at one of the
UUIDs, which should be in the format 00000000-0000-A000-B000-000000000000
.
If the character at position B
is 8
, 9
, a
or b
, you have a variant 1
UUID, and this package is for you! The character in position A
is the version
number. (If it isn't 1
, 2
, 3
, 4
or 5
, then it isn't a UUID as defined
by the RFC.)
Probably either version 4 or 5, depending on your use case. Version 4 UUIDs are randomly generated, while version 5 UUIDs are created from a "name" and a "namespace", such that the same "name" and "namespace" produces the same UUID.
If you want to use the UUID as a key for a value that may change over
time, you may want to use generator
to create version 4 UUIDs.
If the UUID will refer to something that will not change over time, or will
need to be calculated in some other way from some input data, consider using
forName
and forBytes
to create version 5 UUIDs
(version 3 UUIDs are very similar, but version 5 is recommended unless required
for backwards-compatbility).
Please open an issue on Github and I'll get back to you as soon as I can.
This can't reaallly be done in the browser (as far as I know), but feel free to open an issue on Github anyway.
Really!? Umm, well I guess you'd better open an issue on Github.
This might be better suited in a separate package, but why not open an issue on Github?