@@ -26,27 +26,72 @@ import {
26
26
IOptions ,
27
27
} from "./options" ;
28
28
29
+ export function newEscapedCharParser ( options : IOptions ) : Parser < Ast < any > > {
30
+ return pick ( 1 , string ( options . escapeChar ) , regex ( / ^ ./ ) ) ;
31
+ }
32
+
33
+ export function newWildcardParser ( options : IOptions ) : Parser < Ast < any > > {
34
+ return newAst ( "wildcard" , string ( options . wildcardChar ) ) ;
35
+ }
36
+
29
37
/*
30
- *
38
+ * parses just the segment name in a named segment
31
39
*/
32
- export function newUrlPatternParser ( options : IOptions ) : Parser < Ast < any > > {
33
- const parseEscapedChar = pick ( 1 , string ( options . escapeChar ) , regex ( / ^ ./ ) ) ;
34
-
35
- const parseSegmentName = regex ( new RegExp ( `^[${ options . segmentNameCharset } ]+` ) ) ;
40
+ export function newSegmentNameParser ( options : IOptions ) : Parser < string > {
41
+ return regex ( new RegExp ( `^[${ options . segmentNameCharset } ]+` ) ) ;
42
+ }
36
43
37
- let parseNamedSegment = newAst ( "namedSegment" , pick ( 1 ,
38
- string ( options . segmentNameStartChar ) ,
39
- parseSegmentName ) ) ;
40
- if ( options . segmentNameEndChar != null ) {
41
- parseNamedSegment = newAst ( "namedSegment" , pick ( 1 ,
44
+ export function newNamedSegmentParser ( options : IOptions ) : Parser < Ast < any > > {
45
+ const parseSegmentName = newSegmentNameParser ( options ) ;
46
+ if ( options . segmentNameEndChar == null ) {
47
+ return newAst ( "namedSegment" , pick ( 1 ,
48
+ string ( options . segmentNameStartChar ) ,
49
+ parseSegmentName ) ) ;
50
+ } else {
51
+ return newAst ( "namedSegment" , pick ( 1 ,
42
52
string ( options . segmentNameStartChar ) ,
43
53
parseSegmentName ,
44
54
string ( options . segmentNameEndChar ) ) ) ;
45
55
}
56
+ }
57
+
58
+ export function newNamedWildcardParser ( options : IOptions ) : Parser < Ast < any > > {
59
+ if ( options . segmentNameEndChar == null ) {
60
+ return newAst ( "namedWildcard" , pick ( 2 ,
61
+ string ( options . wildcardChar ) ,
62
+ string ( options . segmentNameStartChar ) ,
63
+ newSegmentNameParser ( options ) ,
64
+ ) ) ;
65
+ } else {
66
+ return newAst ( "namedWildcard" , pick ( 2 ,
67
+ string ( options . wildcardChar ) ,
68
+ string ( options . segmentNameStartChar ) ,
69
+ newSegmentNameParser ( options ) ,
70
+ string ( options . segmentNameEndChar ) ,
71
+ ) ) ;
72
+ }
73
+ }
46
74
47
- const parseWildcard = newAst ( "wildcard" , string ( options . wildcardChar ) ) ;
75
+ export function newStaticContentParser ( options : IOptions ) : Parser < Ast < any > > {
76
+ return newAst ( "staticContent" , concatMany1Till ( firstChoice (
77
+ newEscapedCharParser ( options ) ,
78
+ regex ( / ^ ./ ) ) ,
79
+ // parse any normal or escaped char until the following matches:
80
+ firstChoice (
81
+ string ( options . segmentNameStartChar ) ,
82
+ string ( options . optionalSegmentStartChar ) ,
83
+ string ( options . optionalSegmentEndChar ) ,
84
+ newWildcardParser ( options ) ,
85
+ newNamedWildcardParser ( options ) ,
86
+ ) ,
87
+ ) ) ;
88
+ }
48
89
49
- let pattern : Parser < any > = ( input : string ) => {
90
+ /*
91
+ *
92
+ */
93
+ export function newUrlPatternParser ( options : IOptions ) : Parser < Ast < any > > {
94
+ let parsePattern : Parser < any > = ( input : string ) => {
50
95
throw new Error ( `
51
96
this is just a temporary placeholder
52
97
to make a circular dependency work.
@@ -56,27 +101,20 @@ export function newUrlPatternParser(options: IOptions): Parser<Ast<any>> {
56
101
57
102
const parseOptionalSegment = newAst ( "optionalSegment" , pick ( 1 ,
58
103
string ( options . optionalSegmentStartChar ) ,
59
- lazy ( ( ) => pattern ) ,
104
+ lazy ( ( ) => parsePattern ) ,
60
105
string ( options . optionalSegmentEndChar ) ) ) ;
61
106
62
- const parseStatic = newAst ( "static" , concatMany1Till ( firstChoice (
63
- parseEscapedChar ,
64
- regex ( / ^ ./ ) ) ,
65
- firstChoice (
66
- string ( options . segmentNameStartChar ) ,
67
- string ( options . optionalSegmentStartChar ) ,
68
- string ( options . optionalSegmentEndChar ) ,
69
- lazy ( ( ) => parseWildcard ) ) ) ) ;
70
-
71
- const token = firstChoice (
72
- parseWildcard ,
107
+ const parseToken = firstChoice (
108
+ newNamedWildcardParser ( options ) ,
109
+ newWildcardParser ( options ) ,
73
110
parseOptionalSegment ,
74
- parseNamedSegment ,
75
- parseStatic ) ;
111
+ newNamedSegmentParser ( options ) ,
112
+ newStaticContentParser ( options ) ,
113
+ ) ;
76
114
77
- pattern = many1 ( token ) ;
115
+ parsePattern = many1 ( parseToken ) ;
78
116
79
- return pattern ;
117
+ return parsePattern ;
80
118
}
81
119
82
120
// functions that further process ASTs returned as `.value` in parser results
@@ -91,7 +129,7 @@ function baseAstNodeToRegexString(astNode: Ast<any>, segmentValueCharset: string
91
129
return "(.*?)" ;
92
130
case "namedSegment" :
93
131
return `([${ segmentValueCharset } ]+)` ;
94
- case "static " :
132
+ case "staticContent " :
95
133
return escapeStringForRegex ( astNode . value ) ;
96
134
case "optionalSegment" :
97
135
return `(?:${ baseAstNodeToRegexString ( astNode . value , segmentValueCharset ) } )?` ;
@@ -117,7 +155,7 @@ export function astNodeToNames(astNode: Ast<any> | Array<Ast<any>>): string[] {
117
155
return [ "_" ] ;
118
156
case "namedSegment" :
119
157
return [ astNode . value ] ;
120
- case "static " :
158
+ case "staticContent " :
121
159
return [ ] ;
122
160
case "optionalSegment" :
123
161
return astNodeToNames ( astNode . value ) ;
@@ -184,7 +222,7 @@ function astNodeContainsSegmentsForProvidedParams(
184
222
return getParam ( params , "_" , nextIndexes , false ) != null ;
185
223
case "namedSegment" :
186
224
return getParam ( params , astNode . value , nextIndexes , false ) != null ;
187
- case "static " :
225
+ case "staticContent " :
188
226
return false ;
189
227
case "optionalSegment" :
190
228
return astNodeContainsSegmentsForProvidedParams ( astNode . value , params , nextIndexes ) ;
@@ -199,7 +237,7 @@ function astNodeContainsSegmentsForProvidedParams(
199
237
export function stringify (
200
238
astNode : Ast < any > | Array < Ast < any > > ,
201
239
params : { [ index : string ] : any } ,
202
- nextIndexes : { [ index : string ] : number } ,
240
+ nextIndexes : { [ index : string ] : number } = { } ,
203
241
) : string {
204
242
if ( Array . isArray ( astNode ) ) {
205
243
return stringConcatMap ( astNode , ( node ) => stringify ( node , params , nextIndexes ) ) ;
@@ -210,7 +248,7 @@ export function stringify(
210
248
return getParam ( params , "_" , nextIndexes , true ) ;
211
249
case "namedSegment" :
212
250
return getParam ( params , astNode . value , nextIndexes , true ) ;
213
- case "static " :
251
+ case "staticContent " :
214
252
return astNode . value ;
215
253
case "optionalSegment" :
216
254
if ( astNodeContainsSegmentsForProvidedParams ( astNode . value , params , nextIndexes ) ) {
0 commit comments