diff --git a/.verb.md b/.verb.md index cdedd55..2ee6767 100644 --- a/.verb.md +++ b/.verb.md @@ -167,7 +167,7 @@ console.log(braces.expand('a{b}c')); **Type**: `Number` -**Default**: `65,536` +**Default**: `10,000` **Description**: Limit the length of the input string. Useful when the input string is generated or your application allows users to pass a string, et cetera. diff --git a/README.md b/README.md index 9d9f9bc..f59dd60 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ console.log(braces.expand('a{b}c')); **Type**: `Number` -**Default**: `65,536` +**Default**: `10,000` **Description**: Limit the length of the input string. Useful when the input string is generated or your application allows users to pass a string, et cetera. @@ -186,18 +186,6 @@ 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` diff --git a/lib/constants.js b/lib/constants.js index 3280bea..2bb3b88 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -1,8 +1,7 @@ 'use strict'; module.exports = { - MAX_LENGTH: 1024 * 64, - MAX_SYMBOLS: 1024, + MAX_LENGTH: 10000, // Digits CHAR_0: '0', /* 0 */ diff --git a/lib/parse.js b/lib/parse.js index 7a724b8..145ea26 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,7 +1,6 @@ 'use strict'; const stringify = require('./stringify'); -const {isCorrectBraces, validateInput} = require('./validate-input'); /** * Constants @@ -9,7 +8,6 @@ const {isCorrectBraces, validateInput} = require('./validate-input'); const { MAX_LENGTH, - MAX_SYMBOLS, CHAR_BACKSLASH, /* \ */ CHAR_BACKTICK, /* ` */ CHAR_COMMA, /* , */ @@ -36,11 +34,6 @@ 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})`); @@ -311,43 +304,30 @@ const parse = (input, options = {}) => { push({ type: 'text', value }); } - flattenBlocks(stack) - markImbalancedBraces(ast); - push({ type: 'eos' }); - - return ast; -}; - -module.exports = parse; - -function markImbalancedBraces({nodes}) { // Mark imbalanced braces and brackets as invalid - for (const node of nodes) { - 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'; - - node.invalid = true; - } - - delete node.parent; - delete node.prev; - } -} - -function flattenBlocks(stack) { - let block; do { block = stack.pop(); - if (block.type === 'root') - continue; + if (block.type !== 'root') { + block.nodes.forEach(node => { + if (!node.nodes) { + if (node.type === 'open') node.isOpen = true; + if (node.type === 'close') node.isClose = true; + if (!node.nodes) node.type = 'text'; + node.invalid = true; + } + }); - // get the location of the block on parent.nodes (block's siblings) - let parent = stack.at(-1); - let index = parent.nodes.indexOf(block); - // replace the (invalid) block with its nodes - parent.nodes.splice(index, 1, ...block.nodes); + // get the location of the block on parent.nodes (block's siblings) + let parent = stack[stack.length - 1]; + let index = parent.nodes.indexOf(block); + // replace the (invalid) block with it's nodes + parent.nodes.splice(index, 1, ...block.nodes); + } } while (stack.length > 0); -} + + push({ type: 'eos' }); + return ast; +}; + +module.exports = parse; diff --git a/lib/validate-input.js b/lib/validate-input.js deleted file mode 100644 index 0f987b6..0000000 --- a/lib/validate-input.js +++ /dev/null @@ -1,12 +0,0 @@ -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}`); - } -}; diff --git a/test/braces.parse.js b/test/braces.parse.js index 09a2361..b814558 100644 --- a/test/braces.parse.js +++ b/test/braces.parse.js @@ -10,16 +10,6 @@ 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', () => {