Capy-Bearer Documentation

string

lib call

Capy

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

Description

string() reads a scalar as text. Request values are any, 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 any 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 an any 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(42), "\n")

Output

42

Example

Capy

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

Output

guest

Example

Capy

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

Output

no search term

Example

Capy

var target := unit_load("/doc/examples/call_target.capy")
print(string(target.echo(name: "Ada").name, fallback: "unknown"), "\n")

Output

Ada