@@ -72,35 +72,32 @@ export abstract class AbstractTimeExpressionParser implements Parser {
72
72
extract ( context : ParsingContext , match : RegExpMatchArray ) : ParsingResult {
73
73
const startComponents = this . extractPrimaryTimeComponents ( context , match ) ;
74
74
if ( ! startComponents ) {
75
- match . index += match [ 0 ] . length ;
75
+ match . index += match [ 0 ] . length ; // Skip over potential overlapping pattern
76
76
return null ;
77
77
}
78
78
79
- const result = context . createParsingResult (
80
- match . index + match [ 1 ] . length ,
81
- match [ 0 ] . substring ( match [ 1 ] . length ) ,
82
- startComponents
83
- ) ;
79
+ const index = match . index + match [ 1 ] . length ;
80
+ const text = match [ 0 ] . substring ( match [ 1 ] . length ) ;
81
+ const result = context . createParsingResult ( index , text , startComponents ) ;
82
+ match . index += match [ 0 ] . length ; // Skip over potential overlapping pattern
84
83
85
- const remainingText = context . text . substring ( match . index + match [ 0 ] . length ) ;
84
+ const remainingText = context . text . substring ( match . index ) ;
86
85
const followingPattern = this . getFollowingTimePatternThroughCache ( ) ;
87
- match = followingPattern . exec ( remainingText ) ;
86
+ const followingMatch = followingPattern . exec ( remainingText ) ;
88
87
if (
89
- ! match ||
88
+ ! followingMatch ||
90
89
// Pattern "YY.YY -XXXX" is more like timezone offset
91
- match [ 0 ] . match ( / ^ \s * ( [ + - ] ) \s * \d { 3 , 4 } $ / )
90
+ followingMatch [ 0 ] . match ( / ^ \s * ( [ + - ] ) \s * \d { 3 , 4 } $ / )
92
91
) {
93
92
return this . checkAndReturnWithoutFollowingPattern ( result ) ;
94
93
}
95
94
96
- result . end = this . extractFollowingTimeComponents ( context , match , result ) ;
95
+ result . end = this . extractFollowingTimeComponents ( context , followingMatch , result ) ;
97
96
if ( result . end ) {
98
- if ( result . end ) {
99
- result . text += match [ 0 ] ;
100
- }
97
+ result . text += followingMatch [ 0 ] ;
101
98
}
102
99
103
- return result ;
100
+ return this . checkAndReturnWithFollowingPattern ( result ) ;
104
101
}
105
102
106
103
extractPrimaryTimeComponents (
@@ -334,6 +331,37 @@ export abstract class AbstractTimeExpressionParser implements Parser {
334
331
return result ;
335
332
}
336
333
334
+ private checkAndReturnWithFollowingPattern ( result ) {
335
+ if ( result . text . match ( / ^ \d + - \d + $ / ) ) {
336
+ return null ;
337
+ }
338
+
339
+ // If it ends only with numbers or dots
340
+ const endingWithNumbers = result . text . match ( / [ ^ \d : . ] ( \d [ \d . ] + ) \s * - \s * ( \d [ \d . ] + ) $ / ) ;
341
+ if ( endingWithNumbers ) {
342
+ // In strict mode (e.g. "at 1-3" or "at 1.2 - 2.3"), this should not be accepted
343
+ if ( this . strictMode ) {
344
+ return null ;
345
+ }
346
+
347
+ const startingNumbers : string = endingWithNumbers [ 1 ] ;
348
+ const endingNumbers : string = endingWithNumbers [ 2 ] ;
349
+ // If it ends only with dot single digit, e.g. "at 1.2"
350
+ if ( endingNumbers . includes ( "." ) && ! endingNumbers . match ( / \d ( \. \d { 2 } ) + $ / ) ) {
351
+ return null ;
352
+ }
353
+
354
+ // If it ends only with numbers above 24, e.g. "at 25"
355
+ const endingNumberVal = parseInt ( endingNumbers ) ;
356
+ const startingNumberVal = parseInt ( startingNumbers ) ;
357
+ if ( endingNumberVal > 24 || startingNumberVal > 24 ) {
358
+ return null ;
359
+ }
360
+ }
361
+
362
+ return result ;
363
+ }
364
+
337
365
private cachedPrimaryPrefix = null ;
338
366
private cachedPrimarySuffix = null ;
339
367
private cachedPrimaryTimePattern = null ;
0 commit comments