Skip to content

Instantly share code, notes, and snippets.

@edulan
Last active August 1, 2017 10:15
Show Gist options
  • Save edulan/ceda565214ce8c872c84aa7b7d47363c to your computer and use it in GitHub Desktop.
Save edulan/ceda565214ce8c872c84aa7b7d47363c to your computer and use it in GitHub Desktop.
Slice a string by given chunk sizes
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