Skip to content

Commit

Permalink
feature: braces: add maxSymbols (#36 (comment))
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 15, 2024
1 parent 9f5b4cf commit 2092bd1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ console.log(braces.expand('a{b}c'));
console.log(braces('a/{b,c}/d', { maxLength: 3 })); //=> throws an error
```

### options.maxSymbols

**Type**: `Number`

**Default**: `1024`

**Description**: Limit the count of unique symbols the input string.

```js
console.log(braces('a/{b,c}/d', { maxSymbols: 2 })); //=> throws an error
```

### options.expand

**Type**: `Boolean`
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
MAX_LENGTH: 1024 * 64,
MAX_SYMBOLS: 1024,

// Digits
CHAR_0: '0', /* 0 */
Expand Down
20 changes: 14 additions & 6 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

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

/**
* Constants
*/

const {
MAX_LENGTH,
MAX_SYMBOLS,
CHAR_BACKSLASH, /* \ */
CHAR_BACKTICK, /* ` */
CHAR_COMMA, /* , */
Expand All @@ -34,6 +36,11 @@ const parse = (input, options = {}) => {
}

let opts = options || {};

validateInput(input, {
maxSymbols: opts.maxSymbols || MAX_SYMBOLS,
});

let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
if (input.length > max) {
throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
Expand Down Expand Up @@ -316,15 +323,16 @@ module.exports = parse;
function markImbalancedBraces({nodes}) {
// Mark imbalanced braces and brackets as invalid
for (const node of nodes) {
if (node.nodes || node.invalid)
continue;
if (!node.nodes && !node.invalid) {
if (node.type === 'open') node.isOpen = true;
if (node.type === 'close') node.isClose = true;
if (!node.nodes) node.type = 'text';

if (node.type === 'open') node.isOpen = true;
if (node.type === 'close') node.isClose = true;
if (!node.nodes) node.type = 'text';
node.invalid = true;
}

node.invalid = true;
delete node.parent;
delete node.prev;
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/validate-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports.validateInput = (line, {maxSymbols}) => {
const symbols = {};

for (const current of line) {
symbols[current] = (symbols[current] || 0) + 1;
}

for (const [value, count] of Object.entries(symbols)) {
if (count > maxSymbols)
throw SyntaxError(`To many symbols '${value}'. Maximum: ${maxSymbols} allowed. Received: ${count}`);
}
};
10 changes: 10 additions & 0 deletions test/braces.parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe('braces.parse()', () => {
let MAX_LENGTH = 1024 * 64;
assert.throws(() => parse('.'.repeat(MAX_LENGTH + 2)));
});
it('should throw an error when symbols exceeds max symbols count default', () => {
let SYMBOLS= 1024;
assert.throws(() => parse('.'.repeat(MAX_SYMBOLS * 2)));
});
it('should throw an error when symbols exceeds max symbols count ', () => {
let SYMBOLS= 2;
assert.throws(() => parse('...', {
maxSymbols: 2,
}));
});
});

describe('valid', () => {
Expand Down

0 comments on commit 2092bd1

Please sign in to comment.