Skip to content

Instantly share code, notes, and snippets.

@fedeghe
Created November 26, 2022 20:21
Show Gist options
  • Save fedeghe/b6065af8e6f5cd0a7b7e9897b754bc64 to your computer and use it in GitHub Desktop.
Save fedeghe/b6065af8e6f5cd0a7b7e9897b754bc64 to your computer and use it in GitHub Desktop.
getSplitter
var testone = require('@fedeghe/testone'),
benchs = [
{
in: ['"', ',', `a,,"a;.b,c",1.4.3.2,3,r,"c.,d r;t",s`],
out: [
'a', '', '"a;.b,c"', '1.4.3.2', '3', 'r', '"c.,d r;t"', 's'
]
},{
in: ["'", ';', `a;;'a,b;c';r;'c.,d r;t';s`],
out: [ 'a', '', "'a,b;c'", 'r', "'c.,d r;t'", 's' ]
}
],
getSplitter = (delimiter, separator) =>
str => str.split(separator).reduce(
(acc, s) => {
var has = s.includes(delimiter);
if (acc.flag) {
acc.r[acc.i] += `${separator}${s}`;
if (has) {
acc.i++;
acc.flag = false;
}
} else {
acc.r[acc.i] = s;
acc.flag = has;
if (!has) acc.i++;
}
return acc;
}, {r: [], flag:false, i: 0}
).r,
f = (del, sep, str) => {
var splitter = getSplitter(del, sep)
return splitter(str)
};
testone(benchs, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment