Skip to content
VaelJS Blog

vael:strings

Unicode 16.0 case-folding string comparisons and code-point trimming.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Implements locale-independent Go strings.EqualFold semantics over UTF-16 strings and code-point-based trimming.

import ... from "vael:strings"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: vael-strings (enabled by default in vaeljs-core and vaeljs-lite-core)
import { equalFold, indexOfFold, trim } from "vael:strings";
console.log(equalFold("GoLang", "golang")); // true
console.log(indexOfFold("Hello World", "WORLD")); // 6
console.log(trim("///path/to/file///", "/")); // "path/to/file"
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function equalFold(text: string, other: string): boolean;

Compare text and other under locale-independent Unicode 16.0 simple case folding (Go strings.EqualFold semantics over UTF-16 code units).

Lone surrogates fold to themselves and never match half of a surrogate pair.

Parameters
NameTypeDescription
textstringFirst string to compare.
otherstringSecond string to compare.
Returns
boolean — true if text and other are equal under Unicode simple case folding.
Exceptions
  • TypeError — when text or other is not a string.
import { equalFold } from "vael:strings";
console.log(equalFold("Kelvin", "Kelvin")); // true
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function startsWithFold(text: string, prefix: string): boolean;

Return true if text starts with prefix under Unicode 16.0 simple case folding.

Parameters
NameTypeDescription
textstringInput string.
prefixstringPrefix string to match.
Returns
boolean — true if text starts with prefix under case folding.
Exceptions
  • TypeError — when text or prefix is not a string.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function endsWithFold(text: string, suffix: string): boolean;

Return true if text ends with suffix under Unicode 16.0 simple case folding.

Parameters
NameTypeDescription
textstringInput string.
suffixstringSuffix string to match.
Returns
boolean — true if text ends with suffix under case folding.
Exceptions
  • TypeError — when text or suffix is not a string.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function indexOfFold(text: string, search: string, fromIndex?: number): number;

Return the UTF-16 code-unit index of the first occurrence of search in text at or after fromIndex under Unicode 16.0 simple case folding, or -1 if not found.

Parameters
NameTypeDescription
textstringInput string to search.
searchstringSubstring to locate.
fromIndex optionalnumberOptional UTF-16 code-unit offset (clamped at 0 and text.length; Infinity / -Infinity clamp, NaN and fractions throw RangeError).
Returns
number — UTF-16 code-unit index of the first match, or -1.
Exceptions
  • TypeError — when text or search is not a string, or fromIndex is defined and not a number.
  • RangeError — when fromIndex is NaN or a non-integer finite number.
import { indexOfFold } from "vael:strings";
console.log(indexOfFold("alpha BETA beta", "beta", 7)); // 11
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function lastIndexOfFold(text: string, search: string, fromIndex?: number): number;

Return the UTF-16 code-unit index of the last occurrence of search in text starting at or before fromIndex under Unicode 16.0 simple case folding, or -1 if not found.

Parameters
NameTypeDescription
textstringInput string to search.
searchstringSubstring to locate.
fromIndex optionalnumberOptional UTF-16 code-unit offset (defaults to text.length).
Returns
number — UTF-16 code-unit index of the last match, or -1.
Exceptions
  • TypeError — when text or search is not a string, or fromIndex is defined and not a number.
  • RangeError — when fromIndex is NaN or a non-integer finite number.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function trim(text: string, chars?: string): string;

Remove leading and trailing code points contained in chars (or standard ECMAScript String.prototype.trim whitespace when chars is omitted).

Preserves lone surrogates in the retained interior.

Parameters
NameTypeDescription
textstringInput string to trim.
chars optionalstringOptional cutset string of code points to strip from both ends.
Returns
string — Trimmed string.
Exceptions
  • TypeError — when text is not a string, or chars is defined and not a string.
import { trim } from "vael:strings";
console.log(trim("--hello--", "-")); // "hello"
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function trimStart(text: string, chars?: string): string;

Remove leading code points contained in chars (or ECMAScript whitespace when chars is omitted).

Parameters
NameTypeDescription
textstringInput string to trim.
chars optionalstringOptional cutset string of code points to strip from the start.
Returns
string — String with matching leading code points removed.
Exceptions
  • TypeError — when text is not a string, or chars is defined and not a string.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function trimEnd(text: string, chars?: string): string;

Remove trailing code points contained in chars (or ECMAScript whitespace when chars is omitted).

Parameters
NameTypeDescription
textstringInput string to trim.
chars optionalstringOptional cutset string of code points to strip from the end.
Returns
string — String with matching trailing code points removed.
Exceptions
  • TypeError — when text is not a string, or chars is defined and not a string.