|
56 | 56 |
|
57 | 57 | For time manipulation in VibeScript, use the `Time` object (`Time.now`, `Time.parse`, `Time.utc`, etc.). See `docs/time.md`. |
58 | 58 |
|
| 59 | +## Random IDs |
| 60 | + |
| 61 | +### `uuid()` |
| 62 | + |
| 63 | +Returns an RFC 4122 version 4 UUID string: |
| 64 | + |
| 65 | +```vibe |
| 66 | +event_id = uuid() |
| 67 | +``` |
| 68 | + |
| 69 | +### `random_id(length = 16)` |
| 70 | + |
| 71 | +Returns an alphanumeric random identifier string: |
| 72 | + |
| 73 | +```vibe |
| 74 | +short = random_id(8) |
| 75 | +token = random_id() |
| 76 | +``` |
| 77 | + |
| 78 | +## Numeric Conversion |
| 79 | + |
| 80 | +### `to_int(value)` |
| 81 | + |
| 82 | +Converts `int`, integral `float`, or base-10 numeric `string` values into `int`. |
| 83 | + |
| 84 | +### `to_float(value)` |
| 85 | + |
| 86 | +Converts `int`, `float`, or numeric `string` values into `float`. |
| 87 | + |
| 88 | +```vibe |
| 89 | +count = to_int("42") |
| 90 | +ratio = to_float("1.25") |
| 91 | +``` |
| 92 | + |
| 93 | +## JSON |
| 94 | + |
| 95 | +### `JSON.parse(string)` |
| 96 | + |
| 97 | +Parses a JSON string into VibeScript values (`hash`, `array`, `string`, `int`, |
| 98 | +`float`, `bool`, `nil`): |
| 99 | + |
| 100 | +```vibe |
| 101 | +payload = JSON.parse("{\"id\":\"p-1\",\"score\":10}") |
| 102 | +payload[:score] # 10 |
| 103 | +``` |
| 104 | + |
| 105 | +`JSON.parse` enforces a 1 MiB input limit. |
| 106 | + |
| 107 | +### `JSON.stringify(value)` |
| 108 | + |
| 109 | +Serializes supported values (`hash`/`object`, `array`, scalar primitives) into |
| 110 | +a JSON string: |
| 111 | + |
| 112 | +```vibe |
| 113 | +raw = JSON.stringify({ id: "p-1", score: 10, tags: ["a", "b"] }) |
| 114 | +``` |
| 115 | + |
| 116 | +`JSON.stringify` enforces a 1 MiB output limit. |
| 117 | + |
| 118 | +## Regex |
| 119 | + |
| 120 | +### `Regex.match(pattern, text)` |
| 121 | + |
| 122 | +Returns the first match string or `nil` when no match exists. |
| 123 | + |
| 124 | +### `Regex.replace(text, pattern, replacement)` |
| 125 | + |
| 126 | +Replaces the first regex match in `text`. |
| 127 | + |
| 128 | +### `Regex.replace_all(text, pattern, replacement)` |
| 129 | + |
| 130 | +Replaces all regex matches in `text`. |
| 131 | + |
| 132 | +```vibe |
| 133 | +Regex.match("ID-[0-9]+", "ID-12 ID-34") # "ID-12" |
| 134 | +Regex.replace("ID-12 ID-34", "ID-[0-9]+", "X") # "X ID-34" |
| 135 | +Regex.replace_all("ID-12 ID-34", "ID-[0-9]+", "X") # "X X" |
| 136 | +Regex.replace("ID-12", "ID-([0-9]+)", "X-$1") # "X-12" |
| 137 | +``` |
| 138 | + |
| 139 | +Regex helpers enforce input guards (max pattern size 16 KiB, max text size 1 MiB). |
| 140 | + |
59 | 141 | ## Module Loading |
60 | 142 |
|
61 | 143 | ### `require(module_name, as: alias?)` |
|
0 commit comments