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"
vael-fmt (enabled by default in vaeljs-core and vaeljs-lite-core)Example
Section titled “Example”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.");sprintfv0.0.0-alpha.2args according to format and return the resulting string.printfv0.0.0-alpha.2args according to format and write the result to stdout without appending a trailing newline.printv0.0.0-alpha.2args with no separator and write them directly to stdout without a trailing newline.echov0.0.0-alpha.2args separated by single spaces to stdout, followed by a newline (\n).echofv0.0.0-alpha.2args according to format and write the result to stdout followed by a newline (\n).errorfv0.0.0-alpha.2args according to format and return a new Error instance whose .message is the formatted string.setNoColorv0.0.0-alpha.2%i and %I inspect formatting.Functions
Section titled “Functions”sprintf
Section titled “sprintf”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.
| Name | Type | Description |
|---|---|---|
format | string | Format template string containing literal text and % directives. |
...args | unknown[] | Values interpolated by directives in format. |
string — Formatted string.TypeError— whenformatis 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"printf
Section titled “printf”export function printf(format: string, ...args: unknown[]): void;
Format args according to format and write the result to stdout
without appending a trailing newline.
| Name | Type | Description |
|---|---|---|
format | string | Format template string. |
...args | unknown[] | Values interpolated by directives in format. |
voidTypeError— whenformatis not a string.
import { printf } from "vael:fmt/printf";
printf("Processing: %d%%\n", 75);export function print(...args: unknown[]): void;
Concatenate args with no separator and write them directly to stdout
without a trailing newline.
| Name | Type | Description |
|---|---|---|
...args | unknown[] | Values converted via String(...), concatenated, and written to stdout. |
voidimport { print } from "vael:fmt/printf";
print("Hello", "World"); // writes "HelloWorld"export function echo(...args: unknown[]): void;
Write args separated by single spaces to stdout, followed by a newline
(\n).
| Name | Type | Description |
|---|---|---|
...args | unknown[] | Values converted via String(...), joined with spaces, and written to stdout. |
voidimport { echo } from "vael:fmt/printf";
echo("Hello", "World"); // writes "Hello World\n"export function echof(format: string, ...args: unknown[]): void;
Format args according to format and write the result to stdout
followed by a newline (\n).
| Name | Type | Description |
|---|---|---|
format | string | Format template string. |
...args | unknown[] | Values interpolated by directives in format. |
voidTypeError— whenformatis not a string.
import { echof } from "vael:fmt/printf";
echof("Server listening on port %d", 8080);errorf
Section titled “errorf”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.
| Name | Type | Description |
|---|---|---|
format | string | Format template string. |
...args | unknown[] | Values interpolated by directives in format. |
Error — New Error with formatted message.TypeError— whenformatis not a string.
import { errorf } from "vael:fmt/printf";
throw errorf("Config %s not found at line %d", "vael.jsonc", 12);setNoColor
Section titled “setNoColor”export function setNoColor(noColor: boolean): void;
Enable or disable ANSI color output in %i and %I inspect formatting.
| Name | Type | Description |
|---|---|---|
noColor | boolean | Pass true to disable colors or false to allow colors. |
voidTypeError— whennoColoris not a boolean.
import { setNoColor, sprintf } from "vael:fmt/printf";
setNoColor(true);console.log(sprintf("%I", { ok: true }));