File tree 5 files changed +49
-6
lines changed
5 files changed +49
-6
lines changed Original file line number Diff line number Diff line change @@ -186,6 +186,18 @@ console.log(braces.expand('a{b}c'));
186
186
console .log (braces (' a/{b,c}/d' , { maxLength: 3 })); // => throws an error
187
187
```
188
188
189
+ ### options.maxSymbols
190
+
191
+ ** Type** : ` Number `
192
+
193
+ ** Default** : ` 1024 `
194
+
195
+ ** Description** : Limit the count of unique symbols the input string.
196
+
197
+ ``` js
198
+ console .log (braces (' a/{b,c}/d' , { maxSymbols: 2 })); // => throws an error
199
+ ```
200
+
189
201
### options.expand
190
202
191
203
** Type** : ` Boolean `
Original file line number Diff line number Diff line change 2
2
3
3
module . exports = {
4
4
MAX_LENGTH : 1024 * 64 ,
5
+ MAX_SYMBOLS : 1024 ,
5
6
6
7
// Digits
7
8
CHAR_0 : '0' , /* 0 */
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const stringify = require ( './stringify' ) ;
4
+ const { isCorrectBraces, validateInput} = require ( './validate-input' ) ;
4
5
5
6
/**
6
7
* Constants
7
8
*/
8
9
9
10
const {
10
11
MAX_LENGTH ,
12
+ MAX_SYMBOLS ,
11
13
CHAR_BACKSLASH , /* \ */
12
14
CHAR_BACKTICK , /* ` */
13
15
CHAR_COMMA , /* , */
@@ -34,6 +36,11 @@ const parse = (input, options = {}) => {
34
36
}
35
37
36
38
let opts = options || { } ;
39
+
40
+ validateInput ( input , {
41
+ maxSymbols : opts . maxSymbols || MAX_SYMBOLS ,
42
+ } ) ;
43
+
37
44
let max = typeof opts . maxLength === 'number' ? Math . min ( MAX_LENGTH , opts . maxLength ) : MAX_LENGTH ;
38
45
if ( input . length > max ) {
39
46
throw new SyntaxError ( `Input length (${ input . length } ), exceeds max characters (${ max } )` ) ;
@@ -316,15 +323,16 @@ module.exports = parse;
316
323
function markImbalancedBraces ( { nodes} ) {
317
324
// Mark imbalanced braces and brackets as invalid
318
325
for ( const node of nodes ) {
319
- if ( node . nodes || node . invalid )
320
- continue ;
326
+ if ( ! node . nodes && ! node . invalid ) {
327
+ if ( node . type === 'open' ) node . isOpen = true ;
328
+ if ( node . type === 'close' ) node . isClose = true ;
329
+ if ( ! node . nodes ) node . type = 'text' ;
321
330
322
- if ( node . type === 'open' ) node . isOpen = true ;
323
- if ( node . type === 'close' ) node . isClose = true ;
324
- if ( ! node . nodes ) node . type = 'text' ;
331
+ node . invalid = true ;
332
+ }
325
333
326
- node . invalid = true ;
327
334
delete node . parent ;
335
+ delete node . prev ;
328
336
}
329
337
}
330
338
Original file line number Diff line number Diff line change
1
+ module . exports . validateInput = ( line , { maxSymbols} ) => {
2
+ const symbols = { } ;
3
+
4
+ for ( const current of line ) {
5
+ symbols [ current ] = ( symbols [ current ] || 0 ) + 1 ;
6
+ }
7
+
8
+ for ( const [ value , count ] of Object . entries ( symbols ) ) {
9
+ if ( count > maxSymbols )
10
+ throw SyntaxError ( `To many symbols '${ value } '. Maximum: ${ maxSymbols } allowed. Received: ${ count } ` ) ;
11
+ }
12
+ } ;
Original file line number Diff line number Diff line change @@ -10,6 +10,16 @@ describe('braces.parse()', () => {
10
10
let MAX_LENGTH = 1024 * 64 ;
11
11
assert . throws ( ( ) => parse ( '.' . repeat ( MAX_LENGTH + 2 ) ) ) ;
12
12
} ) ;
13
+ it ( 'should throw an error when symbols exceeds max symbols count default' , ( ) => {
14
+ let SYMBOLS = 1024 ;
15
+ assert . throws ( ( ) => parse ( '.' . repeat ( MAX_SYMBOLS * 2 ) ) ) ;
16
+ } ) ;
17
+ it ( 'should throw an error when symbols exceeds max symbols count ' , ( ) => {
18
+ let SYMBOLS = 2 ;
19
+ assert . throws ( ( ) => parse ( '...' , {
20
+ maxSymbols : 2 ,
21
+ } ) ) ;
22
+ } ) ;
13
23
} ) ;
14
24
15
25
describe ( 'valid' , ( ) => {
You can’t perform that action at this time.
0 commit comments