split-skip
Splits a String into an Array of substrings with the option to skip some cases where the separator is found, based on some truthy condition.
Node.js and Browser ready.
Installation
npm install split-skip
CLI
npm install split-skip -g
npx split-skip --help
Usage
const splitSkip = require('split-skip');
const str = 'Some,String,as,an,Input';
const separator = ',';
const skipState = (state, char, i) => {
return state;
};
const resultArray = splitSkip(str, separator, skipState);
Example
As an input we have a string representing the parameters definition of a function from where we want to get each individual parameter. One idea of doing this is to split on every comma, but skipping the commas that are present on destructuring parameters definitions.
Using splitSkip, it could be like:
const input = `[destru,cturu,cing]=[1],param,{dd,ee,ff}={dd:{b:1,c:2,arr:[1,6]}},last`;
const result = splitSkip(input, ',', (state, char, i) => {
if ('{[('.indexOf(char) >= 0) {
state.skip += 1;
}
if ('}])'.indexOf(char) >= 0) {
state.skip -= 1;
}
return state;
});