Low-level codec operations for TypeID encoding and decoding.
This namespace provides the building blocks for TypeID encoding/decoding:
encode - UUID bytes + prefix → TypeID stringdecode - TypeID string → UUID bytesuuid->hex - UUID bytes → hex stringhex->uuid - Hex string → UUID bytesMost users should use the high-level API in typeid.core instead.
These functions are exposed for advanced use cases and testing.
(require '[typeid.codec :as codec])
(def uuid-bytes (byte-array 16)) ; Your UUID bytes
(codec/encode uuid-bytes "user")
;;=> "user_00000000000000000000000000"
(codec/decode "user_00000000000000000000000000")
;;=> #bytes[0x00 0x00 ... (16 bytes)]
Low-level codec operations for TypeID encoding and decoding. This namespace provides the building blocks for TypeID encoding/decoding: ## Core Functions - [[encode]] - UUID bytes + prefix → TypeID string - [[decode]] - TypeID string → UUID bytes - [[uuid->hex]] - UUID bytes → hex string - [[hex->uuid]] - Hex string → UUID bytes ## Usage Note Most users should use the high-level API in [[typeid.core]] instead. These functions are exposed for advanced use cases and testing. ## Quick Example ```clojure (require '[typeid.codec :as codec]) (def uuid-bytes (byte-array 16)) ; Your UUID bytes (codec/encode uuid-bytes "user") ;;=> "user_00000000000000000000000000" (codec/decode "user_00000000000000000000000000") ;;=> #bytes[0x00 0x00 ... (16 bytes)] ```
(decode typeid-str)Decode a TypeID string to extract UUID bytes.
typeid-str - A valid TypeID string (with or without prefix)
16-byte array (big-endian) representing the UUID.
Throws ex-info if the input is invalid.
(decode "user_01h5fskfsk4fpeqwnsyz5hj55t")
;;=> #bytes[0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
;; 0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]
(decode "01h5fskfsk4fpeqwnsyz5hj55t")
;;=> #bytes[0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
;; 0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]
See also: encode, typeid.core/parse
Decode a TypeID string to extract UUID bytes. ## Parameters **typeid-str** - A valid TypeID string (with or without prefix) ## Returns 16-byte array (big-endian) representing the UUID. ## Exceptions Throws `ex-info` if the input is invalid. ## Examples ```clojure (decode "user_01h5fskfsk4fpeqwnsyz5hj55t") ;;=> #bytes[0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d ;; 0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1] (decode "01h5fskfsk4fpeqwnsyz5hj55t") ;;=> #bytes[0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d ;; 0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1] ``` See also: [[encode]], [[typeid.core/parse]]
(encode uuid-bytes prefix)Encode UUID bytes with a prefix into a TypeID string.
uuid-bytes - 16-byte array representing a UUID
prefix - Can be:
[a-z]([a-z_]{0,61}[a-z])?nil or empty string: generates prefix-less TypeIDTypeID string with format prefix_suffix or just suffix if no prefix.
Throws ex-info if inputs are invalid.
(def uuid-bytes (byte-array [0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]))
(encode uuid-bytes "user")
;;=> "user_01h5fskfsk4fpeqwnsyz5hj55t"
(encode uuid-bytes nil)
;;=> "01h5fskfsk4fpeqwnsyz5hj55t"
(encode uuid-bytes :org)
;;=> "org_01h5fskfsk4fpeqwnsyz5hj55t"
See also: decode, typeid.core/create
Encode UUID bytes with a prefix into a TypeID string.
## Parameters
**uuid-bytes** - 16-byte array representing a UUID
**prefix** - Can be:
- A string: 0-63 lowercase alphanumeric matching `[a-z]([a-z_]{0,61}[a-z])?`
- A keyword: its name will be used as the prefix
- `nil` or empty string: generates prefix-less TypeID
## Returns
TypeID string with format `prefix_suffix` or just `suffix` if no prefix.
## Exceptions
Throws `ex-info` if inputs are invalid.
## Examples
```clojure
(def uuid-bytes (byte-array [0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]))
(encode uuid-bytes "user")
;;=> "user_01h5fskfsk4fpeqwnsyz5hj55t"
(encode uuid-bytes nil)
;;=> "01h5fskfsk4fpeqwnsyz5hj55t"
(encode uuid-bytes :org)
;;=> "org_01h5fskfsk4fpeqwnsyz5hj55t"
```
See also: [[decode]], [[typeid.core/create]](hex->uuid hex-string)Convert hexadecimal string to UUID bytes.
hex-string - 32-character hex string
16-byte UUID array.
Throws ex-info if input is invalid.
(hex->uuid "0188e5f5f34a7b3d9f2a1c5de67fa8c1")
;;=> #bytes[0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
;; 0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]
(hex->uuid "018c3f9e-9e4e-7a8a-8b2a-7e8e9e4e7a8a")
;;=> #bytes[0x01 0x8c 0x3f 0x9e ...]
(hex->uuid "018C3F9E9E4E7A8A8B2A7E8E9E4E7A8A")
;;=> #bytes[0x01 0x8c 0x3f 0x9e ...] ; Uppercase accepted
See also: uuid->hex
Convert hexadecimal string to UUID bytes. ## Parameters **hex-string** - 32-character hex string - With or without hyphens - Case-insensitive ## Returns 16-byte UUID array. ## Exceptions Throws `ex-info` if input is invalid. ## Examples ```clojure (hex->uuid "0188e5f5f34a7b3d9f2a1c5de67fa8c1") ;;=> #bytes[0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d ;; 0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1] (hex->uuid "018c3f9e-9e4e-7a8a-8b2a-7e8e9e4e7a8a") ;;=> #bytes[0x01 0x8c 0x3f 0x9e ...] (hex->uuid "018C3F9E9E4E7A8A8B2A7E8E9E4E7A8A") ;;=> #bytes[0x01 0x8c 0x3f 0x9e ...] ; Uppercase accepted ``` See also: [[uuid->hex]]
(uuid->hex uuid-bytes)Convert UUID bytes to hexadecimal string.
uuid-bytes - 16-byte UUID array
32-character lowercase hex string (no hyphens).
Throws ex-info if input is invalid.
(def uuid-bytes (byte-array [0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]))
(uuid->hex uuid-bytes)
;;=> "0188e5f5f34a7b3d9f2a1c5de67fa8c1"
See also: hex->uuid
Convert UUID bytes to hexadecimal string.
## Parameters
**uuid-bytes** - 16-byte UUID array
## Returns
32-character lowercase hex string (no hyphens).
## Exceptions
Throws `ex-info` if input is invalid.
## Examples
```clojure
(def uuid-bytes (byte-array [0x01 0x88 0xe5 0xf5 0xf3 0x4a 0x7b 0x3d
0x9f 0x2a 0x1c 0x5d 0xe6 0x7f 0xa8 0xc1]))
(uuid->hex uuid-bytes)
;;=> "0188e5f5f34a7b3d9f2a1c5de67fa8c1"
```
See also: [[hex->uuid]]cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |