Created
January 4, 2021 08:59
-
-
Save lenconda/6287a28c5b4ee5904236be6bb92714ef to your computer and use it in GitHub Desktop.
chunk string into array
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
const chunk = (text: string, chunkSize = 128) => { | |
const traverse = ( | |
currentResult: string[], | |
text: string, | |
chunkSize: number, | |
): string[] => { | |
if (!text || text.length === 0) { | |
return currentResult; | |
} else { | |
const currentSegmentation = text.slice(0, chunkSize); | |
const nextSegmentation = text.slice(chunkSize); | |
return traverse( | |
currentResult.concat(currentSegmentation), | |
nextSegmentation, | |
chunkSize, | |
); | |
} | |
}; | |
return traverse([], text, chunkSize); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment