Skip to content

Instantly share code, notes, and snippets.

@NateScarlet
Created August 31, 2020 09:17
Show Gist options
  • Save NateScarlet/19a5b4d4b50768f127e98e041cdeb898 to your computer and use it in GitHub Desktop.
Save NateScarlet/19a5b4d4b50768f127e98e041cdeb898 to your computer and use it in GitHub Desktop.
/** Split string to exact n parts, last part may contains splitter. */
export default function splitN(v: string, splitter: string, n: number): string[] {
const parts = v.split(splitter);
const ret = parts.slice(0, n);
while (ret.length < n) {
ret.push('');
}
ret[n - 1] = parts.slice(n - 1).join(splitter);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment