@@ -62,10 +62,7 @@ export function format(id: string, parameters: Record<string, string | number> =
62
62
if ( / ^ \| + $ / . test ( id ) ) {
63
63
parts = id . split ( '|' ) ;
64
64
} else {
65
- const matches = id . match ( / (?: \| \| | [ ^ | ] ) + / g) ;
66
- if ( matches !== null ) {
67
- parts = matches ;
68
- }
65
+ parts = id . match ( / (?: \| \| | [ ^ | ] ) + / g) || [ ] ;
69
66
}
70
67
71
68
const intervalRegex = / ^ (?< interval > ( { \s * ( - ? \d + ( \. \d + ) ? [ \s * , \s * \- ? \d + ( . \d + ) ? ] * ) \s * } ) | (?< left_delimiter > [ [ \] ] ) \s * (?< left > - I n f | - ? \d + ( \. \d + ) ? ) \s * , \s * (?< right > \+ ? I n f | - ? \d + ( \. \d + ) ? ) \s * (?< right_delimiter > [ [ \] ] ) ) \s * (?< message > .* ?) $ / s;
@@ -74,31 +71,31 @@ export function format(id: string, parameters: Record<string, string | number> =
74
71
for ( let part of parts ) {
75
72
part = part . trim ( ) . replace ( / \| \| / g, '|' ) ;
76
73
77
- let matches = part . match ( intervalRegex ) ;
78
- if ( matches !== null ) {
74
+ const matches = part . match ( intervalRegex ) ;
75
+ if ( matches ) {
76
+ /**
77
+ * @type {interval: string, left_delimiter: string, left: string, right_delimiter: string, right: string, message: string }
78
+ */
79
+ const matchGroups : { [ p : string ] : string } = matches . groups || { } ;
79
80
if ( matches [ 2 ] ) {
80
81
for ( const n of matches [ 3 ] . split ( ',' ) ) {
81
82
if ( number === Number ( n ) ) {
82
- return strtr ( matches . groups ! [ ' message' ] , parameters ) ;
83
+ return strtr ( matchGroups . message , parameters ) ;
83
84
}
84
85
}
85
86
} else {
86
- const leftNumber = '-Inf' === matches . groups ! [ ' left' ] ? Number . NEGATIVE_INFINITY : Number ( matches . groups ! [ ' left' ] ) ;
87
- const rightNumber = [ 'Inf' , '+Inf' ] . includes ( matches . groups ! [ ' right' ] ) ? Number . POSITIVE_INFINITY : Number ( matches . groups ! [ ' right' ] ) ;
87
+ const leftNumber = '-Inf' === matchGroups . left ? Number . NEGATIVE_INFINITY : Number ( matchGroups . left ) ;
88
+ const rightNumber = [ 'Inf' , '+Inf' ] . includes ( matchGroups . right ) ? Number . POSITIVE_INFINITY : Number ( matchGroups . right ) ;
88
89
89
- if ( ( '[' === matches . groups ! [ ' left_delimiter' ] ? number >= leftNumber : number > leftNumber )
90
- && ( ']' === matches . groups ! [ ' right_delimiter' ] ? number <= rightNumber : number < rightNumber )
90
+ if ( ( '[' === matchGroups . left_delimiter ? number >= leftNumber : number > leftNumber )
91
+ && ( ']' === matchGroups . right_delimiter ? number <= rightNumber : number < rightNumber )
91
92
) {
92
- return strtr ( matches . groups ! [ ' message' ] , parameters ) ;
93
+ return strtr ( matchGroups . message , parameters ) ;
93
94
}
94
95
}
95
96
} else {
96
- matches = part . match ( / ^ \w + : \s * ( .* ?) $ / ) ;
97
- if ( matches !== null ) {
98
- standardRules . push ( matches [ 1 ] ) ;
99
- } else {
100
- standardRules . push ( part ) ;
101
- }
97
+ const ruleMatch = part . match ( / ^ \w + : \s * ( .* ?) $ / ) ;
98
+ standardRules . push ( ruleMatch ? ruleMatch [ 1 ] : part ) ;
102
99
}
103
100
}
104
101
0 commit comments