KScript documentation
FR

champs et méthodes de any

Méthodes communes à toutes les valeurs.

Un champ est une information simple sur la valeur, lue avec un point et sans crochets : $value.name. Une méthode prend des arguments ou effectue un traitement, et s'appelle avec -> : $value->name(arguments), ou avec la valeur transmise par un pipe : $value |> name(arguments).

Depuis 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

Modifie le format de la valeur. from : lit un texte écrit dans un format (json, yaml, csv, markdown ou latex), et to : écrit la valeur dans un format (json, yaml, csv, ou html, markdown et latex pour les documents) ; donnez les deux pour convertir l'un en l'autre. Lecture de csv : not header lorsqu'il n'y a pas de ligne d'en-tête. Lecture de json, yaml ou csv : as : un type vérifie les données et les transforme en enregistrements. Écriture de csv : headers : choisit et ordonne les colonnes. Écriture de json : indented les répartit sur plusieurs lignes. ->from(type:) et ->to(type:) sont des raccourcis pour from: et to: utilisés seuls.
ParamètrePrend
from facultatifl'un de ces mots : 'json'|yaml|'csv'|'markdown'|'latex'
to optionnell'un de ces mots : 'json'|yaml|'csv'|html|'markdown'|'latex'
header facultatifun indicateur : header ou not header
as facultatiftype
headers facultatiflist
indented optionnelun indicateur : indented ou not indented

Les arguments peuvent être nommés : ->convert(…, to: …, header: …).

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

Lit un texte écrit dans un format : raccourci pour ->convert(from: type). ->from(type: csv, not header) lit du csv sans ligne d'en-tête, et as : un type vérifie les données et les transforme en enregistrements.
ParamètrePrend
type obligatoirel'un de ces mots : 'json'|yaml|'csv'|'markdown'|'latex'
header facultatifun indicateur : header ou not header
as facultatiftype

Les arguments peuvent être nommés : ->from(…, header: …, as: …).

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

Vrai lorsque la valeur a ce type, ou lorsque text passe la validation pour un type natif comme uuid.
ParamètrePrend
type obligatoireword
Exemple
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

Vrai lorsque la valeur est null.
Exemple
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

La valeur si elle est vraie, sinon fallback.
Exemple
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

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

Écrit la valeur dans un format : raccourci pour ->convert(to: type). headers : choisit et ordonne les colonnes csv, et indented répartit le json sur plusieurs lignes.
ParamètrePrend
type obligatoirel'un de ces mots : 'json'|yaml|'csv'|html|'markdown'|'latex'
headers facultatiflist
indented optionnelun indicateur : indented ou not indented

Les arguments peuvent être nommés : ->to(…, headers: …, indented: …).

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

La valeur sous forme de texte.
Exemple
<echo>{ 42->to_string() + '!' }</echo>
echo 42->to_string() + '!'

#any->type() → string

Renvoie le nom de type de la valeur. Une liste avec un type d'élément renvoie ce type suivi de [], comme number[]. Une liste vide ou mixte renvoie any[]. Un enregistrement renvoie le nom de son type déclaré.
Exemple
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()