Browser-safe utilities for transforming, matching, and measuring strings.
These utilities have no runtime-native dependencies and work in current browsers, Node, Deno, and Bun.
import string from "@rcompat/string";string.Base64#
UTF-8 Base64 encoding and decoding operations.
string.Base64: Base64Utilities;Base64 methods#
string.Base64.encode()#
Encodes a string's UTF-8 bytes as Base64.
Parameters
decoded— String to encode.
string.Base64.encode(decoded: string): string;string.Base64.decode()#
Decodes Base64 bytes as a UTF-8 string.
Parameters
encoded— Base64 string to decode.
string.Base64.decode(encoded: string): string;string.dedent#
Removes common indentation and trims a string or tagged template.
Interpolated values are string-coerced. Escaped newlines are removed and escaped backticks are restored before indentation is calculated.
Parameters
strings— String value or template segments.values— Tagged-template interpolation values.
Example
const name = "Ada";
string.dedent`
Hello, ${name}.
Welcome back.
`; // "Hello, Ada.\nWelcome back."string.dedent(strings: TemplateStrings, ...values: unknown[]): string;string.glob#
Compiles a whole-string glob pattern to a Unicode regular expression.
* excludes /, ** includes /, and ** followed by / matches zero
or more path segments. ? matches one character, and bracket classes and
ranges are supported. Other regular-expression syntax is escaped. Input
validation is consistent with
assert.string().
Parameters
pattern— Glob pattern to compile.
Returns: An anchored regular expression.
Example
string.glob("src/**.ts").test("src/private/index.ts"); // truestring.glob(pattern: string): RegExp;string.toCamelCase#
Converts hyphen- or underscore-separated text to camel case.
The complete input is lowercased first. Repeated and surrounding separators are ignored.
Parameters
string— Text to convert.
string.toCamelCase(string: string): string;string.toSlug#
Converts text to a lowercase ASCII URL slug.
Latin diacritics are removed, non-ASCII characters are discarded, and
runs of remaining punctuation or whitespace become one -.
Parameters
string— Text to convert.
string.toSlug(string: string): string;string.upperfirst#
Uppercases the first character and leaves the remainder unchanged.
Parameters
string— Text to transform.
string.upperfirst(string: string): string;string.utf8#
UTF-8 byte-size operations.
string.utf8: Utf8Utilities;UTF-8 methods#
string.utf8.bytelength()#
Returns UTF-8 byte length by encoding the string with TextEncoder.
Parameters
string— String to measure.
string.utf8.bytelength(string: string): number;string.utf8.size()#
Calculates UTF-8 byte size directly without allocating an encoded array.
Parameters
string— String to measure.
string.utf8.size(string: string): number;Supporting contracts#
Base64Utilities#
UTF-8 Base64 encoding and decoding operations.
interface Base64Utilities {
// Encodes a string's UTF-8 bytes as Base64.
encode(decoded: string): string;
// Decodes Base64 bytes as a UTF-8 string.
decode(encoded: string): string;
}Utf8Utilities#
UTF-8 byte-size operations.
interface Utf8Utilities {
// Returns UTF-8 byte length by encoding the string with `TextEncoder`.
bytelength(string: string): number;
// Calculates UTF-8 byte size directly without allocating an encoded array.
size(string: string): number;
}TemplateStrings#
String or template segments accepted by string.dedent.
type TemplateStrings = string | TemplateStringsArray;