KScript 文档
ZH

任意字段与方法

每个值都有的方法。

字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。

来自 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

更改值的格式。from:读取以某种格式(json、yaml、csv、markdown 或 latex)编写的文本;to:将值写成某种格式(json、yaml、csv,或用于文档的 html、markdown 和 latex);两者同时指定即可进行转换。读取 csv:没有表头行时使用 not header。读取 json、yaml 或 csv:as:指定类型后,会校验数据并将其转换为记录。写入 csv:headers:选择并排列列。写入 json:indented 会将其分行展开。->from(type:) 和 ->to(type:) 是 from: 与 to: 单独使用时的简写。
参数接受
from 可选以下单词之一:'json'|yaml|'csv'|'markdown'|'latex'
to 可选以下单词之一:'json'|yaml|'csv'|html|'markdown'|'latex'
header 可选一个标志:header 或 not header
as 可选type
headers 可选list
indented 可选一个标志:indented 或 not indented

参数可以命名:->convert(…, to: …, header: …)。

示例
<!-- '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

读取以某种格式编写的文本,是 ->convert(from: type) 的简写。->from(type: csv, not header) 读取没有表头行的 csv;as:指定类型后,会校验数据并将其转换为记录。
参数接受
type 必填以下单词之一:'json'|yaml|'csv'|'markdown'|'latex'
header 可选一个标志:header 或 not header
as 可选type

参数可以命名:->from(…, header: …, as: …)。

示例
<!-- 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

当该值具有此类型,或文本通过了 uuid 等原生类型的校验时为 true。
参数接受
type 必填word
示例
<echo>{ 1h->is_a(duration) }</echo>
echo 1h->is_a(duration)

#any->is_null() → bool

当值为 null 时为 true。
示例
<echo>{ null->is_null() }</echo>
echo null->is_null()

#any->or(fallback) → any

若该值为真则返回该值,否则返回 fallback。
示例
<echo>{ ''->or('nobody') }</echo>
echo ''->or('nobody')

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

以某种格式写出值,是 ->convert(to: type) 的简写。headers: 选择并排列 csv 列,indented 会将 json 分行展开。
参数接受
type 必填以下单词之一:'json'|yaml|'csv'|html|'markdown'|'latex'
headers 可选list
indented 可选一个标志:indented 或 not indented

参数可以命名:->to(…, headers: …, indented: …)。

示例
<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

该值的文本形式。
示例
<echo>{ 42->to_string() + '!' }</echo>
echo 42->to_string() + '!'

#any->type() → string

返回值的类型名称。只有一种元素类型的列表会返回其类型名,后面跟 [],例如 number[]。混合类型或无类型的空列表返回 any[]。记录返回其声明的类型名。
示例
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()