Methods and fields
A plain fact about a value is a field, read with a dot and no brackets: $file.name, $file.size, $response.status, $date.year. Calling one ($file->name()) is an error that says so. Counting is ->count() on every value.
| On | Before | Now |
|---|---|---|
| a list, a map, text | ->length() |
->count() |
| text | $text->is_empty() to accept empty or whitespace-only text |
Use $text->is_blank() for that check. $text->is_empty() now tests for zero characters. |
| a list | ->where(fn), ->append(item) |
->filter(fn), ->push(item) |
| a list | $items->count_of($value) to count strict matches |
Use $items->count_of(fn(x) => $x === $value) for strict matches. Passing a value now compares with ==. |
| a number | ->pow(2) |
$n ** 2 |
| a number | $n->between(1, 10) |
1 <= $n <= 10, a chain of comparisons |
| a response | ->ok() |
.is_ok |
| an ip | ->private(), ->loopback() |
.is_private, .is_loopback |
| a jwt | ->expired() |
.is_expired |
| a database | ->in_transaction() |
.is_in_transaction |
| a password | ->strong() |
->is_strong() |
| a logger | ->enabled(level) |
->is_enabled(level) |
| a timezone | ->dst() |
->is_dst() |
| a cache | ->put(key, value), ->forget(key), ->flush() |
->set(key, value), ->remove(key), ->clear() |
| a session | ->put(key, value), ->forget(key) |
->set(key, value), ->remove(key) |
| a storage folder | ->path(name) |
->path_of(name); .path is the folder itself |
| a color | ->contrast(other) |
->contrast_ratio(other) |
| a query, a model | ->avg(column) |
->average(column) |
| a url | ->with_query(values) |
->with(query: values); ->without(query, fragment) drops whole parts |
| a date | ->add_years(n), ->add_months(n), ->add_days(n) |
->add(years: n, months: n, days: n), with weeks: too; ->subtract(...) goes back |
| a date | ->add_hours(n), ->add_minutes(n) |
a duration: $date + 3h, $date - 45m |
| a date | ->duration_since(other) |
$date - $other, which is a duration |
| a date | ->months_since(other), ->years_since(other) |
->since(other, in: months), ->since(other, in: years) (also days and weeks) |
| a date | $date + 1, $date - 1 (days), $a - $b (a number of days) |
$date + 1d or ->add(days: 1); $a - $b is now a duration |
| a duration | ->plus(other), ->minus(other) |
->add(other), ->subtract(other) |
| text | ->trim_start(chars?), ->trim_end(chars?) |
->trim(start), ->trim(chars, end) |
| text | ->pad_left(width, char?), ->pad_right(width, char?) |
->pad(width, char?, start), ->pad(width, char?) |
| text | ->strip_prefix(text), ->strip_suffix(text) |
->strip(prefix: text), ->strip(suffix: text), or both at once |
| text | ->before_last(text), ->after_last(text), ->last_index_of(text) |
->before(text, last), ->after(text, last), ->index_of(text, last) |
| text | ->replace_pattern(pattern, replacement) |
->replace(/pattern/, replacement), with $1 or ${name} for groups |
| text | ->snake_case(), ->kebab_case(), ->camel_case(), ->pascal_case() |
->case(snake), ->case(kebab), ->case(camel), ->case(pascal) |
| a list | ->find_last(fn), ->find_index(fn) |
->find(fn, last), ->index_of(fn) (also last) |
| a list, a map | ->sort_by(fn, desc) |
->sort(by: fn, order: desc) |
| a list | ->max_by(fn), ->min_by(fn), ->unique_by(fn) |
->max(by: fn), ->min(by: fn), ->unique(by: fn) |
| a list | ->reduce(fn) with no initial value started from null |
the first item is the first total, as ->scan() does: [3, 5, 2]->reduce(fn(total, n) => $total + $n) is 10 |
| a map | ->reduce(fn) with no initial value started from null |
the first value is the first total, as for a list: { apples: 3, pears: 5 }->reduce(fn(total, n) => $total + $n) is 8 |
| a list | ->most_common(count?) |
->common(count?), or ->common(count?, least) for the rarest first |
| a number | -5->abs() read as -(5->abs()), which is -5 |
a minus written straight onto a number is part of it, so -5->abs() is 5; -$x->abs() still negates what ->abs() gives |
| json | ->parse() |
->convert(from: json) |
| text, a response | ->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) is the short form |
| any value | ->to_json(indented?), ->to_yaml(), a list's ->to_csv(headers?) |
->convert(to: json, indented), ->convert(to: yaml), ->convert(to: csv, headers: [...]); ->to(type: json) is the short form |
| a document, 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) reads one row, ->cells(columns) every column and ->cells(row: 0, column: 'price') one cell. Cells written as numbers are numbers, and the header is worked out from the first line |
a mask for ->shapes(like:) |
any type with layout: string = ... and symbols: map = ... |
type Tree extends Mask { layout: ..., symbols: { ... } }: only a type that extends Mask is a mask, and its members need no types |
| a mask | $grid->shapes(like: Tree, rotated) |
$input->shapes(Tree, rotated): the mask goes first, and text, a list or a 3D list work as well as a grid, so ->grid() isn't needed first. like: is an error with a fix that rewrites the call |
| a mask | symbols: { '#': '#', '.': null }, every character listed |
symbols: { '.': any }: a character matches itself unless symbols says otherwise, so a mask with none to change needs no symbols. null is an error with a fix that writes any, and '#': '#' is a warning (KS6025) with a fix that removes it |
| a mask | layout = ..., symbols = ... |
layout: ..., symbols: ...: a mask's members are written with a colon, and the editor's fix rewrites =. A mask holds only layout, symbols and methods |
| a list of lists | ->transpose() |
->cells(columns), which reads a ->grid() or any list of lists the way a csv is read: ->cells(rows), ->cells(row: 0), ->cells(column: 2) and ->cells(row: 0, column: 2). Rows of different lengths are allowed: a column skips the rows too short to reach it |