Skip to content
VaelJS Blog

vael:fmt

Cross-runtime string formatting (sprintf, printf), value inspection (inspect), and ANSI escape code stripping (stripAnsiCode).

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Byte and duration formatters are exposed from "vael:fmt/bytes" and "vael:fmt/duration"; subpath modules "vael:fmt/printf", "vael:fmt/inspect", and "vael:fmt/strip-ansi-code" provide focused entry points for the symbols exported here.

import ... from "vael:fmt"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature: vael-fmt (enabled by default in vaeljs-core and vaeljs-lite-core)
import { echof, errorf, inspect, sprintf, stripAnsiCode } from "vael:fmt";
const msg = sprintf("Hello %s! You have %d messages.", "Alice", 5);
echof("%s", msg);
console.log(inspect({ user: "bob", scores: [95, 87, 92] }));
console.log(stripAnsiCode("\x1b[31mError\x1b[0m: Failed"));
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function sprintf(format: string, ...args: unknown[]): string;

Format args according to format and return the resulting string.

Supports POSIX/Go verbs (%s, %d, %b, %o, %x, %X, %e, %E, %f, %F, %g, %G, %c, %t, %v, %#v, %T, %j, %i, %I, %%), flags (+, -, #, 0, space, <), width/precision (including *), and Go 1-based positional indices (%[2]s). Unmatched verbs, missing arguments, and unused extra arguments produce inline %!(...) diagnostic markers.

Parameters
NameTypeDescription
formatstringFormat template string containing literal text and % directives.
...argsunknown[]Values interpolated by directives in format.
Returns
string — Formatted string.
Exceptions
  • TypeError — when format is not a string.
import { sprintf } from "vael:fmt";
console.log(sprintf("Value: %d, Hex: %#x, Float: %.2f", 255, 255, 3.14159));
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function printf(format: string, ...args: unknown[]): void;

Format args according to format and write the result to stdout without appending a trailing newline.

Parameters
NameTypeDescription
formatstringFormat template string.
...argsunknown[]Values interpolated by directives in format.
Returns
void
Exceptions
  • TypeError — when format is not a string.
import { printf } from "vael:fmt";
printf("Processing: %d%%\n", 75);
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function print(...args: unknown[]): void;

Concatenate args with no separator and write them directly to stdout without a trailing newline.

Parameters
NameTypeDescription
...argsunknown[]Values converted via String(...), concatenated, and written to stdout.
Returns
void
import { print } from "vael:fmt";
print("Hello", "World");
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function echo(...args: unknown[]): void;

Write args separated by single spaces to stdout, followed by a newline (\n).

Parameters
NameTypeDescription
...argsunknown[]Values converted via String(...), joined with spaces, and written to stdout.
Returns
void
import { echo } from "vael:fmt";
echo("Hello", "World");
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function echof(format: string, ...args: unknown[]): void;

Format args according to format and write the result to stdout followed by a newline (\n).

Parameters
NameTypeDescription
formatstringFormat template string.
...argsunknown[]Values interpolated by directives in format.
Returns
void
Exceptions
  • TypeError — when format is not a string.
import { echof } from "vael:fmt";
echof("Name: %-10s Age: %d", "Alice", 30);
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function errorf(format: string, ...args: unknown[]): Error;

Format args according to format and return a new Error instance whose .message is the formatted string.

Parameters
NameTypeDescription
formatstringFormat template string.
...argsunknown[]Values interpolated by directives in format.
Returns
Error — New Error with formatted message.
Exceptions
  • TypeError — when format is not a string.
import { errorf } from "vael:fmt";
throw errorf("File %s not found at line %d", "config.json", 42);
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function setNoColor(noColor: boolean): void;

Enable or disable ANSI color output in %i and %I inspect formatting.

Parameters
NameTypeDescription
noColorbooleanPass true to disable colors or false to allow colors.
Returns
void
Exceptions
  • TypeError — when noColor is not a boolean.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function inspect(value: unknown, options?: InspectOptions): string;

Format any JavaScript value into a human-readable diagnostic string.

Parameters
NameTypeDescription
valueunknownAny value to inspect.
options optionalInspectOptionsOptional formatting configuration.
Returns
string — Formatted string representation of value.
Exceptions
  • TypeError — when options is provided and not an object.
import { inspect } from "vael:fmt";
console.log(inspect({ a: 1, b: [2, 3] }, { compact: false }));
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function stripAnsiCode(text: string): string;

Remove all ANSI escape sequences from text.

Parameters
NameTypeDescription
textstringInput string potentially containing ANSI escape sequences.
Returns
string — Plain string with ANSI escape sequences removed.
Exceptions
  • TypeError — when text is not a string.
import { stripAnsiCode } from "vael:fmt";
console.log(stripAnsiCode("\x1b[31mError\x1b[0m: Something failed"));
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface InspectOptions {
  colors?: boolean;
  compact?: boolean;
  depth?: number;
  breakLength?: number;
  escapeSequences?: boolean;
  iterableLimit?: number;
  showProxy?: boolean;
  sorted?: boolean;
  trailingComma?: boolean;
  getters?: boolean;
  showHidden?: boolean;
  strAbbreviateSize?: number;
}

Formatting options accepted by inspect.

Properties
PropertyTypeDescriptionSince
colors optionalbooleanInclude ANSI color sequences in formatted output. Defaults to false.v0.0.0-alpha.2
compact optionalbooleanCollapse nested objects onto fewer lines when true, or force multi-line expansion when false.v0.0.0-alpha.2
depth optionalnumberMaximum recursion depth when formatting nested objects and collections. Defaults to 4.v0.0.0-alpha.2
breakLength optionalnumberMaximum line length before breaking collections across multiple lines. Defaults to 80.v0.0.0-alpha.2
escapeSequences optionalbooleanEscape control characters in string values. Defaults to true.v0.0.0-alpha.2
iterableLimit optionalnumberMaximum number of array, Map, Set, or object entries to display before truncating. Defaults to 100.v0.0.0-alpha.2
showProxy optionalbooleanInspect Proxy target and handler details when supported.v0.0.0-alpha.2
sorted optionalbooleanSort object property keys lexicographically. Defaults to false.v0.0.0-alpha.2
trailingComma optionalbooleanAppend a trailing comma on multi-line objects and arrays. Defaults to false.v0.0.0-alpha.2
getters optionalbooleanInvoke getter properties when inspecting objects instead of printing [Getter]. Defaults to false.v0.0.0-alpha.2
showHidden optionalbooleanInclude non-enumerable and symbol-keyed properties. Defaults to false.v0.0.0-alpha.2
strAbbreviateSize optionalnumberTruncate string values longer than this character count with "...".v0.0.0-alpha.2