humanize

Types

Utilities for formatting numbers, bytes, durations, lists, and timestamps in a human-friendly and locale-aware format. Decimal separator selection for formatted numbers.

pub type DecimalSeparator {
  Dot
  Comma
}

Constructors

  • Dot
  • Comma

Localized strings and unit definitions for relative time formatting.

pub type LocaleData {
  LocaleData(
    ago: String,
    in_: String,
    now: String,
    conj: String,
    units: List(#(Int, String, String)),
  )
}

Constructors

  • LocaleData(
      ago: String,
      in_: String,
      now: String,
      conj: String,
      units: List(#(Int, String, String)),
    )

Options record for number formatting.

pub type NumberOpts {
  NumberOpts(decimals: Int, decimal_sep: DecimalSeparator)
}

Constructors

Values

pub fn bytes_binary(bytes: Int) -> String

Format bytes using binary units (base 1024), supports up to Yobibyte (YiB).

pub fn bytes_decimal(bytes: Int) -> String

Format bytes using decimal units (base 1000), supports up to Yottabyte (YB).

pub fn duration(seconds: Int) -> String

Format a duration in seconds into a single largest unit description.

pub fn duration_precise(seconds: Int) -> String

Precise duration with multiple units, joined by commas.

pub fn get_locale_data(locale: String) -> LocaleData

Retrieve the built-in LocaleData for a given locale code.

pub fn get_locale_data_with(
  locale: String,
  overrides: List(#(String, LocaleData)),
) -> LocaleData

Lookup LocaleData with custom overrides.

pub fn list(items: List(String)) -> String

Join a list of strings into a human-readable sentence fragment.

pub fn list_locale(
  items: List(String),
  conj: String,
  oxford: Bool,
) -> String

Join a list with a custom conjunction and Oxford comma option.

pub fn list_with_ampersand(items: List(String)) -> String

Join a list using an ampersand as the final conjunction.

pub fn list_with_oxford(items: List(String)) -> String

Join a list using the Oxford comma when appropriate.

pub fn make_locale(
  code: String,
  ago: String,
  in_: String,
  nowv: String,
  conj: String,
  units: List(#(Int, String, String)),
) -> #(String, LocaleData)

Create a custom LocaleData entry.

pub fn number(n: Int) -> String

Format an integer into a compact human-friendly string. Example: 1234 -> “1.2K”.

pub fn number_with(
  n: Int,
  decimals: Int,
  sep: DecimalSeparator,
) -> String

Format an integer using a specific number of decimals and decimal separator.

pub fn number_with_opts(n: Int, opts: NumberOpts) -> String

Format an integer using a NumberOpts record.

pub fn ordinal(n: Int) -> String

Return the English ordinal suffix for an integer (e.g., 1st, 2nd).

pub fn percent(n: Int) -> String

Format an integer as a percentage string. Uses no decimals by default.

pub fn percent_ratio(
  numerator: Int,
  denominator: Int,
  decimals: Int,
  sep: DecimalSeparator,
) -> String

Compute and format a ratio as a percentage with rounding.

pub fn percent_with(
  n: Int,
  decimals: Int,
  sep: DecimalSeparator,
  spaced: Bool,
) -> String

Format a percentage value with custom options.

pub fn time_ago_millis(
  past_millis: Int,
  now_millis: Int,
  locale: String,
  precise: Bool,
  max_units: Int,
) -> String

Convenience wrapper: accept timestamps in milliseconds instead of seconds.

pub fn time_ago_millis_with_overrides(
  past_millis: Int,
  now_millis: Int,
  locale: String,
  overrides: List(#(String, LocaleData)),
  precise: Bool,
  max_units: Int,
) -> String

Millisecond-based wrapper with overrides.

pub fn time_ago_unix(
  past_seconds: Int,
  now_seconds: Int,
  locale: String,
  precise: Bool,
  max_units: Int,
) -> String

Format a Unix timestamp relative to now_seconds using built-in locales.

pub fn time_ago_unix_with_overrides(
  past_seconds: Int,
  now_seconds: Int,
  locale: String,
  overrides: List(#(String, LocaleData)),
  precise: Bool,
  max_units: Int,
) -> String

Like time_ago_unix but accepts a list of runtime overrides.

Search Document