Last updated

Free-function string utilities: the subject string is always the first argument. Semantics match the JS builtins wherever one exists (UTF-16 indices, locale-independent case). Argument types are checked strictly (non-strings throw); validators return false on non-strings instead.

const str = require("std/str");console.log(str.camelCase("hello-world_foo")); // "helloWorldFoo"console.log(str.slugify("Hello, World!"));     // "hello-world"console.log(str.levenshtein("kitten", "sitting")); // 3console.log(str.isEmail("a@b.com"), str.isPort("443")); // true true
FunctionDescription
indexOf(s, needle[, from]) -> numberFirst UTF-16 offset at/after from, -1 when absent
lastIndexOf(s, needle[, from]) -> numberLast offset at/before from, -1 when absent
includes(s, needle[, from]) -> boolTrue when needle occurs at/after from
count(s, needle) -> numberNon-overlapping occurrences; empty needle gives length + 1
equals(a, b) -> boolTrue for an exact ( ===) match
startsWith(s, prefix) -> boolPrefix check
endsWith(s, suffix) -> boolSuffix check

🔗Case and trim

FunctionDescription
toUpperCase(s) -> stringUpper-cases (JS semantics)
toLowerCase(s) -> stringLower-cases (JS semantics)
trim(s) -> stringStrips leading and trailing whitespace
trimStart(s) -> stringStrips leading whitespace
trimEnd(s) -> stringStrips trailing whitespace
isAscii(s) -> boolTrue when every UTF-16 unit is below 0x80
isBlank(s) -> boolTrue when empty or all whitespace

🔗Split and join

FunctionDescription
lines(s) -> string[]Splits on \n, stripping one trailing \r per line ( "" gives [""])
split(s, sep) -> string[]Splits on a separator ( "" splits into UTF-16 units)
join(parts[, sep = ","]) -> stringJoins a string array (every element must be a string)
words(s) -> string[]Splits on whitespace runs, dropping empties ( "" gives [])

🔗Replace

FunctionDescription
replace(s, search, replacement) -> stringFirst occurrence ( $-patterns stay literal)
replaceAll(s, search, replacement) -> stringEvery non-overlapping occurrence

🔗Shape

FunctionDescription
slice(s[, start[, end]]) -> stringJS slice (negatives count from the end)
padStart(s, width[, pad]) -> stringPads left to width
padEnd(s, width[, pad]) -> stringPads right to width
padCenter(s, width[, pad]) -> stringCenters (extra padding goes right)
repeat(s, n) -> stringRepeats (native RangeError on bad counts)
reverse(s) -> stringReverses by code points (astral characters intact)
truncate(s, maxLen[, ellipsis]) -> stringCuts to maxLen code points, appending ellipsis when cut
chomp(s) -> stringDrops one trailing newline
collapseWhitespace(s) -> stringCollapses whitespace runs to one space, trims ends

🔗Case converters

Word split is non-alphanumeric runs plus camel boundaries:

FunctionDescription
camelCase(s) -> stringhello-world_foo → helloWorldFoo
snakeCase(s) -> stringhelloWorld → hello_world
kebabCase(s) -> stringhelloWorld → hello-world
titleCase(s) -> stringhello world → Hello World
slugify(s) -> stringLower-cases, maps runs to single dashes

🔗HTML, distance, layout

FunctionDescription
escapeHtml(s) -> stringEscapes &<>"' to entities
unescapeHtml(s) -> stringUnescapes named/numeric entities; unknown pass through
levenshtein(a, b) -> numberCode-point edit distance (throws past 4M DP cells)
wordWrap(s, width) -> stringGreedy wrap keeping blank-line paragraphs
indent(s, prefix) -> stringPrefixes every line
dedent(s) -> stringDrops the common space/tab margin
stripAnsi(s) -> stringDrops CSI/OSC/ESC escape sequences

🔗Validators

Non-strings are false, not throws (isPort also accepts numbers):

FunctionDescription
isEmail(s) -> bool

< 256 chars | | `isDomain(s) -> bool` | LDH labels, alpha TLD | | `isIPv4(s) -> bool` | Strict dotted quad (no leading zeros) | | `isIPv6(s) -> bool` | Strict IPv6 (8 groups or one `::`) | | `isIP(s) -> bool` | IPv4 or IPv6 | | `isWebUrl(s) -> bool` | Practical http(s) URL check | | `isUUID(s) -> bool` | Canonical 8-4-4-4-12 hex | | `isPort(v) -> bool` | Integer port 0–65535 (numeric strings ok, floats rejected) |