Skip to content

Instantly share code, notes, and snippets.

@hperrin
Last active January 12, 2022 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hperrin/8ffe2146e81da81facfd3be42dfbde0c to your computer and use it in GitHub Desktop.
Save hperrin/8ffe2146e81da81facfd3be42dfbde0c to your computer and use it in GitHub Desktop.
splitn.js, a split function that returns everything after the limit in the last element.
export default const splitn = (s, d, n = Infinity) => {
const a = s.split(d);
return [
...a.slice(0, n - 1),
...(a.length >= n ? [a.slice(n - 1).join(d)] : [])
];
};
export default const splitn = (s: string, d: string, n = Infinity) => {
const a = s.split(d);
return [
...a.slice(0, n - 1),
...(a.length >= n ? [a.slice(n - 1).join(d)] : [])
];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment