( pattern: string )
| 6 | * @param pattern |
| 7 | */ |
| 8 | export function PartitionPattern<T extends string>( |
| 9 | pattern: string |
| 10 | ): Array<{type: T; value: string | undefined}> { |
| 11 | const result = [] |
| 12 | let beginIndex = pattern.indexOf('{') |
| 13 | let endIndex = 0 |
| 14 | let nextIndex = 0 |
| 15 | const length = pattern.length |
| 16 | while (beginIndex < pattern.length && beginIndex > -1) { |
| 17 | endIndex = pattern.indexOf('}', beginIndex) |
| 18 | invariant(endIndex > beginIndex, `Invalid pattern ${pattern}`) |
| 19 | if (beginIndex > nextIndex) { |
| 20 | result.push({ |
| 21 | type: 'literal' as T, |
| 22 | value: pattern.substring(nextIndex, beginIndex), |
| 23 | }) |
| 24 | } |
| 25 | result.push({ |
| 26 | type: pattern.substring(beginIndex + 1, endIndex) as T, |
| 27 | value: undefined, |
| 28 | }) |
| 29 | nextIndex = endIndex + 1 |
| 30 | beginIndex = pattern.indexOf('{', nextIndex) |
| 31 | } |
| 32 | if (nextIndex < length) { |
| 33 | result.push({ |
| 34 | type: 'literal' as T, |
| 35 | value: pattern.substring(nextIndex, length), |
| 36 | }) |
| 37 | } |
| 38 | return result |
| 39 | } |
no test coverage detected