Reference · General

uuid

signature
uuid() -> str

What it does

A new version 4 UUID as text: 36 characters, lower-case hex in five groups of 8, 4, 4, 4 and 12, with a 4 at the start of the third group. Its bytes come from the operating system, so it can serve as a session id, a token or a file name that cannot be guessed, which random cannot.

Examples

let u = uuid()print(type_of(u), len(u))print(split(u, "-") | map(len))print(re.test(u, "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"))
output
str 36[8, 4, 4, 4, 12]true

Edge cases

print(range(1000) | map((i) => uuid()) | unique | len)let u = uuid()print(u == lower(u), len(replace(u, "-", "")), slice(u, 14, 15))
output
1000true 32 4

Errors

uuid never gives an error value. It is a runtime error only when the operating system gives no random bytes, which does not happen on a working machine.

print(is_err(uuid()))
output
false

An argument is a runtime error, and so is one for quillo check when the call is written out.

this one fails
print(uuid(1))
output
line 1: `uuid` takes 0 arguments, got 1: uuid()

Chaining

uuid starts a pipeline rather than taking one, since it has no argument.

print(uuid() | upper | len)print(`photos/${uuid()}.jpg` | ends_with(".jpg"))
output
36true
in a program
server fn add_photo(file: map) = save_upload(file, `photos/${uuid()}.jpg`)

See also

random, random_int, crypto.token, save_upload

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On uuid(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo