Macaw.Format.Measure (macaw_format v0.1.0)

Copy Markdown View Source

A field's default format (spec/01 §6): how a measure's values are written with no format given, built by Macaw.Format.for_measure/2 and written by Macaw.Format.measure/2.

It is data: the resolved measure, the way its values are written (kind), the d3 formats it writes their numbers with, each a Macaw.Format, and what its unit resolves to (units): each conversion it writes values by, resolved once with Macaw.Units.Unit.conversion/2, and each suffix it writes, so that writing a value applies a conversion with Macaw.Units.Unit.convert/2 and reads no unit code (macaw_units spec/01 §5, macaw_data#459).

iex> {:ok, bytes} = Macaw.Format.for_measure(%{unit: "By"})
iex> {bytes.kind, Macaw.Format.measure(bytes, 1_500_000)}
{:quantity, "1.50 MB"}

Summary

Types

A conversion Macaw.Units.Unit.conversion/2 resolved, or nil for a value written as it is: the unit is the one written, or none, or the pair does not convert.

A unit a value is written in: the conversion into it and its suffix.

How a measure's values are written (spec/01 §6's table).

A number's notation with no measure; :fixed is a precision given alone.

t()

What the measure's unit resolves to, once, for its kind: :suffix the unit's own suffix; :seconds a duration's conversion to s; :percent and :one a ratio's to % and to 1; :display a quantity's units, {:one, display} for a unit written in one unit whatever the magnitude, else {:ladder, members, zero}, its family's members smallest first and the one zero is written in.

Types

conversion()

@type conversion() :: {Macaw.Units.Unit.exact(), {integer(), pos_integer()}} | nil

A conversion Macaw.Units.Unit.conversion/2 resolved, or nil for a value written as it is: the unit is the one written, or none, or the pair does not convert.

display()

@type display() :: {conversion(), String.t()}

A unit a value is written in: the conversion into it and its suffix.

kind()

@type kind() :: :duration | :currency | :ratio | :count | :quantity | :unit | :number

How a measure's values are written (spec/01 §6's table).

notation()

@type notation() :: :auto | :plain | :grouped | :si | {:si, 3..24} | :fixed

A number's notation with no measure; :fixed is a precision given alone.

t()

@type t() :: %Macaw.Format.Measure{
  currency:
    %{code: String.t(), symbol: String.t() | nil, amount: Macaw.Format.t()}
    | nil,
  fixed: Macaw.Format.t(),
  format: Macaw.Format.t() | nil,
  kind: kind(),
  locale: Macaw.Format.Locale.t(),
  measure: %{
    unit: Macaw.Units.Unit.code() | nil,
    quantity: Macaw.Units.Quantity.t() | nil
  },
  notation: notation(),
  precision: 0..20,
  style: :compound | :magnitude,
  system: :metric | :us | :uk | :as_posted,
  units: units()
}

units()

@type units() :: %{
  optional(:suffix) => String.t(),
  optional(:seconds) => conversion(),
  optional(:percent) => conversion(),
  optional(:one) => conversion(),
  optional(:display) =>
    {:one, display()} | {:ladder, [display(), ...], display()}
}

What the measure's unit resolves to, once, for its kind: :suffix the unit's own suffix; :seconds a duration's conversion to s; :percent and :one a ratio's to % and to 1; :display a quantity's units, {:one, display} for a unit written in one unit whatever the magnitude, else {:ladder, members, zero}, its family's members smallest first and the one zero is written in.