| 64 | : never; |
| 65 | |
| 66 | export function buildMatchFn< |
| 67 | Value extends LocaleUnitValue, |
| 68 | DefaultMatchWidth extends LocaleWidth, |
| 69 | DefaultParseWidth extends LocaleWidth, |
| 70 | >( |
| 71 | args: BuildMatchFnArgs<Value, DefaultMatchWidth, DefaultParseWidth>, |
| 72 | ): MatchFn<Value> { |
| 73 | return (string, options = {}) => { |
| 74 | const width = options.width; |
| 75 | |
| 76 | const matchPattern = |
| 77 | (width && args.matchPatterns[width]) || |
| 78 | args.matchPatterns[args.defaultMatchWidth]; |
| 79 | const matchResult = string.match(matchPattern); |
| 80 | |
| 81 | if (!matchResult) { |
| 82 | return null; |
| 83 | } |
| 84 | const matchedString = matchResult[0]; |
| 85 | |
| 86 | const parsePatterns = |
| 87 | (width && args.parsePatterns[width]) || |
| 88 | args.parsePatterns[args.defaultParseWidth]; |
| 89 | |
| 90 | const key = ( |
| 91 | Array.isArray(parsePatterns) |
| 92 | ? findIndex(parsePatterns, (pattern) => pattern.test(matchedString)) |
| 93 | : class="cm">// [TODO] -- I challenge you to fix the type |
| 94 | findKey(parsePatterns, (pattern: any) => pattern.test(matchedString)) |
| 95 | ) as Value extends LocaleDayPeriod ? string : number; |
| 96 | |
| 97 | let value: Value; |
| 98 | |
| 99 | value = (args.valueCallback ? args.valueCallback(key) : key) as Value; |
| 100 | value = options.valueCallback |
| 101 | ? class="cm">// [TODO] -- I challenge you to fix the type |
| 102 | options.valueCallback(value as any) |
| 103 | : value; |
| 104 | |
| 105 | const rest = string.slice(matchedString.length); |
| 106 | |
| 107 | return { value, rest }; |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | function findKey<Value, Obj extends { [key in string | number]: Value }>( |
| 112 | object: Obj, |