@@ -3,6 +3,7 @@ const tokenEnd = '}';
3
3
const tokenPipe = '|' ;
4
4
const tokenPipeArguments = ',' ;
5
5
const tokenPipeArgumentsValue = ':' ;
6
+ const tokenEscape = '\\' ;
6
7
7
8
// "Hello {name} from the {planet | capitalize} {distance | number, unit:"lightyears"}"
8
9
// (a) => "Hello " + a.name + " from the " + capitalize(a.planet) + " " + number(a.distance, {unit:'lightyears'})
@@ -22,13 +23,14 @@ function reducePipe(acc: string, [pipeName, ...pipeProps]: [string, ...[string,
22
23
+ acc
23
24
+ ( ! pipeProps . length
24
25
? ''
25
- : ',{' + pipeProps . map ( mapPipeProps ) . join ( ) + '}' )
26
+ : ( acc . trim ( ) ? '' : 'void 0' ) + ',{' + pipeProps . map ( mapPipeProps ) . join ( ) + '}' )
26
27
+ ')' ;
27
28
}
28
29
29
30
export function parser ( text : string ) : string [ ] {
30
31
const output : string [ ] = [ ] ;
31
32
let chunk = '' ;
33
+ let previousToken = '' ;
32
34
33
35
// This is where message starts.
34
36
// "Hello {name}!"
@@ -40,7 +42,12 @@ export function parser(text: string): string[] {
40
42
let localPipeIndex = - 1 ;
41
43
42
44
function end ( index : number ) {
43
- const selector = localSelector ? 'a.' + localSelector : '' ;
45
+ const selectorTrim = localSelector . trim ( ) ;
46
+ const selector = selectorTrim
47
+ ? selectorTrim [ 0 ] === '['
48
+ ? 'a' + selectorTrim
49
+ : 'a.' + selectorTrim
50
+ : '' ;
44
51
const output = localPipes . reduce ( reducePipe , selector ) ;
45
52
46
53
return {
@@ -51,6 +58,12 @@ export function parser(text: string): string[] {
51
58
52
59
for ( let i = startIndex ; i < text . length ; i ++ ) {
53
60
const char = text [ i ] ;
61
+ const escaped = previousToken === tokenEscape ;
62
+ previousToken = char ;
63
+
64
+ if ( char === tokenEscape ) {
65
+ continue ;
66
+ }
54
67
55
68
if ( tokenPipe === char ) {
56
69
localPipeIndex += 1 ;
@@ -59,12 +72,14 @@ export function parser(text: string): string[] {
59
72
}
60
73
61
74
// Handle message inside selector
62
- if ( tokenStart === char && localPipeIndex === - 1 ) {
75
+ if ( tokenStart === char && localPipeIndex === - 1 && ! escaped ) {
63
76
const messageData = messageStart ( i + 1 ) ;
64
77
i = messageData . index ;
78
+
65
79
if ( localSelector [ localSelector . length - 1 ] === '.' ) {
66
80
localSelector = localSelector . slice ( 0 , - 1 ) ;
67
81
}
82
+
68
83
localSelector += '[' + messageData . output + ']' ;
69
84
continue ;
70
85
}
@@ -89,7 +104,7 @@ export function parser(text: string): string[] {
89
104
}
90
105
91
106
// Handle message inside arguments
92
- if ( tokenStart === char ) {
107
+ if ( tokenStart === char && ! escaped ) {
93
108
const messageData = messageStart ( i + 1 ) ;
94
109
i = messageData . index ;
95
110
args [ args . length - 1 ] += messageData . output ;
@@ -101,7 +116,7 @@ export function parser(text: string): string[] {
101
116
}
102
117
103
118
// Message starts is outside allowed zones
104
- if ( tokenStart === char ) {
119
+ if ( tokenStart === char && ! escaped ) {
105
120
fail ( char , i ) ;
106
121
}
107
122
@@ -122,8 +137,14 @@ export function parser(text: string): string[] {
122
137
123
138
for ( let i = 0 ; i < text . length ; i ++ ) {
124
139
const char = text [ i ] ;
140
+ const escaped = previousToken === tokenEscape ;
141
+ previousToken = char ;
142
+
143
+ if ( char === tokenEscape ) {
144
+ continue ;
145
+ }
125
146
126
- if ( tokenStart === char ) {
147
+ if ( tokenStart === char && ! escaped ) {
127
148
output . push ( JSON . stringify ( chunk ) ) ;
128
149
chunk = '' ;
129
150
const messageData = messageStart ( i + 1 ) ;
0 commit comments