Skip to content

Instantly share code, notes, and snippets.

@sergueyarellano
Created June 23, 2016 06:42
Show Gist options
  • Save sergueyarellano/b4c30820d82b03637edca565b132f421 to your computer and use it in GitHub Desktop.
Save sergueyarellano/b4c30820d82b03637edca565b132f421 to your computer and use it in GitHub Desktop.
function splitBy (by, str) {
return (function splitByFourAux (input, index, acc, string) {
return index > input.length -1 ?
(string ? acc.concat(string) : acc) :
index % by === by - 1 ?
splitByFourAux(input, index + 1, acc.concat(string.concat(input.charAt(index))), '') :
splitByFourAux(input, index + 1, acc, string.concat(input.charAt(index)));
})(str, 0, [], '');
}
splitBy(3, '222244443333'); //["222", "244", "443", "333"]
splitBy(2, '222244443333'); //["22", "22", "44", "44", "33", "33"]
@sergueyarellano
Copy link
Author

Although, you could do:

str.split(/(\d{4})/).filter(Boolean) // By groups of 4

XD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment