@neutrium/formatter - v1.0.1
    Preparing search index...

    Interface CompiledFormatter<Value, Spec, Rounded>

    Common operations on an immutable specification bound to a extensions!Formatter.

    This type describes the common rendering operations. Prefer an inferred compiled result (or typeof an existing result) to retain its exact range capabilities; unsupported methods are omitted automatically. All methods are bound and may be passed as callbacks. Obtain this object from extensions!Formatter.compile or extensions!Formatter.compileSeries; it has no parser methods. Use extensions-parse!Parser.compile on its spec to create a matching parser.

    interface CompiledFormatter<
        Value = unknown,
        Spec extends FormatSpecBase = FormatSpecBase,
        Rounded = unknown,
    > {
        resolution: SupportedResolvedFormat;
        spec: { kind: string; locale: string };
        format(value: Value): string;
        formatColumn(
            values: readonly Value[],
            options?: ColumnFormatOptions,
        ): readonly string[];
        formatDetailed(value: Value): DetailedFormatResult<Rounded>;
        formatSeries(
            values: readonly Value[],
            options?: SeriesFormatOptions,
        ): readonly string[];
        formatSeriesToParts(
            values: readonly Value[],
            options?: SeriesFormatOptions,
        ): readonly (readonly FormatToken[])[];
        formatToParts(value: Value): readonly FormatToken[];
    }

    Type Parameters

    • Value = unknown

      Value accepted by formatting operations.

    • Spec extends FormatSpecBase = FormatSpecBase

      Bound specification type.

    • Rounded = unknown

      Exact value reported as rounded metadata.

    Index
    resolution: SupportedResolvedFormat

    Cached successful resolution for spec.

    spec: { kind: string; locale: string }

    Deeply frozen bound specification including the effective locale and any selected series scale.

    Type Declaration

    • Readonlykind: string

      Literal discriminator used to select a registered codec.

    • Readonlylocale: string

      Explicit specification locale or the default captured during compilation.

    • Formats one value using the bound specification and locale.

      Parameters

      • value: Value

        Value accepted by the bound domain.

      Returns string

      Localized text.

      import { formatter } from "@neutrium/formatter";
      const money = formatter.compile({ kind: "currency", currency: "USD" });
      [12.5, 20].map(money.format); // ["$12.50", "$20.00"]
    • Formats and pads a column for monospaced text, measuring Unicode code points.

      Parameters

      • values: readonly Value[]

        Values in row order.

      • Optionaloptions: ColumnFormatOptions

        Alignment (decimal), fill (" "), and scale selection (shared).

      Returns readonly string[]

      Padded strings; use application-specific alignment for terminal cell widths.

      import { formatter } from "@neutrium/formatter";
      const number = formatter.compile({ kind: "number" });
      number.formatColumn([1, 20, 300], { align: "right" }); // [" 1", " 20", "300"]
    • Formats one value with tokens, resolution, and any available rounded value and scale.

      Parameters

      • value: Value

        Value to render and inspect.

      Returns DetailedFormatResult<Rounded>

      Detailed output; optional metadata depends on the domain and presentation.

      import { formatter } from "@neutrium/formatter";
      const compact = formatter.compile({ kind: "number", compactExponent: 6, maximumFractionDigits: 2 });
      const points = ["1234567", "2345678"].map(value => {
      const result = compact.formatDetailed(value);
      return {
      value,
      label: result.text,
      tooltip: result.roundedValue === undefined ? "" :
      `Label represents ${formatter.format(result.roundedValue, { kind: "number" })} visits`,
      };
      });
      // First point: { value: "1234567", label: "1.23M", tooltip: "Label represents 1,230,000 visits" }
    • Formats several values, sharing an automatic compact or byte scale by default. A fixed scale in spec, including one selected by compileSeries, takes precedence over individual scale selection.

      Parameters

      • values: readonly Value[]

        Values in output order.

      • Optionaloptions: SeriesFormatOptions

        Scale selection, defaulting to shared.

      Returns readonly string[]

      One string per input; no values returns an empty array.

      import { formatter } from "@neutrium/formatter";
      const size = formatter.compile({ kind: "bytes" });
      size.formatSeries([512, 1536]); // ["0.5 KiB", "1.5 KiB"]
      size.formatSeries([512, 1536], { scale: "individual" }); // ["512 B", "1.5 KiB"]
    • Formats several values into one semantic token array per value.

      Parameters

      Returns readonly (readonly FormatToken[])[]

      Ordered token arrays for each input row.

      import { formatter } from "@neutrium/formatter";
      const size = formatter.compile({ kind: "bytes" });
      const cells = size.formatSeriesToParts([1024, 1536]).map(parts => {
      const cell = document.createElement("td");
      for (const part of parts) {
      const span = document.createElement(part.type === "unit" ? "small" : "span");
      span.textContent = part.value;
      cell.append(span);
      }
      return cell;
      });
      // Append each cell to its file's table row: 1 KiB and 1.5 KiB, with smaller units.
    • Formats one value into semantic tokens, preserving localized literals.

      Parameters

      • value: Value

        Value accepted by the bound domain.

      Returns readonly FormatToken[]

      Tokens that render to the same text as format.

      import { formatter } from "@neutrium/formatter";
      const money = formatter.compile({ kind: "currency", currency: "USD" });
      const price = document.createElement("span");
      for (const part of money.formatToParts(12.5)) {
      const span = document.createElement(part.type === "currency" ? "small" : "span");
      span.textContent = part.value;
      price.append(span);
      }
      document.body.append(price); // $12.50 with a smaller $.