split-string icon indicating copy to clipboard operation
split-string copied to clipboard

Index passed to state can sometimes be incorrect

Open derek-miller opened this issue 6 years ago • 0 comments

Using the state param within the keep() function has the incorrect index when encountering a quote. Looking at the code, state.index = i, but then later i is changed without updating state

 $ node 
Welcome to Node.js v12.3.1.
Type ".help" for more information.
> const split = require('split-string');
undefined
> split('a\\"', {
...   separator: ' ',
...   quotes: ['"'],
...   keep: (value, state) => {
.....     console.log({
.......       value,
.......       prev: state.prev(),
.......       index: state.index,
.......       charAtIndex: state.input[state.index],
.......       input: state.input
.......     });
.....     if (state.input[state.index] !== value) {
.......       throw new Error()
.......     }
.....     return true;
.....   }
... });
{
  value: 'a',
  prev: undefined,
  index: 0,
  charAtIndex: 'a',
  input: 'a\\"'
}
{ value: '\\', prev: 'a', index: 1, charAtIndex: '\\', input: 'a\\"' }
{ value: '"', prev: '\\', index: 1, charAtIndex: '\\', input: 'a\\"' }
Thrown:
Error
    at keep (repl:13:13)
    at append (./node_modules/split-string/index.js:42:18)
    at module.exports (./node_modules/split-string/index.js:68:9)

derek-miller avatar Oct 24 '19 22:10 derek-miller