KScript Dokumentation
DE

Sprache

Automatisch aus dem Englischen übersetzt; das englische Original ist maßgeblich.

beliebige Felder und Methoden

Methoden, die jeder Wert hat.

Ein Feld ist eine einfache Angabe über den Wert, die mit einem Punkt und ohne Klammern gelesen wird: $value.name. Eine Methode nimmt Argumente entgegen oder führt eine Aktion aus und wird mit -> aufgerufen: $value->name(arguments), oder mit dem übergebenen Wert: $value |> name(arguments).

Aus 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

Ändert das Format des Wertes. from: liest Text in einem Format (json, yaml, csv, markdown oder latex), und to: schreibt den Wert in einem Format (json, yaml, csv oder html, markdown und latex für Dokumente); gib beides an, um das eine in das andere umzuwandeln. Beim Lesen von csv: not header, wenn keine Kopfzeile vorhanden ist. Beim Lesen von json, yaml oder csv: as: ein Typ prüft die Daten und wandelt sie in Datensätze um. Beim Schreiben von csv: headers: wählt die Spalten aus und ordnet sie. Beim Schreiben von json: indented verteilt die Ausgabe auf mehrere Zeilen. ->from(type:) und ->to(type:) sind Kurzschreibweisen für from: und to: allein.
ParameterNimmt
from optionaleines dieser Wörter: 'json'|yaml|'csv'|'markdown'|'latex'
to optionaleines dieser Wörter: 'json'|yaml|'csv'|html|'markdown'|'latex'
header optionalein Flag: header oder not header
as optionaltype
headers optionallist
indented optionalein Schalter: indented oder not indented

Argumente können benannt werden: ->convert(…, to: …, header: …).

Beispiel
<!-- '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

Liest Text in einem Format: eine Kurzschreibweise für ->convert(from: type). ->from(type: csv, not header) liest csv ohne Kopfzeile, und as: ein Typ prüft die Daten und wandelt sie in Datensätze um.
ParameterNimmt
type erforderlicheines dieser Wörter: 'json'|yaml|'csv'|'markdown'|'latex'
header optionalein Flag: header oder not header
as optionaltype

Argumente können benannt werden: ->from(…, header: …, as: …).

Beispiel
<!-- 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

Wahr, wenn der Wert diesen Typ hat oder wenn text die Prüfung für einen nativen Typ wie uuid besteht.
ParameterNimmt
type erforderlichword
Beispiel
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

Wahr, wenn der Wert null ist.
Beispiel
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

Der Wert, wenn er wahr ist, andernfalls fallback.
Beispiel
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

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

Schreibt den Wert in einem Format: eine Kurzschreibweise für ->convert(to: type). headers: wählt csv-Spalten aus und ordnet sie, und indented verteilt json auf mehrere Zeilen.
ParameterNimmt
type erforderlicheines dieser Wörter: 'json'|yaml|'csv'|html|'markdown'|'latex'
headers optionallist
indented optionalein Schalter: indented oder not indented

Argumente können benannt werden: ->to(…, headers: …, indented: …).

Beispiel
<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

Der Wert als Text.
Beispiel
<echo>{ 42->to_string() + '!' }</echo>
echo 42->to_string() + '!'

#any->type() → string

Gibt den Typnamen des Wertes zurück. Eine Liste mit einem einheitlichen Elementtyp gibt ihren Typ gefolgt von [] zurück, etwa number[]. Gemischte oder untypisierte leere Listen geben any[] zurück. Ein Datensatz gibt seinen deklarierten Typnamen zurück.
Beispiel
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()