Browse API packages

API · v0.22.1

string

Standard library strings

Member signatures use schematic object.member(...) API notation. Exported type declarations are valid TypeScript.

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.

ts
import string from "@rcompat/string";

string.Base64#

UTF-8 Base64 encoding and decoding operations.

ts
string.Base64: Base64Utilities;

Base64 methods#

string.Base64.encode()#

Encodes a string's UTF-8 bytes as Base64.

Parameters

  • decoded — String to encode.
ts
string.Base64.encode(decoded: string): string;

string.Base64.decode()#

Decodes Base64 bytes as a UTF-8 string.

Parameters

  • encoded — Base64 string to decode.
ts
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

ts
const name = "Ada";
string.dedent`
  Hello, ${name}.
  Welcome back.
`; // "Hello, Ada.\nWelcome back."
ts
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

ts
string.glob("src/**.ts").test("src/private/index.ts"); // true
ts
string.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.
ts
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.
ts
string.toSlug(string: string): string;

string.upperfirst#

Uppercases the first character and leaves the remainder unchanged.

Parameters

  • string — Text to transform.
ts
string.upperfirst(string: string): string;

string.utf8#

UTF-8 byte-size operations.

ts
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.
ts
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.
ts
string.utf8.size(string: string): number;

Supporting contracts#

Base64Utilities#

UTF-8 Base64 encoding and decoding operations.

ts
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.

ts
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.

ts
type TemplateStrings = string | TemplateStringsArray;