KScript documentação
PT

Idioma

Traduzido automaticamente do inglês, que é a versão original.

campos e métodos de any

Métodos que todo valor tem.

Um campo é um dado simples sobre o valor, lido com um ponto e sem colchetes: $value.name. Um método recebe argumentos ou executa alguma ação, e é chamado com ->: $value->name(arguments), ou com o valor passado por pipe: $value |> name(arguments).

Do 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

Muda o formato do valor. from: lê texto escrito em um formato (json, yaml, csv, markdown ou latex), e to: escreve o valor em um formato (json, yaml, csv, ou html, markdown e latex para documentos); use os dois para converter um no outro. Lendo csv: not header quando não há linha de cabeçalho. Lendo json, yaml ou csv: as: um tipo verifica os dados e os transforma em registros. Escrevendo csv: headers: escolhe e ordena as colunas. Escrevendo json: indented os espalha em linhas. ->from(type:) e ->to(type:) são atalhos para from: e to: sozinhos.
ParâmetroRecebe
from opcionaluma destas palavras: 'json'|yaml|'csv'|'markdown'|'latex'
to opcionaluma destas palavras: 'json'|yaml|'csv'|html|'markdown'|'latex'
header opcionaluma flag: header ou not header
as opcionaltype
headers opcionallist
indented opcionaluma flag: indented ou not indented

Os argumentos podem ser nomeados: ->convert(…, to: …, header: …).

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

Lê texto escrito em um formato: um atalho para ->convert(from: type). ->from(type: csv, not header) lê csv sem linha de cabeçalho, e as: um tipo verifica os dados e os transforma em registros.
ParâmetroRecebe
type obrigatóriouma destas palavras: 'json'|yaml|'csv'|'markdown'|'latex'
header opcionaluma flag: header ou not header
as opcionaltype

Os argumentos podem ser nomeados: ->from(…, header: …, as: …).

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

Verdadeiro quando o valor tem este tipo, ou quando text passa na validação de um tipo nativo como uuid.
ParâmetroRecebe
type obrigatórioword
Exemplo
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

Verdadeiro quando o valor é null.
Exemplo
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

O valor, se for verdadeiro; caso contrário, fallback.
Exemplo
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

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

Escreve o valor em um formato: um atalho para ->convert(to: type). headers: escolhe e ordena as colunas do csv, e indented espalha o json em linhas.
ParâmetroRecebe
type obrigatóriouma destas palavras: 'json'|yaml|'csv'|html|'markdown'|'latex'
headers opcionallist
indented opcionaluma flag: indented ou not indented

Os argumentos podem ser nomeados: ->to(…, headers: …, indented: …).

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

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

#any->type() → string

Retorna o nome do tipo do valor. Uma lista com um tipo de elemento retorna esse tipo seguido de [], como number[]. Listas vazias ou sem tipo definido retornam any[]. Um registro retorna o nome do tipo declarado.
Exemplo
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()