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

    Class Parser<Codecs>

    Parses localized text using the codecs installed on this instance.

    Most applications should use parse!parser or parse!createParser, which include every built-in parser. Direct construction creates an empty registry unless codecs are supplied. Locale and application context are independent of any formatter instance; compiled specifications carry their bound locale.

    import { Parser, numberParser } from "@neutrium/formatter/extensions/parse";
    const numbers = new Parser({ codecs: [numberParser], locale: "de-DE" });
    numbers.parse("1.234,5", { kind: "number" }); // "1234.5"

    Type Parameters

    • const Codecs extends readonly AnyParseCodec[] = readonly []

      Installed parser tuple, used to infer specifications and results.

    Index
    • Creates a parser with exactly the supplied codecs, or an empty registry.

      Type Parameters

      • const Codecs extends readonly AnyParseCodec[] = readonly []

        Installed parser tuple, used to infer specifications and results.

      Parameters

      Returns Parser<Codecs>

      index!DuplicateFormatError for duplicate codec kinds.

      TypeError for an invalid codec kind or method shape.

      RangeError for an invalid locale identifier.

      import { Parser, bytesParser } from "@neutrium/formatter/extensions/parse";
      const sizes = new Parser({ codecs: [bytesParser] });
      sizes.parse("1.5 KiB", { kind: "bytes" }); // "1536"
    • Validates and freezes a specification for repeated parsing.

      Later edits to the original specification have no effect. The returned parse method is bound and safe to pass as a callback. Specifications may contain primitives, plain records, and arrays; class instances, functions, and accessors are rejected. A successful compile always provides parse.

      Type Parameters

      • const Spec extends never

      Parameters

      • spec: Spec & FormatSpecBase & Record<
            Exclude<
                keyof Spec,
                | (keyof FormatSpecBase)
                | {
                    toLocaleString(): string;
                    toLocaleString(
                        locales?: string | string[],
                        options?: NumberFormatOptions,
                    ): string;
                    toLocaleString(
                        locales?: LocalesArgument,
                        options?: NumberFormatOptions,
                    ): string;
                    toString(): string;
                    toString(radix?: number): string;
                    valueOf(): string | number | symbol;
                },
            >,
            never,
        >

        Specification to copy and resolve with this parser's defaults.

      Returns CompiledParser<ParsedValue<Codecs, Spec>, Spec>

      A frozen parser with spec, successful resolution, and bound parse.

      For invalid or unsupported specifications, preserving the original error.

      import { formatter } from "@neutrium/formatter";
      import { parser } from "@neutrium/formatter/parse";

      const thousands = formatter.compileSeries([900, 1200], {
      kind: "number", notation: "compact", maximumFractionDigits: 1,
      });
      const inputs = parser.compile(thousands.spec);
      ["0.9K", "1.2K"].map(inputs.parse); // ["900", "1200"]
    • Parses one complete localized string into the selected codec's result type.

      Built-in numeric and elapsed-duration parsers return canonical decimal strings, preserving exact digits while removing presentation padding. Parsing recovers displayed precision: "1.23M" represents "1230000". Grouping, affixes, signs, and scales must match the specification. Surrounding whitespace is tolerated after an exact presentation match is attempted.

      Type Parameters

      • const Spec extends never

      Parameters

      • input: string

        Complete localized text, not a string containing an embedded number.

      • spec: Spec & FormatSpecBase & Record<
            Exclude<
                keyof Spec,
                | (keyof FormatSpecBase)
                | {
                    toLocaleString(): string;
                    toLocaleString(
                        locales?: string | string[],
                        options?: NumberFormatOptions,
                    ): string;
                    toLocaleString(
                        locales?: LocalesArgument,
                        options?: NumberFormatOptions,
                    ): string;
                    toString(): string;
                    toString(radix?: number): string;
                    valueOf(): string | number | symbol;
                },
            >,
            never,
        >

        The presentation and locale used to produce or accept the text.

      Returns ParsedValue<Codecs, Spec>

      A canonical string for built-ins, or the custom parser's result type.

      index!UnknownFormatError if no parser is installed for the kind.

      parse!UnsupportedParseError if a presentation is not parseable.

      TypeError or RangeError for invalid input or options.

      import { parser } from "@neutrium/formatter/parse";

      parser.parse("1.234,50", { kind: "number", locale: "de-DE" }); // "1234.5"
      parser.parse("12.5%", { kind: "percentage", maximumFractionDigits: 1 }); // "0.125"
      try {
      parser.parse("12,34.5", { kind: "number" }); // invalid en-US grouping
      } catch (error) {
      console.error(error instanceof Error ? error.message : String(error));
      }
    • Inspects parsing support, returning a serializable error for a rejected specification. Check supported before reading success-only fields. Rendering capabilities are always false; use a formatter's resolve to inspect formatting separately.

      Parameters

      Returns ResolvedFormat

      A successful resolution or the reason parsing is unavailable.

      import { parser } from "@neutrium/formatter/parse";

      const spec = { kind: "duration", presentation: "localized" } as const;
      const result = parser.resolve(spec);
      if (!result.supported) {
      console.error(`This display cannot be used as an editable duration: ${result.error.message}`);
      }
    • Checks whether a complete specification can be parsed on this runtime. This checks options and available codecs, not the validity of an input string. Use resolve when the failure reason is needed.

      Parameters

      • spec: FormatSpecBase

        Specification to inspect, including required domain options.

      Returns boolean

      false for unknown kinds, invalid options, or unsupported presentations.

      import { parser } from "@neutrium/formatter/parse";
      const elapsed = { kind: "duration", presentation: "elapsed" } as const;
      const localized = { kind: "duration", presentation: "localized" } as const;
      parser.supports(elapsed); // true
      parser.supports(localized); // false
    • Returns an independent parser with one additional codec, preserving locale and context.

      Type Parameters

      • const Codec extends AnyParseCodec

      Parameters

      • codec: Codec

        Parser for a kind not already installed on this instance.

      Returns Parser<readonly [Codecs, Codec]>

      A new parser whose inferred types include the added codec.

      index!DuplicateFormatError if the kind is already registered.

      TypeError for an invalid codec kind or method shape.

      import { Parser, numberParser } from "@neutrium/formatter/extensions/parse";
      const empty = new Parser();
      const numbers = empty.withCodec(numberParser);
      numbers.parse("1,234", { kind: "number" }); // "1234"
      empty.supports({ kind: "number" }); // false