slug.string
slug.string — string manipulation utilities
Trimming, splitting, joining, searching, replacing, case conversion, padding, case formatting, and random string generation.
All functions treat strings as sequences of Unicode code points where relevant (e.g. indexOf is Unicode-aware).
TOC
camelCase(s, sep)contains(str, seq)containsAny(str, seq)endsWith(str, end)fromCodePoint(codePoint)indexOf(str, seq, index)isLower(str)isUpper(str)join(strs, delimiter, str)kebabCase(s, sep)lastIndexOf(str, seq, index, prev)padLeft(str, with, length)padRight(str, with, length)pascalCase(s, sep)randomHexString(length)randomString(length, chars, acc)replaceAll(str, replace, with)snakeCase(s, sep, screaming)split(str, delimiter, max, count, strs)startsWith(str, start)toLower(str)toUpper(str)trim(str)
Functions
camelCase(s, sep)
fn slug.string#camelCase(s:str, sep:str = " "):str
converts a string to camelCase, capitalising each word except the first.
| Parameter | Type | Default |
|---|---|---|
s | str | — |
sep | str | " " |
Examples
camelCase("LAST NAME") // => "lastName"
camelCase("last name") // => "lastName"
camelCase("l") // => "l"
camelCase("l n") // => "lN"
camelCase("l n") // => "lN"
contains(str, seq)
fn slug.string#contains(str:str, seq:str):bool
returns true if seq appears anywhere in str.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
seq | str | — |
Examples
contains("hello slug", "slug") // => true
contains("hello slug", "snail") // => false
containsAny(str, seq)
fn slug.string#containsAny(str:str, ...seq):bool
returns true if any on the seq strings appear in str.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
seq | — |
Examples
containsAny("hello slug", "slug") // => true
containsAny("hello slug", "snail", "fish") // => false
endsWith(str, end)
fn slug.string#endsWith(str:str, end:str):bool
returns true if str ends with end.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
end | str | — |
Examples
endsWith("hello slug", "slug") // => true
endsWith("hello slug", "hello") // => false
fromCodePoint(codePoint)
fn slug.string#fromCodePoint(codePoint:num):str
returns the string corresponding to a Unicode code point.
| Parameter | Type | Default |
|---|---|---|
codePoint | num | — |
Examples
fromCodePoint(65) // => "A"
fromCodePoint(129315) // => "🤣"
indexOf(str, seq, index)
fn slug.string#indexOf(str:str, seq:str, index:num = 0):num
returns the byte index of the first occurrence of seq in str at or after index.
Returns -1 if not found. Unicode-aware.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
seq | str | — |
index | num | 0 |
Examples
indexOf("hello slug", "zoo") // => -1
indexOf("hello slug", "lu") // => 7
indexOf("hello slug", "l") // => 2
indexOf("hello slug", "l", 3) // => 3
indexOf("hello slug", "l", 4) // => 7
indexOf("éé|éé", "|") // => 2
isLower(str)
fn slug.string#isLower(str:str):bool
returns true if str is non-empty and entirely lowercase.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
Examples
isLower(nil) // => false
isLower("") // => false
isLower("slug") // => true
isLower("SLUG") // => false
isUpper(str)
fn slug.string#isUpper(str:str):bool
returns true if str is non-empty and entirely uppercase.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
Examples
isUpper(nil) // => false
isUpper("") // => false
isUpper("slug") // => false
isUpper("SLUG") // => true
join(strs, delimiter, str)
fn slug.string#join(strs:list<str>, delimiter:str = "", str:str|nil = nil):str
joins a list of strings with delimiter between each element.
Returns an empty string for an empty list.
| Parameter | Type | Default |
|---|---|---|
strs | list | — |
delimiter | str | "" |
str | str | nil | nil |
Examples
join([], ".") // => ""
join(["slug"], ".") // => "slug"
join(["slug", "test"], ".") // => "slug.test"
kebabCase(s, sep)
fn slug.string#kebabCase(s:str, sep:str = " "):str
converts a string to kebab-case by replacing sep with -.
| Parameter | Type | Default |
|---|---|---|
s | str | — |
sep | str | " " |
Examples
kebabCase("LAST NAME") // => "LAST-NAME"
kebabCase("last name") // => "last-name"
kebabCase("l") // => "l"
kebabCase("l n") // => "l-n"
kebabCase("l n") // => "l---n"
lastIndexOf(str, seq, index, prev)
fn slug.string#lastIndexOf(str:str, seq:str, index:num = 0, prev:num = (-1)):num
returns the index of the last occurrence of seq in str.
Returns -1 if not found.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
seq | str | — |
index | num | 0 |
prev | num | (-1) |
Examples
lastIndexOf("hello slug", "zoo") // => -1
lastIndexOf("hello slug", "l") // => 7
lastIndexOf("hello slug", "h") // => 0
lastIndexOf("hello slug", "g") // => 9
padLeft(str, with, length)
fn slug.string#padLeft(str:str, with:str, length:num):str
pads str on the left with with until it reaches length.
Returns str unchanged if already at or above length.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
with | str | — |
length | num | — |
padRight(str, with, length)
fn slug.string#padRight(str:str, with:str, length:num):str
pads str on the right with with until it reaches length.
Returns str unchanged if already at or above length.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
with | str | — |
length | num | — |
pascalCase(s, sep)
fn slug.string#pascalCase(s:str, sep:str = " "):str
converts a string to PascalCase, capitalising the first letter of each word.
| Parameter | Type | Default |
|---|---|---|
s | str | — |
sep | str | " " |
Examples
pascalCase("LAST NAME") // => "LastName"
pascalCase("last name") // => "LastName"
pascalCase("l") // => "L"
pascalCase("l n") // => "LN"
pascalCase("l n") // => "LN"
randomHexString(length)
fn slug.string#randomHexString(length:num):str
generates a random lowercase hex string of length characters.
@effects(‘random’)
| Parameter | Type | Default |
|---|---|---|
length | num | — |
Effects: random
randomString(length, chars, acc)
fn slug.string#randomString(length:num, chars:str, acc:str = ""):str
generates a random string of length characters drawn from chars.
@effects(‘random’)
| Parameter | Type | Default |
|---|---|---|
length | num | — |
chars | str | — |
acc | str | "" |
Effects: random
replaceAll(str, replace, with)
fn slug.string#replaceAll(str:str|nil, replace:str, with:str):str|nil
replaces all occurrences of replace in str with with.
Returns nil for nil input.
| Parameter | Type | Default |
|---|---|---|
str | str | nil | — |
replace | str | — |
with | str | — |
Examples
replaceAll(nil, "/", ".") // => nil
replaceAll("slug/test", "/", ".") // => "slug.test"
replaceAll("slug", "/", ".") // => "slug"
replaceAll("E & S", "&", "&") // => "E & S"
snakeCase(s, sep, screaming)
fn slug.string#snakeCase(s:str, sep:str = " ", screaming = false):str
converts a string to snake_case by replacing sep with _.
Pass screaming: true for SCREAMING_SNAKE_CASE.
| Parameter | Type | Default |
|---|---|---|
s | str | — |
sep | str | " " |
screaming | false |
Examples
snakeCase("LAST NAME", " ", true) // => "LAST_NAME"
snakeCase("last name", " ", true) // => "LAST_NAME"
snakeCase("LAST NAME") // => "LAST_NAME"
snakeCase("last name") // => "last_name"
snakeCase("l") // => "l"
snakeCase("l n") // => "l_n"
snakeCase("l n") // => "l___n"
split(str, delimiter, max, count, strs)
fn slug.string#split(str:str, delimiter:str, max:num = (-1), count:num = 1, strs:list<str> = []):list<str>
splits str on delimiter, returning a list of substrings.
max limits the number of splits performed; -1 means unlimited. The remaining unsplit portion is always included as the last element.
count and strs are internal accumulators — do not pass them directly.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
delimiter | str | — |
max | num | (-1) |
count | num | 1 |
strs | list | [] |
Examples
split("slug/test", "/") // => ["slug", "test"]
split("éé|éé", "|") // => ["éé", "éé"]
split("a|b|c", "|", 2) // => ["a", "b|c"]
startsWith(str, start)
fn slug.string#startsWith(str:str, start:str):bool
returns true if str begins with start.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
start | str | — |
Examples
startsWith("hello slug", "slug") // => false
startsWith("hello slug", "hello") // => true
toLower(str)
fn slug.string#toLower(str:str):str|nil
converts a string to lowercase.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
Examples
toLower(nil) // => nil
toLower("") // => ""
toLower("SLUG") // => "slug"
toUpper(str)
fn slug.string#toUpper(str:str):str|nil
converts a string to uppercase.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
Examples
toUpper(nil) // => nil
toUpper("") // => ""
toUpper("slug") // => "SLUG"
trim(str)
fn slug.string#trim(str:str):str|nil
fn slug.string#trim(s:str|nil, prefix:str):str|nil
removes all leading and trailing occurrences of prefix from s.
Repeatedly strips the prefix/suffix until neither end matches. Note: prefix is stripped from both ends, not just the left.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
Examples
trim(" a ", " ") // => "a"
trim("xxaxxx", "x") // => "a"
trim("xxa", "x") // => "a"
trim("axxx", "x") // => "a"
trim("abxxx", "xx") // => "abx"