Skip to main content

@std/fmt@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score58%
Published2 years ago (0.215.0)
bytes

Functions

f
format(
num: number,
options?: FormatOptions
): string

Convert bytes to a human-readable string: 1337 → 1.34 kB

Interfaces

I

Options for format.

  • binary: boolean

    Uses binary bytes (e.g. kibibyte).

  • bits: boolean

    Uses bits representation.

  • locale: boolean | string | string[]

    Uses localized number formatting. If it is set to true, uses default locale on the system. If it's set to string, uses that locale. The given string should be a BCP 47 language tag. You can also give the list of language tags.

  • The maximum number of fraction digits to display. If neither minimumFractionDigits or maximumFractionDigits are set, the default behavior is to round to 3 significant digits.

  • The minimum number of fraction digits to display. If neither minimumFractionDigits or maximumFractionDigits are set, the default behavior is to round to 3 significant digits.

  • signed: boolean

    Include plus sign for positive numbers.

colors

String formatters and utilities for dealing with ANSI color codes.

Examples

Example 1

import {
  bgBlue,
  bgRgb24,
  bgRgb8,
  bold,
  italic,
  red,
  rgb24,
  rgb8,
} from "@std/fmt/colors";

console.log(bgBlue(italic(red(bold("Hello, World!")))));

// also supports 8bit colors

console.log(rgb8("Hello, World!", 42));

console.log(bgRgb8("Hello, World!", 42));

// and 24bit rgb

console.log(rgb24("Hello, World!", {
  r: 41,
  g: 42,
  b: 43,
}));

console.log(bgRgb24("Hello, World!", {
  r: 41,
  g: 42,
  b: 43,
}));

Functions

f
bgBlack(str: string): string

Set background color to black.

f
bgBlue(str: string): string

Set background color to blue.

f
bgBrightBlack(str: string): string

Set background color to bright black.

f
bgBrightBlue(str: string): string

Set background color to bright blue.

f
bgBrightCyan(str: string): string

Set background color to bright cyan.

f
bgBrightGreen(str: string): string

Set background color to bright green.

f
bgBrightMagenta(str: string): string

Set background color to bright magenta.

f
bgBrightRed(str: string): string

Set background color to bright red.

f
bgBrightWhite(str: string): string

Set background color to bright white.

f
bgBrightYellow(str: string): string

Set background color to bright yellow.

f
bgCyan(str: string): string

Set background color to cyan.

f
bgGreen(str: string): string

Set background color to green.

f
bgMagenta(str: string): string

Set background color to magenta.

f
bgRed(str: string): string

Set background color to red.

f
bgRgb24(
str: string,
color: number | Rgb
): string

Set background color using 24bit rgb. color can be a number in range 0x000000 to 0xffffff or an Rgb.

f
bgRgb8(
str: string,
color: number
): string

Set background color using paletted 8bit colors. https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit

f
bgWhite(str: string): string

Set background color to white.

f
bgYellow(str: string): string

Set background color to yellow.

f
black(str: string): string

Set text color to black.

f
blue(str: string): string

Set text color to blue.

f
bold(str: string): string

Make the text bold.

f
brightBlack(str: string): string

Set text color to bright black.

f
brightBlue(str: string): string

Set text color to bright blue.

f
brightCyan(str: string): string

Set text color to bright cyan.

f
brightGreen(str: string): string

Set text color to bright green.

f
brightMagenta(str: string): string

Set text color to bright magenta.

f
brightRed(str: string): string

Set text color to bright red.

f
brightWhite(str: string): string

Set text color to bright white.

f
brightYellow(str: string): string

Set text color to bright yellow.

f
cyan(str: string): string

Set text color to cyan.

f
dim(str: string): string

The text emits only a small amount of light.

f
getColorEnabled(): boolean

Get whether text color change is enabled or disabled.

f
gray(str: string): string

Set text color to gray.

f
green(str: string): string

Set text color to green.

f
hidden(str: string): string

Make the text hidden.

f
inverse(str: string): string

Invert background color and text color.

f
italic(str: string): string

Make the text italic.

f
magenta(str: string): string

Set text color to magenta.

f
red(str: string): string

Set text color to red.

f
reset(str: string): string

Reset the text modified.

f
rgb24(
str: string,
color: number | Rgb
): string

Set text color using 24bit rgb. color can be a number in range 0x000000 to 0xffffff or an Rgb.

f
rgb8(
str: string,
color: number
): string

Set text color using paletted 8bit colors. https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit

f
setColorEnabled(value: boolean): void

Set changing text color to enabled or disabled

f
strikethrough(str: string): string

Put horizontal line through the center of the text.

f
stripAnsiCode(string: string): string

Remove ANSI escape codes from the string.

f
underline(str: string): string

Make the text underline.

f
white(str: string): string

Set text color to white.

f
yellow(str: string): string

Set text color to yellow.

f
stripColor(string: string): string

Remove ANSI escape codes from the string.

Interfaces

I

RGB 8-bits per channel. Each in range 0->255 or 0x00->0xff

  • b: number

    Blue component value

  • g: number

    Green component value

  • r: number

    Red component value

duration

Format milliseconds to time duration.

Functions

f
format(
ms: number,
options?: Partial<PrettyDurationOptions>
): string

Format milliseconds to time duration.

Interfaces

I

Options for format.

  • ignoreZero: boolean

    Whether to ignore zero values. With style="narrow" | "full", all zero values are ignored. With style="digital", only values in the ends are ignored.

  • style: "narrow" | "digital" | "full"

    "narrow" for "0d 0h 0m 0s 0ms..." "digital" for "00:00:00:00:000..." "full" for "0 days, 0 hours, 0 minutes,..."

printf

sprintf and printf for printing formatted strings to stdout.

Functions

f
printf(
format: string,
...args: unknown[]
): void

Converts and format a variable number of args as is specified by format. printf writes the formatted string to standard output.

f
sprintf(
format: string,
...args: unknown[]
): string

Converts and format a variable number of args as is specified by format. sprintf returns the formatted string.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.