KScript 文档
ZH

类型与验证

具有独立含义的文本

类型 它包含的内容 已整理 方法
email 一个地址,例如 joan@example.com。 空格会被去除,域名会转换为小写。 .local, .domain
handle @后跟字母、数字、_、.或-,最多30个。 转为小写。 .name(不含 @)
uuid UUID,支持版本 1 到 8,以及 nil UUID 和 max UUID。 已转为小写形式:8-4-4-4-12。urn:uuid:、大括号和 32 位十六进制数字同样会被读取。 .version, .timestamp
regex 带有标志 i、m、s 和 x 的正则表达式。 ->test(), ->match(), ->match_all(), ->replace(), ->split(), .pattern, .flags
password 一个秘密。它从不打印。 ->hash(), ->verify(), ->needs_rehash(), ->strength(), ->is_strong(), ->reveal()

原生值仍然是文本,整理之后与同样的文本比较时视为相等。电子邮件比较时会将域名转为小写。它保留了适用于它的字符串方法,编辑器也只提示这些方法:

类型 它保留的字符串方法
email contains, starts_with, ends_with, index_of, count, lower, upper, slice, split, replace, matches, truncate, pad, escape, url_encode, markdown_escape
handle contains, starts_with, ends_with, count, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
uuid contains, starts_with, ends_with, upper, slice, split, pad, escape, url_encode, markdown_escape
regex contains, starts_with, ends_with, count, escape, markdown_escape
url contains, starts_with, ends_with, index_of, count, split, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
domain contains, starts_with, ends_with, index_of, count, split, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
ip contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
cidr contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
mac contains, starts_with, ends_with, index_of, count, split, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
slug contains, starts_with, ends_with, index_of, count, split, upper, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
file_path contains, starts_with, ends_with, index_of, count, split, lower, upper, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
slash_path contains, starts_with, ends_with, index_of, count, split, lower, upper, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
semver contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
mime contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
hex contains, starts_with, ends_with, index_of, count, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
base64 contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
jwt contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
json contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
csv contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
phone contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
country contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
currency contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
locale contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
timezone contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
duration contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
date_format contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
cron contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
color contains, starts_with, ends_with, index_of, count, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
iban contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape

port、card或password不保留任何内容。其余的,例如uuid上的->to_date(),会在编辑器中报告,并在脚本运行时失败。如果仍要对文本使用它,请先调用->to_string():$id->to_string()->to_date()。

<let name="email" type="email" value="' Joan.Smith@Example.COM '" />
<echo>{ $email } { $email.local } at { $email.domain }</echo>
<echo>{ $email == 'Joan.Smith@EXAMPLE.com' }</echo>

<let name="who" type="handle" value="@Joan_S" />
<echo>{ $who } { $who.name }</echo>

<let name="id" value="@uuid()" />
<let name="key" value="@uuid(v: 8, name: 'joan@example.com')" />
<echo>{ $id.version } { $key.version } { $key == @uuid(v: 8, name: 'joan@example.com') }</echo>
$email: email = ' Joan.Smith@Example.COM '
echo "{ $email } { $email.local } at { $email.domain }"
echo "{ $email == 'Joan.Smith@EXAMPLE.com' }"

$who: handle = @Joan_S
echo "{ $who } { $who.name }"

$id = @uuid()
$key = @uuid(v: 8, name: 'joan@example.com')
echo "{ $id.version } { $key.version } { $key == @uuid(v: 8, name: 'joan@example.com') }"

要检查文本是否为 UUID,请使用 $text->is_a(uuid)。使用 $text as uuid 进行转换,并将无效文本报告为错误。

@uuid() 生成一个随机的第 4 版 UUID。@uuid(v: 7) 生成一个按时间排序的第 7 版 UUID,通常用作数据库主键,因为新生成的 UUID 排在旧的之后;其 .timestamp 即为生成时的时间。@uuid(v: 8) 生成一个同样按时间排序的第 8 版 UUID。使用 name: 时,同一个名称每次都会得到相同的 UUID,namespace: 则用于区分来自不同位置的名称。

值的形式错误时,会在产生该值的位置直接失败,并给出一条 Shape 错误,说明原因:

文本
$email doesn't fit email: 'joan@' isn't an email: there's nothing after the @.

编辑器会在你输入时于该值处报告相同的问题(KS5012)。