Created
November 26, 2022 20:21
-
-
Save fedeghe/b6065af8e6f5cd0a7b7e9897b754bc64 to your computer and use it in GitHub Desktop.
getSplitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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