Skip to content

Commit 2092bd1

Browse files
committed
feature: braces: add maxSymbols (#36 (comment))
1 parent 9f5b4cf commit 2092bd1

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ console.log(braces.expand('a{b}c'));
186186
console.log(braces('a/{b,c}/d', { maxLength: 3 })); //=> throws an error
187187
```
188188

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+
189201
### options.expand
190202

191203
**Type**: `Boolean`

lib/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = {
44
MAX_LENGTH: 1024 * 64,
5+
MAX_SYMBOLS: 1024,
56

67
// Digits
78
CHAR_0: '0', /* 0 */

lib/parse.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use strict';
22

33
const stringify = require('./stringify');
4+
const {isCorrectBraces, validateInput} = require('./validate-input');
45

56
/**
67
* Constants
78
*/
89

910
const {
1011
MAX_LENGTH,
12+
MAX_SYMBOLS,
1113
CHAR_BACKSLASH, /* \ */
1214
CHAR_BACKTICK, /* ` */
1315
CHAR_COMMA, /* , */
@@ -34,6 +36,11 @@ const parse = (input, options = {}) => {
3436
}
3537

3638
let opts = options || {};
39+
40+
validateInput(input, {
41+
maxSymbols: opts.maxSymbols || MAX_SYMBOLS,
42+
});
43+
3744
let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
3845
if (input.length > max) {
3946
throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
@@ -316,15 +323,16 @@ module.exports = parse;
316323
function markImbalancedBraces({nodes}) {
317324
// Mark imbalanced braces and brackets as invalid
318325
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';
321330

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+
}
325333

326-
node.invalid = true;
327334
delete node.parent;
335+
delete node.prev;
328336
}
329337
}
330338

lib/validate-input.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};

test/braces.parse.js

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ describe('braces.parse()', () => {
1010
let MAX_LENGTH = 1024 * 64;
1111
assert.throws(() => parse('.'.repeat(MAX_LENGTH + 2)));
1212
});
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+
});
1323
});
1424

1525
describe('valid', () => {

0 commit comments

Comments
 (0)