KScript docs
EN

any fields and methods

Methods every value has.

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

From KScript.Core

#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any

Changes the value's format. from: reads text written in a format (json, yaml, csv, markdown or latex), and to: writes the value in one (json, yaml, csv, or html, markdown and latex for documents); give both to turn one into the other. Reading csv: not header when there is no header line. Reading json, yaml or csv: as: a type checks the data and turns it into records. Writing csv: headers: picks and orders the columns. Writing json: indented spreads it over lines. ->from(type:) and ->to(type:) are shortcuts for from: and to: on their own.
ParameterTakes
from optionalone of these words: 'json'|yaml|'csv'|'markdown'|'latex'
to optionalone of these words: 'json'|yaml|'csv'|html|'markdown'|'latex'
header optionala flag: header or not header
as optionaltype
headers optionallist
indented optionala flag: indented or not indented

Arguments can be named: ->convert(…, to: …, header: …).

Example
<!-- 'apple': the json text is read into a list of maps -->
<echo>{ '[{"fruit": "apple"}]'->convert(from: json)->first().fruit }</echo>
<let name="prices" value="[{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]" />
<!-- fruit,price on the first line, then one line a row: the maps' keys name the columns -->
<echo>{ $prices->convert(to: csv) }</echo>
<!-- 'apple: 3': yaml read, then written as json -->
<echo>{ 'apple: 3'->convert(from: yaml, to: json) }</echo>
// 'apple': the json text is read into a list of maps
echo '[{"fruit": "apple"}]'->convert(from: json)->first().fruit
$prices = [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]
// fruit,price on the first line, then one line a row: the maps' keys name the columns
echo $prices->convert(to: csv)
// 'apple: 3': yaml read, then written as json
echo 'apple: 3'->convert(from: yaml, to: json)

#any->from(type: 'json'|yaml|'csv'|'markdown'|'latex', header?: bool, as?: type) → any

Reads text written in a format: a shortcut for ->convert(from: type). ->from(type: csv, not header) reads csv with no header line, and as: a type checks the data and turns it into records.
ParameterTakes
type requiredone of these words: 'json'|yaml|'csv'|'markdown'|'latex'
header optionala flag: header or not header
as optionaltype

Arguments can be named: ->from(…, header: …, as: …).

Example
<!-- 3: the yaml text read into a map -->
<echo>{ 'apple: 3'->from(type: yaml).apple }</echo>
// 3: the yaml text read into a map
echo 'apple: 3'->from(type: yaml).apple

#any->is_a(type: word) → bool

True when the value has this type, or when text passes validation for a native type such as uuid.
ParameterTakes
type requiredword
Example
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

True when the value is null.
Example
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

The value if it is truthy, otherwise fallback.
Example
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

#any->to(type: 'json'|yaml|'csv'|html|'markdown'|'latex', headers?: list, indented?: bool) → any

Writes the value in a format: a shortcut for ->convert(to: type). headers: picks and orders csv columns, and indented spreads json over lines.
ParameterTakes
type requiredone of these words: 'json'|yaml|'csv'|html|'markdown'|'latex'
headers optionallist
indented optionala flag: indented or not indented

Arguments can be named: ->to(…, headers: …, indented: …).

Example
<let name="prices" value="[{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]" />
<!-- the list written as json text -->
<echo>{ $prices->to(type: json) }</echo>
$prices = [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]
// the list written as json text
echo $prices->to(type: json)

#any->to_string() → string

The value as text.
Example
<echo>{ 42->to_string() + '!' }</echo>
echo 42->to_string() + '!'

#any->type() → string

Returns the value's type name. A list with one element type returns its type followed by [], such as number[]. Mixed or untyped empty lists return any[]. A record returns its declared type name.
Example
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()