Capy-Bearer Documentation

string

lib call

Capy

function string(value : as string, opt : dval = {}) string

Description

string() reads a scalar as text. Request values are dval, so this is the normal way to read request fields.

value accepts scalar values through as string. The conversion keeps source text exact. Text returns unchanged. Numbers use exact round-trip text. Booleans become true or false.

opt.fallback is the result for empty text or a value with no text form. It defaults to the empty string. Empty and missing dval values both convert to empty text. Use the ? suffix before conversion when presence matters. value? is false for a missing value and true for an empty value.

The options map accepts only fallback. An unknown key, including base, is an error.

The other converters

All numeric constructors use the same value, opt shape. Integer constructor options also support base. bool() keeps a dval first parameter, because strings cannot be conditions.

Parameters

value : scalar value or text to convert

opt : options map with fallback

Return Values

The converted text, or opt.fallback.

Example

Capy

print(string(dval(42)), "\n")

Example

Capy

print(string(request.query.name, {fallback: "guest"}), "\n")

Example

Capy

if request.query.q? {
    print("searched: ", string(request.query.q), "\n")
} else {
    print("no search term\n")
}

Example

Capy

var target := unit_load("/tests/capy-module-target.capy")
print(string(target.echo({name: "Ada"}).name, {fallback: "unknown"}), "\n")