RatMinimal web framework

Conversion

Builtins for moving values between kinds. str renders any value as a string, num parses numeric text, and bool applies the truthiness rules. int is the strict integer parser, and ord/chr bridge a character and its code point. Use them at boundaries: reading form input, accepting query parameters, or preparing values for an external API. They are not meant for normal arithmetic.

str: render anything as a string

Numbers, bools, arrays, objects, null

str(value) calls the type's standard rendering. Numbers use their natural decimal form; booleans become 'true' or 'false'; arrays and objects use the same compact form interpolation does. null becomes 'null'.

<p> ([str(42)])
<p> ([str(3.14)])
<p> ([str(true)])
Result

str(42) → (42)

str(3.14) → (3.14)

str(true) → (true)

num: parse a string as a number

Float result; non-numeric strings become 0

num(s) parses the string as a decimal number, returning a float. Leading and trailing whitespace is tolerated. A string that doesn't parse cleanly returns 0, so wrap the call in a guard when you need to tell "parsed to zero" apart from "garbage in".

<p> [num('3.14')]
<p> [num('42')]
Result

num('3.14') = 3.14

num('42') = 42

int: parse to a whole number

Truncates toward zero; unparseable input is null, not 0

int(s) parses a string (or truncates a float) to an integer: '42' and '42.9' both become 42, and 3.7 becomes 3. Unlike num, an unparseable string returns null. A real digit reads as a number and garbage reads as "no number", rather than a silent 0 you can't tell apart from the digit zero. Guard with (int(s) is null) when the input is untrusted.

<p> [int('42')]
<p> [int('42.9')]
<p> [int('abc') is null]
Result

int('42') = 42

int('42.9') = 42

int('abc') is null = true

ord / chr: character ↔ code point

Digit and letter math without a lookup table

ord(c) returns the code point of the first character; chr(n) renders a code point back to a one-character string. Together they cover digit math (ord(c) - ord('0') turns '7' into 7), case shifts, and Caesar-cipher style problems.

<p> [ord('A')]
<p> [chr(66)]
<p> [ord('7') - ord('0')]
Result

ord('A') = 65

chr(66) = B

ord('7') - ord('0') = 7

bool: truthiness

Same rules as guards: 0 / empty / null are false

bool(value) returns the JavaScript-style truthiness of any value. 0, '', null, empty arrays, and empty objects are all false; everything else is true. The same rules drive guard conditions, so (value) in a guard is equivalent to (bool(value)).

<p> [bool(0)]
<p> [bool('hi')]
<p> [bool(null)]
<p> [bool([])]
Result

bool(0) = false

bool('hi') = true

bool(null) = false

bool([]) = false

Form-input coercion

Strings arrive from the wire; coerce before use

Inputs from <input> are always strings, even on type='number'. To do arithmetic on the value, wrap it in num(...) first. The same goes for query parameters and JSON values whose schema you don't control.

> page
qty: '0'
total >> num(qty) * 9.99

<input value[qty] on_input[qty << event.target.value]>
<p> Total: $[total]

See also Strings · Math · Forms