方法与字段
关于某个值的简单事实是字段,使用点号读取,不带方括号:$file.name、$file.size、$response.status、$date.year。调用字段($file->name())会报错并说明原因。对任何值都可用 ->count() 进行计数。
| 在 | 之前 | 现在 |
|---|---|---|
| 列表、映射、文本 | ->length() |
->count() |
| 文本 | $text->is_empty() 用于接受空文本或仅含空白的文本 |
请为该检查使用 $text->is_blank()。$text->is_empty() 现在检测零个字符。 |
| 列表 | ->where(fn), ->append(item) |
->filter(fn), ->push(item) |
| 列表 | $items->count_of($value) 用于统计严格匹配的数量 |
严格匹配请使用 $items->count_of(fn(x) => $x === $value)。传入值现在与 == 比较。 |
| 数字 | ->pow(2) |
$n ** 2 |
| 数字 | $n->between(1, 10) |
1 <= $n <= 10,一条比较链 |
| 响应 | ->ok() |
.is_ok |
| IP | ->private(), ->loopback() |
.is_private, .is_loopback |
| JWT | ->expired() |
.is_expired |
| 数据库 | ->in_transaction() |
.is_in_transaction |
| 密码 | ->strong() |
->is_strong() |
| 日志记录器 | ->enabled(level) |
->is_enabled(level) |
| 时区 | ->dst() |
->is_dst() |
| 缓存 | ->put(key, value), ->forget(key), ->flush() |
->set(key, value), ->remove(key), ->clear() |
| 会话 | ->put(key, value), ->forget(key) |
->set(key, value), ->remove(key) |
| 存储文件夹 | ->path(name) |
->path_of(name);.path 即文件夹本身 |
| 颜色 | ->contrast(other) |
->contrast_ratio(other) |
| 查询、模型 | ->avg(column) |
->average(column) |
| URL | ->with_query(values) |
->with(query: values);->without(query, fragment) 会去掉整段部分 |
| 日期 | ->add_years(n), ->add_months(n), ->add_days(n) |
->add(years: n, months: n, days: n),也可用 weeks:;->subtract(...) 则回退 |
| 日期 | ->add_hours(n), ->add_minutes(n) |
时长:$date + 3h、$date - 45m |
| 日期 | ->duration_since(other) |
$date - $other,即一个时长 |
| 日期 | ->months_since(other), ->years_since(other) |
->since(other, in: months)、->since(other, in: years)(也可用 days 和 weeks) |
| 日期 | $date + 1、$date - 1(天)、$a - $b(天数) |
$date + 1d 或 ->add(days: 1);$a - $b 现在是一个时长 |
| 时长 | ->plus(other), ->minus(other) |
->add(other), ->subtract(other) |
| 文本 | ->trim_start(chars?), ->trim_end(chars?) |
->trim(start), ->trim(chars, end) |
| 文本 | ->pad_left(width, char?), ->pad_right(width, char?) |
->pad(width, char?, start), ->pad(width, char?) |
| 文本 | ->strip_prefix(text), ->strip_suffix(text) |
->strip(prefix: text)、->strip(suffix: text),或同时使用两者 |
| 文本 | ->before_last(text), ->after_last(text), ->last_index_of(text) |
->before(text, last), ->after(text, last), ->index_of(text, last) |
| 文本 | ->replace_pattern(pattern, replacement) |
->replace(/pattern/, replacement),分组时可用 $1 或 ${name} |
| 文本 | ->snake_case(), ->kebab_case(), ->camel_case(), ->pascal_case() |
->case(snake), ->case(kebab), ->case(camel), ->case(pascal) |
| 列表 | ->find_last(fn), ->find_index(fn) |
->find(fn, last)、->index_of(fn)(也可用 last) |
| 列表、映射 | ->sort_by(fn, desc) |
->sort(by: fn, order: desc) |
| 列表 | ->max_by(fn), ->min_by(fn), ->unique_by(fn) |
->max(by: fn), ->min(by: fn), ->unique(by: fn) |
| 列表 | ->reduce(fn) 无初始值,从 null 开始 |
与 ->scan() 一样,第一项即为第一个总计值:[3, 5, 2]->reduce(fn(total, n) => $total + $n) 为 10 |
| 映射 | ->reduce(fn) 无初始值,从 null 开始 |
与列表一样,第一个值即为第一个总计值:{ apples: 3, pears: 5 }->reduce(fn(total, n) => $total + $n) 为 8 |
| 列表 | ->most_common(count?) |
->common(count?),或用 ->common(count?, least) 取最稀有的第一项 |
| 数字 | -5->abs() 解读为 -(5->abs()),即 -5 |
直接写在数字上的负号属于该数字,因此 -5->abs() 为 5;-$x->abs() 仍会对 ->abs() 的结果取反 |
| JSON | ->parse() |
->convert(from: json) |
| 文本、响应 | ->json(as?), ->yaml(), ->csv(header?, as?), ->markdown() |
->convert(from: json, as?)、->convert(from: yaml)、->convert(from: csv, not header, as?)、->convert(from: markdown, to: html);->from(type: json) 为简写形式 |
| 任意值 | ->to_json(indented?)、->to_yaml()、列表的 ->to_csv(headers?) |
->convert(to: json, indented)、->convert(to: yaml)、->convert(to: csv, headers: [...]);->to(type: json) 为简写形式 |
| 文档、Markdown、LaTeX | ->html(), ->latex(), ->markdown() |
->convert(to: html), ->convert(to: latex), ->convert(to: markdown) |
| CSV | ->rows(header?, as?), ->column(at, header?, as?) |
->cells(header?, as?)、->cells(column: at, header?, as?);->cells(row: 0) 读取一行,->cells(columns) 读取每一列,->cells(row: 0, column: 'price') 读取一个单元格。以数字书写的单元格即为数字,表头由第一行推断得出 |
用于 ->shapes(like:) 的遮罩 |
任意带有 layout: string = ... 和 symbols: map = ... 的类型 |
type Tree extends Mask { layout: ..., symbols: { ... } }:只有扩展了 Mask 的类型才是遮罩,其成员无需声明类型 |
| 遮罩 | $grid->shapes(like: Tree, rotated) |
$input->shapes(Tree, rotated):遮罩须放在最前,文本、列表或三维列表与网格一样可用,因此 ->grid() 不必排在最前。like: 是一个错误,附带可改写该调用的修复方案 |
| 遮罩 | symbols: { '#': '#', '.': null },列出每个字符 |
symbols: { '.': any }:字符默认与自身匹配,除非 symbols 另有指定,因此无需更改的遮罩不需要 symbols。null 是一个错误,修复方案会写入 any;'#': '#' 是一个警告(KS6025),修复方案会将其删除 |
| 遮罩 | layout = ..., symbols = ... |
layout: ...、symbols: ...:遮罩的成员使用冒号书写,编辑器的修复会改写为 =。遮罩只能包含 layout、symbols 和方法 |
| 多列表的列表 | ->transpose() |
->cells(columns),用于像读取 csv 那样读取 ->grid() 或任何多列表的列表:->cells(rows)、->cells(row: 0)、->cells(column: 2) 和 ->cells(row: 0, column: 2)。允许行的长度不同:列会跳过长度不足以到达该列的行 |