Skip to content
VaelJS Blog

vael:fmt/printf

POSIX and Go-inspired sprintf/printf formatting and stdout helpers.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Supports %s, %d, %b, %o, %x, %X, %e, %E, %f, %F, %g, %G, %c, %t, %v, %T, %j, %i, %I, %%, width/precision (*), flags (+, -, #, 0, ' ', <), and Go positional indices (%[n]).

import ... from "vael:fmt/printf"
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 { echo, echof, errorf, sprintf } from "vael:fmt/printf";
console.log(sprintf("Hello %s! Count: %04d", "Vael", 7));
echof("Progress: %.1f%%", 99.5);
echo("Done.");
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/printf";
console.log(sprintf("%[2]s %[1]s", "World", "Hello")); // "Hello World"
console.log(sprintf("%#x %.2f", 255, 3.14159)); // "0xff 3.14"
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";
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/printf";
print("Hello", "World"); // writes "HelloWorld"
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/printf";
echo("Hello", "World"); // writes "Hello World\n"
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/printf";
echof("Server listening on port %d", 8080);
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/printf";
throw errorf("Config %s not found at line %d", "vael.jsonc", 12);
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.
import { setNoColor, sprintf } from "vael:fmt/printf";
setNoColor(true);
console.log(sprintf("%I", { ok: true }));