KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

any のフィールドとメソッド

すべての値が持つメソッド。

フィールドは、値についての単純な事実で、ドットを使い角括弧なしで読み取ります: $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

値がこの型である場合、または text が 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

値の型名を返します。要素型が 1 つだけのリストは、その型に続けて [] を付けたもの(例: number[])を返します。型が混在する場合や型のない空のリストは any[] を返します。レコードは宣言された型名を返します。
例
<echo>{ [1, 2]->type() }</echo>
echo [1, 2]->type()