Last active
August 1, 2017 10:15
-
-
Save edulan/ceda565214ce8c872c84aa7b7d47363c to your computer and use it in GitHub Desktop.
Slice a string by given chunk sizes
This file contains 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
function sliceBy(str, [ chunkSize = -1, ...rest ] = []) { | |
// Guard against infinite recursion | |
if (chunkSize < 1) { | |
return [str] | |
} | |
if (str.length <= chunkSize) { | |
return [str] | |
} | |
return [ | |
str.slice(0, chunkSize), | |
...sliceBy(str.slice(chunkSize), rest) | |
] | |
} | |
sliceBy('371449635398431', [4, 6, 5]).join(' ') | |
// 3714 496353 98431 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment