Skip to content

yummies/parser

Description

Parsers for user-entered numbers, percents, and strings with tolerant input and typed results. Use when normalizing form values, query params, or CSV-like text before validation schemas run, without duplicating regex and parseFloat edge cases in every feature module.

Usage

ts
import { parser } from "yummies/parser";

parser.NumberParserSettings

No description.

parser.number()

Parses a number from raw input and optionally clamps, rounds or limits fractional digits.

Strings are normalized by removing spaces and replacing , with . before parsing. Invalid inputs return the configured fallback.

Examples:

ts
number('1 234,5'); // 1234.5
ts
number('bad', { fallback: 0 }); // 0

parser.percent()

Converts a value into a percentage of maxValue and parses the result with the shared numeric parser.

Examples:

ts
percent(25, 200); // 12.5
ts
percent('bad', 100, { fallback: 0 }); // 0

parser.StringParserSettings

No description.

parser.string()

Converts arbitrary input into a string representation.

Objects are serialized with JSON.stringify, optionally pretty-printed, and nullish values resolve to the configured fallback.

Examples:

ts
string(123); // '123'
ts
string({ id: 1 }, { prettyJson: true });

Released under the MIT License.