KScript documentazione
IT

Lingua

Tradotto automaticamente dall’inglese, che è la versione originale.

campi e metodi di any

Metodi che ogni valore possiede.

Un campo è un dato semplice sul valore, letto con un punto e senza parentesi: $value.name. Un metodo richiede argomenti o esegue un'operazione, e si chiama con ->: $value->name(arguments), oppure passando il valore con la pipe: $value |> name(arguments).

Da 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

Cambia il formato del valore. from: legge un testo scritto in un formato (json, yaml, csv, markdown o latex), e to: scrive il valore in un formato (json, yaml, csv, oppure html, markdown e latex per i documenti); indicali entrambi per convertire un formato in un altro. Lettura di csv: not header quando non c'è una riga di intestazione. Lettura di json, yaml o csv: as: un tipo controlla i dati e li trasforma in record. Scrittura di csv: headers: sceglie e ordina le colonne. Scrittura di json: indented li distribuisce su più righe. ->from(type:) e ->to(type:) sono scorciatoie per from: e to: da soli.
ParametroAccetta
from facoltativouna di queste parole: 'json'|yaml|'csv'|'markdown'|'latex'
to facoltativouna di queste parole: 'json'|yaml|'csv'|html|'markdown'|'latex'
header facoltativoun flag: header oppure not header
as facoltativotype
headers facoltativolist
indented facoltativoun flag: indented o not indented

Gli argomenti possono essere con nome: ->convert(…, to: …, header: …).

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

Legge un testo scritto in un formato: scorciatoia per ->convert(from: type). ->from(type: csv, not header) legge csv senza riga di intestazione, e as: un tipo controlla i dati e li trasforma in record.
ParametroAccetta
type obbligatoriouna di queste parole: 'json'|yaml|'csv'|'markdown'|'latex'
header facoltativoun flag: header oppure not header
as facoltativotype

Gli argomenti possono essere con nome: ->from(…, header: …, as: …).

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

Vero quando il valore ha questo tipo, oppure quando text supera la validazione per un tipo nativo come uuid.
ParametroAccetta
type obbligatorioword
Esempio
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

Vero quando il valore è null.
Esempio
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

Il valore se è veritiero, altrimenti fallback.
Esempio
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

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

Scrive il valore in un formato: scorciatoia per ->convert(to: type). headers: sceglie e ordina le colonne csv, e indented distribuisce json su più righe.
ParametroAccetta
type obbligatoriouna di queste parole: 'json'|yaml|'csv'|html|'markdown'|'latex'
headers facoltativolist
indented facoltativoun flag: indented o not indented

Gli argomenti possono essere con nome: ->to(…, headers: …, indented: …).

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

Il valore come testo.
Esempio
<echo>{ 42->to_string() + '!' }</echo>
echo 42->to_string() + '!'

#any->type() → string

Restituisce il nome del tipo del valore. Una lista con un solo tipo di elemento restituisce il suo tipo seguito da [], come number[]. Le liste vuote miste o senza tipo restituiscono any[]. Un record restituisce il nome del tipo dichiarato.
Esempio
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()