More native types
These work the same way as email and uuid: a value is checked and tidied where it is made, the editor checks values written in code (KS5012), and each type has methods of its own. The names are lowercase only, so a script's own Color or Duration type is never confused with them.
<let name="site" type="url" value="https://example.com:8443/docs?page=2" />
<echo>{ $site.host } { $site.port } { $site.query.page }</echo>
<echo value="$site->with(query: { page: 3 })" />
<echo value="$site->without(query, fragment)" />
<let name="home" type="ip" value="192.168.0.12" />
<echo>{ $home.is_private } { $home in '192.168.0.0/16' }</echo>
<let name="v" type="semver" value="1.4.2" />
<echo>{ $v->satisfies('^1.2.0') } { $v->bump('minor') }</echo>
<let name="uk" type="country" value="'United Kingdom'" />
<echo>{ $uk } { $uk.name } { $uk.currency->format(1234.5) } { $uk.flag }</echo>
<let name="wait" type="duration" value="1h30m" />
<echo>{ $wait.minutes } { $wait + 45m }</echo>
<let name="brand" type="color" value="'#ff8800'" />
<echo>{ $brand->lighten(0.5) } { $brand->contrast_ratio(white) }</echo>
<let name="pay" type="card" value="'4242 4242 4242 4242'" />
<echo>{ $pay } { $pay.brand }</echo>$site: url = https://example.com:8443/docs?page=2
echo "{ $site.host } { $site.port } { $site.query.page }"
echo $site->with(query: { page: 3 })
echo $site->without(query, fragment)
$home: ip = 192.168.0.12
echo "{ $home.is_private } { $home in '192.168.0.0/16' }"
$v: semver = 1.4.2
echo "{ $v->satisfies('^1.2.0') } { $v->bump('minor') }"
$uk: country = 'United Kingdom'
echo "{ $uk } { $uk.name } { $uk.currency->format(1234.5) } { $uk.flag }"
$wait: duration = 1h30m
echo "{ $wait.minutes } { $wait + 45m }"
$brand: color = '#ff8800'
echo "{ $brand->lighten(0.5) } { $brand->contrast_ratio(white) }"
$pay: card = '4242 4242 4242 4242'
echo "{ $pay } { $pay.brand }"Web and network
| Type | What it holds | Tidied | Methods |
|---|---|---|---|
url |
An absolute http, https, ftp, ws or wss URL. |
The scheme and host are lowercased. | .scheme, .host, .port, .path, .query, .fragment, .origin, ->with(path, query, fragment), ->without(query, fragment), ->join(path) |
domain |
A domain name such as api.example.co.uk. International names work. |
Lowercased, without a trailing dot. | .tld, .labels, ->is_subdomain_of(domain), ->ascii() |
ip |
An IPv4 or IPv6 address. | Written the standard way: 2001:db8::1. |
.version, ->in(cidr), .is_private, .is_loopback |
cidr |
A block of addresses, such as 10.0.0.0/8. |
Host bits are cleared: 10.1.2.3/8 is 10.0.0.0/8. |
->contains(ip), .network, .prefix, .size |
mac |
A network card's MAC address. | Six lowercase pairs with :. |
|
port |
A port number from 1 to 65535. It is a number, not text. |
Identifiers and formats
| Type | What it holds | Tidied | Methods |
|---|---|---|---|
slug |
Lowercase words joined by -, for URLs. @slugify(text) makes one. |
.words |
|
file_path |
Where a file or folder is: ./data/input.txt from the script's folder, ../shared/rates.json from the folder above, ~/notes.md from the home folder. Every command that reads or writes files takes one. See file paths. |
Kept as written. | .name, .stem, .extension, .folder, .segments, + 'name' |
slash_path |
Names joined by /: a page, menu or category path such as /docs/getting-started or /My Apps/Notes. It isn't a file: a file is a file_path. See slash paths. |
It always starts with / (home is /home). Spaces around each name, empty parts (//), . and a trailing / are dropped; .. goes up one. Case is kept. |
.segments, .name, .parent, .depth, .is_root, ->join(more), ->is_within(other) |
semver |
A semantic version such as 1.4.2 or 2.0.0-beta.1. |
A leading v is dropped. |
.major, .minor, .patch, .prerelease, ->compare(other), ->satisfies(range), ->bump(part) |
mime |
A media type such as application/json; charset=utf-8. |
The type is lowercased. | .main_type, .subtype, .essence, .params, ->is('text/*') |
hex |
Hexadecimal digits, such as a hash. | Lowercased; 0x is dropped. |
.bytes, ->to_number() |
base64 |
Base64 text, standard or URL-safe. $text->to_base64() makes one. |
->decode(), .bytes |
|
jwt |
A JSON Web Token. Its signature isn't checked, so don't trust its claims for sign-in. | .header, .claims, .expires, .is_expired |
|
json |
Text that holds valid JSON. | ->convert(from: json) |
|
csv |
Text that holds valid CSV, such as a ```csv block. It isn't a file. See CSV. |
.headers, ->cells() with row: or rows, column: or columns, header?, as?, ->save(path) |
People and places
| Type | What it holds | Tidied | Methods |
|---|---|---|---|
phone |
A phone number in E.164: + then the country code and number. |
Spaces, dots, dashes, brackets and a (0) are dropped: +44 (0)7700 900123 is +447700900123. |
.country_code, .national, ->tel() |
country |
An ISO 3166 country. Two-letter and three-letter codes and English names are read. | Two capitals: GB. |
.name, .alpha3, .currency, .flag |
currency |
An ISO 4217 currency code. | Capitals: GBP. |
.name, .symbol, .digits, ->format(amount) |
locale |
A language tag such as en-GB or pt-BR. |
Its usual case: en_gb is en-GB. |
.language, .region, .name, .native_name, ->format(value) |
timezone |
An IANA time zone such as Europe/London, or UTC. |
Its usual case. | ->offset(at?), ->now(), ->convert(time), ->is_dst(at?) |
Time
| Type | What it holds | Tidied | Methods |
|---|---|---|---|
duration |
A length of time: 1h30m, 90s, 2d, years and months such as 5y10mo, ISO 8601 such as PT1H30M or P1Y2M, or a number of seconds. |
The short form: 90m is 1h30m. |
.seconds, .minutes, .hours, .days, .ms, ->iso(), ->after(time?), ->before(time?), ->add(other), ->subtract(other) |
date_format |
How a date is written: dd-MM-yyyy, d MMMM yyyy or HH:mm. Every letter must mean something, so YYYY-DD fails with a hint. See date formats. |
.tokens, .has_date, .has_time, .example, ->format(date), ->parse(text) |
|
cron |
A cron schedule of five fields, or @daily, @hourly and the like. |
Lowercased. | ->next(after?), ->next_times(count, after?) |
color |
#rgb, #rrggbb, #rrggbbaa, rgb(...) or a CSS colour name. |
#rrggbb, or #rrggbbaa with transparency. |
->rgb(), ->hex(), ->css(), ->mix(other, amount?), ->lighten(amount?), ->darken(amount?), .luminance, ->contrast_ratio(other) |
Money
| Type | What it holds | Tidied | Methods |
|---|---|---|---|
iban |
A bank account number, checked with its mod-97 digits. | Capitals without spaces. | .country, ->formatted(), .bban |
card |
A payment card number, checked with the Luhn digit. Like a password, it prints masked: •••• 4242, in logs and JSON too. |
Spaces and dashes are dropped. | .last4, .brand, ->reveal() |
money |
An amount in a currency: GBP 9.50, 9.50 GBP, £9.50, $12 (USD), €3.50, ¥1200 (JPY). The decimal point is always .. |
The code, a space and the currency's decimal places: £9.5 is GBP 9.50. |
.amount, .currency, ->format(locale?), ->add(other, rate?), ->subtract(other, rate?), ->split(parts), ->allocate(ratios) |
A compact amount such as GBP1.0 is a money literal. Use an uppercase ISO currency code followed immediately by an amount. It works in assignments, lists and function arguments. For a negative amount, write -GBP1.0. An explicitly typed variable also accepts text such as $price: money = 'GBP 1.00'.
Addition returns the left amount's currency. To add euros to pounds, supply the number of pounds per euro. You can set the rate once for the running host.
<do>@set_exchange_rate(from: 'EUR', to: 'GBP', rate: 0.80)</do>
<let name="price" value="GBP1.00" />
<let name="extra" value="EUR0.75" />
<echo value="$price + $extra" /> <!-- GBP 1.60 -->
<echo value="$price->add($extra, rate: 0.92)" /> <!-- GBP 1.69 -->@set_exchange_rate(from: 'EUR', to: 'GBP', rate: 0.80)
$price = GBP1.00
$extra = EUR0.75
echo $price + $extra // GBP 1.60
echo $price->add($extra, rate: 0.92) // GBP 1.69To share rates across fresh runs, put them under money.exchange_rates in the config section of kscript.json. The outer key names the incoming currency and the inner key names the receiving currency.
{
"config": {
"money": { "exchange_rates": { "EUR": { "GBP": 0.80 } } }
}
}You can also attach a quote to a particular balance with $price: money(rates: { EUR: 0.85 }) = GBP1.00. For each addition, KScript uses the first available rate in this order.
- An explicit
rate:in->add(), for one call. - The receiving value's
money(rates: ...)table. - A rate registered with
@set_exchange_rate()for this run. - The project's
$config.money.exchange_ratessetting.
+, +=, ->add(), ->subtract() and money[]->total() use the same rule. A list accumulates in its first amount's currency. Each incoming amount rounds to the receiving currency's smallest unit before addition, with ties rounded away from zero. A missing direction raises Type, even when the reverse or an indirect pair exists. Subtraction, comparison and division between two money amounts still require the same currency.
Arguments for the newer types
| Type | Arguments |
|---|---|
url |
schemes (the schemes allowed, such as ['https']), hosts (the hosts allowed) |
ip |
version (4 or 6) |
hex |
bytes (how many bytes it must hold) |
phone |
countries (the calling codes allowed, such as ['+44', '+353']) |
money |
rates (a map from incoming ISO currency codes to positive numeric rates for incoming additions) |
<let name="api" type="url(schemes: ['https'], hosts: ['api.example.com'])" value="https://api.example.com/v1" />
<let name="office" type="ip(version: 4)" value="10.0.0.1" />
<echo>{ $api } { $office }</echo>$api: url(schemes: ['https'], hosts: ['api.example.com']) = https://api.example.com/v1
$office: ip(version: 4) = 10.0.0.1
echo "{ $api } { $office }"