* Prepare and sort base cases for matching. * Order: exact matches first, then patterns with fewer variables (more specific first).
( base: Map<number | string, Expression> )
| 168 | pattern: ParsedPattern; |
| 169 | value: Expression; |
| 170 | /** Number of variables in pattern (more specific = fewer variables) */ |
| 171 | variableCount: number; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Prepare and sort base cases for matching. |
| 176 | * Order: exact matches first, then patterns with fewer variables (more specific first). |
| 177 | */ |
| 178 | function prepareBaseCases( |
| 179 | base: Map<number | string, Expression> |
| 180 | ): PreparedBaseCase[] { |
| 181 | const cases: PreparedBaseCase[] = []; |
| 182 | |
| 183 | for (const [key, value] of base) { |
| 184 | const pattern = parseBasePattern(key); |
| 185 | const variableCount = pattern.values.filter( |
| 186 | (v) => typeof v === 'string' |
| 187 | ).length; |
| 188 | cases.push({ pattern, value, variableCount }); |
| 189 | } |
| 190 | |
| 191 | // Sort: exact matches first, then by ascending variable count |
| 192 | cases.sort((a, b) => { |
| 193 | if (a.pattern.type !== b.pattern.type) { |
| 194 | return a.pattern.type === 'exact' ? -1 : 1; |
| 195 | } |
no test coverage detected