boolean-parser-js icon indicating copy to clipboard operation
boolean-parser-js copied to clipboard

Extra white space cause maximum stack error

Open paulck opened this issue 7 years ago • 2 comments

Hi,

if you invoke the parser with string '(a AND b) OR c' will work but with ' (a AND b) OR c' it will end with :

Unhandled rejection RangeError: Maximum call stack size exceeded

paulck avatar Mar 14 '18 09:03 paulck

Thanks, I'll look into it :)

riichard avatar Mar 14 '18 15:03 riichard

For those who are looking, I found a quick fix (for my issue at least).

I recursion comes when the andQuery is split is not successfully split into parts and the parseBooleanQuery is called multiple times on the same string. The solution below checks that if the andQuery is the same as the first and only index of ands array, it just returns that string.

Hope it helps others get a jump on solving it for their own unique cases.

function parseBooleanQuery(searchPhrase) {

    //... 

    if (ands.length === 1 && ands[0] === andQuery) {
      andPath.push(andQuery);
    }
    else {
      // Iterate through all the strings from the AND query
      for (var i = 0; i < ands.length; i++) {
        // If the string contains brackets, parse it recursively, and add it to
        // `nestedPaths`.
        if (containsBrackets(ands[i])) {
          nestedPaths.push(parseBooleanQuery(ands[i]));
        }

        // If it doesn't. Push the word to `andPath`.
        else {
          andPath.push(ands[i]);
        }
      }
    }

     // ...

vchapple17 avatar May 09 '23 18:05 vchapple17