(text: string)
| 28 | } |
| 29 | |
| 30 | export function parser(text: string): string[] { |
| 31 | const output: string[] = []; |
| 32 | let chunk = ''; |
| 33 | let previousToken = ''; |
| 34 | |
| 35 | // This is where message starts. |
| 36 | // "Hello {name}!" |
| 37 | // ^ |
| 38 | // It must return end index of the message and parsed data. |
| 39 | function messageStart(startIndex: number): { index: number, output: string; } { |
| 40 | let localSelector = ''; |
| 41 | let localPipes: [string, ...[string, string][]][] = []; |
| 42 | let localPipeIndex = -1; |
| 43 | |
| 44 | function end(index: number) { |
| 45 | const selectorTrim = localSelector.trim(); |
| 46 | const selector = selectorTrim |
| 47 | ? selectorTrim[0] === '[' |
| 48 | ? 'a' + selectorTrim |
| 49 | : 'a.' + selectorTrim |
| 50 | : ''; |
| 51 | const output = localPipes.reduce(reducePipe, selector); |
| 52 | |
| 53 | return { |
| 54 | index, |
| 55 | output, |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | for (let i = startIndex; i < text.length; i++) { |
| 60 | const char = text[i]; |
| 61 | const escaped = previousToken === tokenEscape; |
| 62 | previousToken = char; |
| 63 | |
| 64 | if (char === tokenEscape) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (tokenPipe === char) { |
| 69 | localPipeIndex += 1; |
| 70 | localPipes[localPipeIndex] = ['']; |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | // Handle message inside selector |
| 75 | if (tokenStart === char && localPipeIndex === -1 && !escaped) { |
| 76 | const messageData = messageStart(i + 1); |
| 77 | i = messageData.index; |
| 78 | |
| 79 | if (localSelector[localSelector.length - 1] === '.') { |
| 80 | localSelector = localSelector.slice(0, -1); |
| 81 | } |
| 82 | |
| 83 | localSelector += '[' + messageData.output + ']'; |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (tokenEnd === char) { |
no test coverage detected