Skip to content

Instantly share code, notes, and snippets.

@guipn
Last active December 13, 2015 16:39
Show Gist options
  • Save guipn/4942213 to your computer and use it in GitHub Desktop.
Save guipn/4942213 to your computer and use it in GitHub Desktop.
Example input: '(3,5)'
function parseInts(str) {
return str.split(/\s*,\s*/).map(function (v, i) {
switch (i) {
case 0: return parseInt(v.split('').pop(), 10);
case 1: return parseInt(v.split('').shift(), 10);
default: return parseInt(v, 10);
}
});
}
console.log(parseInts('(3,4,5)')); // [3, 4, 5]
console.log(parseInts('(3 , 4 , 5)')); // [3, 4, 5]
console.log(parseInts('(3 , 4 ,)')); // [3, 4, NaN]
console.log(parseInts('(3 , 4 ,5,6,7,8,9)')); // [3, 4, 5, 6, 7, 8, 9]
console.log(parseInts('aaa (3 , 4 ,5,6,7,8,9)')); // [3, 4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment