Skip to content

Instantly share code, notes, and snippets.

@mary-ext
Created January 11, 2025 07:20
Show Gist options
  • Save mary-ext/71c718112ca9c4f9e08b3940ff5d34dc to your computer and use it in GitHub Desktop.
Save mary-ext/71c718112ca9c4f9e08b3940ff5d34dc to your computer and use it in GitHub Desktop.
const split = (str: string, delimiter: string, limit: number): string[] => {
const result: string[] = [];
let last = 0;
while (--limit > 0) {
const index = str.indexOf(delimiter, last);
if (index === -1) {
break;
}
result.push(str.slice(last, index));
last = index + delimiter.length;
}
result.push(str.slice(last));
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment