Skip to content

Instantly share code, notes, and snippets.

@lenconda
Created January 4, 2021 08:59
Show Gist options
  • Save lenconda/6287a28c5b4ee5904236be6bb92714ef to your computer and use it in GitHub Desktop.
Save lenconda/6287a28c5b4ee5904236be6bb92714ef to your computer and use it in GitHub Desktop.
chunk string into array
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