Skip to content

Instantly share code, notes, and snippets.

@lukabot
Last active November 23, 2016 10:03
Show Gist options
  • Save lukabot/3c251c4339bf4c517b37552d28d9e38d to your computer and use it in GitHub Desktop.
Save lukabot/3c251c4339bf4c517b37552d28d9e38d to your computer and use it in GitHub Desktop.
function f(str, seperator, limit) {
// split string by limit
// .split eventually add one more elements.
let arr = str.split(seperator, limit);
if(typeof limit === "number") {
// find last element's index, push it to the original array
let index = arr.reduce(function(prev, curr, i){
return str.indexOf(curr, prev);
}, 0);
arr.pop();
arr.push(str.substring(index));
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment