/* eslint-disable eslint-comments/no-unlimited-disable */ /* eslint-disable */ // @ts-nocheck import type { Quarter, Era, Day, Month } from '../../../types.ts'; import type { LocaleUnitValue, LocaleWidth, LocaleDayPeriod, MatchFn, MatchValueCallback } from '../../types.ts'; export interface BuildMatchFnArgs< Result extends LocaleUnitValue, DefaultMatchWidth extends LocaleWidth, DefaultParseWidth extends LocaleWidth, > { matchPatterns: BuildMatchFnMatchPatterns; defaultMatchWidth: DefaultMatchWidth; parsePatterns: BuildMatchFnParsePatterns; defaultParseWidth: DefaultParseWidth; valueCallback?: MatchValueCallback; } export type BuildMatchFnMatchPatterns = { [Width in LocaleWidth]?: RegExp; } & { [Width in DefaultWidth]: RegExp; }; export type BuildMatchFnParsePatterns = { [Width in LocaleWidth]?: ParsePattern; } & { [Width in DefaultWidth]: ParsePattern; }; export type ParsePattern = Value extends LocaleDayPeriod ? Record : Value extends Quarter ? readonly [RegExp, RegExp, RegExp, RegExp] : Value extends Era ? readonly [RegExp, RegExp] : Value extends Day ? readonly [RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp] : Value extends Month ? readonly [RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp, RegExp] : never; export function buildMatchFn< Value extends LocaleUnitValue, DefaultMatchWidth extends LocaleWidth, DefaultParseWidth extends LocaleWidth, >(args: BuildMatchFnArgs): MatchFn { return (string, options = {}) => { const width = options.width; const matchPattern = (width && args.matchPatterns[width]) || args.matchPatterns[args.defaultMatchWidth]; const matchResult = string.match(matchPattern); if (!matchResult) { return null; } const matchedString = matchResult[0]; const parsePatterns = (width && args.parsePatterns[width]) || args.parsePatterns[args.defaultParseWidth]; const key = ( Array.isArray(parsePatterns) ? findIndex(parsePatterns, (pattern) => pattern.test(matchedString)) : // [TODO] -- I challenge you to fix the type findKey(parsePatterns, (pattern: any) => pattern.test(matchedString)) ) as Value extends LocaleDayPeriod ? string : number; let value: Value; value = (args.valueCallback ? args.valueCallback(key) : key) as Value; value = options.valueCallback ? // [TODO] -- I challenge you to fix the type options.valueCallback(value as any) : value; const rest = string.slice(matchedString.length); return { value, rest }; }; } function findKey( object: Obj, predicate: (value: Value) => boolean, ): keyof Obj | undefined { for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key) && predicate(object[key])) { return key; } } return undefined; } function findIndex(array: Item[], predicate: (item: Item) => boolean): number | undefined { for (let key = 0; key < array.length; key++) { if (predicate(array[key])) { return key; } } return undefined; } /* eslint-enable */