Skip to content

formatProgressBar

Formats a value as a text progress bar, repeating filledChar/emptyChar across width cells proportional to value / max.

value is clamped to [0, max] before computing the ratio — out-of-range values (negative, above max) produce an empty or fully-filled bar instead of throwing. Non-finite max (NaN, Infinity) is treated as 0, yielding an empty bar.

Available since v3.0.1

import { formatProgressBar } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { formatProgressBar } from 'helpers4/string';
formatProgressBar(value: number, options: ProgressBarOptions): string
ParameterTypeDescription
valuenumberThe current value
optionsProgressBarOptionsBar rendering options

string — A string of width repeated filled/empty characters

Renders a percentage as a filled/empty block bar using the default width and characters.

formatProgressBar(65)
// => '▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░'

Renders a value against a custom max, width, and fill characters — useful for anything scored out of a different total than 100.

formatProgressBar(3, { width: 10, max: 5, filledChar: '#', emptyChar: '-' })
// => '######----'

Options for formatProgressBar.

interface ProgressBarOptions {
  emptyChar?: string;
  filledChar?: string;
  max?: number;
  width?: number;
}

View source on GitHub