KScript documentación
ES

Idioma

Traducido automáticamente del inglés, que es la versión original.

campos y métodos de any

Métodos que tiene cualquier valor.

Un campo es un dato fijo sobre el valor, que se lee con un punto y sin corchetes: $value.name. Un método recibe argumentos o realiza una acción, y se llama con ->: $value->name(arguments), o con el valor encadenado: $value |> name(arguments).

Desde 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 el formato del valor. from: lee texto escrito en un formato (json, yaml, csv, markdown o latex), y to: escribe el valor en uno (json, yaml, csv, o html, markdown y latex para documentos); indica ambos para convertir uno en otro. Lectura de csv: not header cuando no hay línea de encabezado. Lectura de json, yaml o csv: as: un tipo valida los datos y los convierte en registros. Escritura de csv: headers: elige y ordena las columnas. Escritura de json: indented los distribuye en líneas. ->from(type:) y ->to(type:) son atajos de from: y to: por separado.
ParámetroRecibe
from opcionaluna de estas palabras: 'json'|yaml|'csv'|'markdown'|'latex'
to opcionaluna de estas palabras: 'json'|yaml|'csv'|html|'markdown'|'latex'
header opcionaluna marca: header o not header
as opcionaltype
headers opcionallist
indented opcionalun indicador: indented o not indented

Los argumentos pueden tener nombre: ->convert(…, to: …, header: …).

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

Lee texto escrito en un formato: un atajo de ->convert(from: type). ->from(type: csv, not header) lee csv sin línea de encabezado, y as: un tipo valida los datos y los convierte en registros.
ParámetroRecibe
type obligatoriouna de estas palabras: 'json'|yaml|'csv'|'markdown'|'latex'
header opcionaluna marca: header o not header
as opcionaltype

Los argumentos pueden tener nombre: ->from(…, header: …, as: …).

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

Verdadero cuando el valor tiene este tipo, o cuando text pasa la validación de un tipo nativo como uuid.
ParámetroRecibe
type obligatorioword
Ejemplo
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

Verdadero cuando el valor es null.
Ejemplo
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

El valor si es verdadero; en caso contrario, fallback.
Ejemplo
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

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

Escribe el valor en un formato: un atajo de ->convert(to: type). headers: elige y ordena las columnas de csv, e indented distribuye json en líneas.
ParámetroRecibe
type obligatoriouna de estas palabras: 'json'|yaml|'csv'|html|'markdown'|'latex'
headers opcionallist
indented opcionalun indicador: indented o not indented

Los argumentos pueden tener nombre: ->to(…, headers: …, indented: …).

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

El valor como texto.
Ejemplo
<echo>{ 42->to_string() + '!' }</echo>
echo 42->to_string() + '!'

#any->type() → string

Devuelve el nombre del tipo del valor. Una lista con un único tipo de elemento devuelve su tipo seguido de [], como number[]. Las listas vacías sin tipo o con tipos mixtos devuelven any[]. Un registro devuelve el nombre de tipo que declara.
Ejemplo
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()