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

    Interface FormatCodec<Kind, Value, Options, Rounded>

    Codec for one discriminated value domain.

    format is the only required operation. Range formatting, series-specific scaling, runtime resolution, and a direct string fast path are opt-in capabilities. Define codec objects with satisfies FormatCodec<...> so TypeScript checks the implementation without erasing which optional methods are actually present.

    import { createFormatter } from "@neutrium/formatter";
    import type { FormatCodec, FormatSpecBase } from "@neutrium/formatter/extensions";

    interface Point { x: number; y: number }
    interface PointSpec extends FormatSpecBase {
    kind: "point";
    separator?: string;
    }

    const pointCodec = {
    kind: "point",
    format(point, spec) {
    return [{
    type: "literal",
    value: `${point.x}${spec.separator ?? ","}${point.y}`,
    }];
    },
    } satisfies FormatCodec<"point", Point, PointSpec, Point>;

    const custom = createFormatter({ codecs: [pointCodec] });
    custom.format({ x: 10, y: 20 }, { kind: "point", separator: ":" });
    // "10:20"
    interface FormatCodec<
        Kind extends string = string,
        Value = unknown,
        Options extends FormatSpecBase = FormatSpecBase,
        Rounded = Value,
    > {
        kind: Kind;
        format(
            value: Value,
            options: Options,
            context: FormatContext,
        ): readonly FormatToken[];
        formatDetailed?(
            value: Value,
            options: Options,
            context: FormatContext,
        ): FormatValueMetadata<Rounded> & { parts: readonly FormatToken[] };
        formatRange?(
            start: Value,
            end: Value,
            options: Options,
            context: FormatContext,
        ): readonly RangeFormatToken[];
        formatSeries?(
            values: readonly Value[],
            options: Options,
            context: FormatContext,
            seriesOptions: SeriesFormatOptions,
        ): readonly (readonly FormatToken[])[];
        formatString?(
            value: Value,
            options: Options,
            context: FormatContext,
        ): string;
        resolve?(options: Options, context: FormatContext): CodecFormatResolution;
        selectSeriesSpec?(
            values: readonly Value[],
            options: Options,
            context: FormatContext,
        ): Options;
    }

    Type Parameters

    • Kind extends string = string

      Literal value of Options["kind"].

    • Value = unknown

      Value accepted by formatting operations.

    • Options extends FormatSpecBase = FormatSpecBase

      Specification accepted by the codec.

    • Rounded = Value

      Value reported as rounded metadata by formatDetailed.

    Index
    kind: Kind

    Unique literal discriminator handled by this codec.

    • Selects reusable series options for Formatter.compileSeries without rendering. The returned specification must retain the same kind and encode the selected scale for scalar, collection, and parsing operations. Input options are frozen; return a new record when changing them. Without this hook, options are unchanged.

      Parameters

      Returns Options