Last active
January 12, 2022 07:35
-
-
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.
This file contains 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
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)] : []) | |
]; | |
}; |
This file contains 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
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