Liking cljdoc? Tell your friends :D

typeid.codec

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

(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)]
```
raw docstring

decodeclj/s

(decode typeid-str)

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

(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]]
sourceraw docstring

encodeclj/s

(encode uuid-bytes prefix)

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

(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]]
sourceraw docstring

hex->uuidclj/s

(hex->uuid hex-string)

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

(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]]
sourceraw docstring

uuid->hexclj/s

(uuid->hex uuid-bytes)

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

(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]]
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close