tryParse
API index · Input types and conventions · Installation
tryParse(text, pattern, reference, options?) → TryResult<reference kind, RangeError>
Use the same parsing rules as parse, with a readonly error-first tuple: [null, result] on success or [error, null] for invalid input. Checking error === null narrows the result's type.
Invalid patterns, unsupported fields, invalid options, missing zones, incompatible references, and non-string input still throw. The tuple only captures input parsing failures.
import { tryParse } from '@frantss/temporal-fns';
const reference = Temporal.PlainDate.from('2000-01-01');
const [error, result] = tryParse('2024-02-29', 'yyyy-MM-dd', reference);
if (error === null) {
result.toString(); // => '2024-02-29'
}
const [invalid, absent] = tryParse('2025-02-29', 'yyyy-MM-dd', reference);
invalid instanceof RangeError; // => true
absent; // => null
tryParse('2026', 'YYYY', reference); // throws RangeErrorRelated: parse, isMatch, tryParseDuration.
Behaviors: PAR-003: tryParse returns an error-first success tuple; PAR-004: Impossible or incomplete input has the same failure across parser variants; PAR-009: Configuration failures still throw from all parser variants; PAR-027: Non-text primary arguments are not ordinary parse failures.