Skip to content

Instantly share code, notes, and snippets.

@stanch
Created April 3, 2014 07:23
Show Gist options
  • Save stanch/9949772 to your computer and use it in GitHub Desktop.
Save stanch/9949772 to your computer and use it in GitHub Desktop.
/* "a123b45c" → ["a", 123, "b", 45, "c"] */
function nat(x) {
// digit-parts
var d = x.split(/[^0-9]+/); // ["", "123", "45, ""]
// alpha-parts
var a = x.split(/[0-9]+/); // ["a", "b", "c"]
// helpers
function nonEmpty(x) { return x != "" }
function toInt(x) { return x - 0 }
// the one that does not start with a "" goes first
var f = d[0]=="" ? a.filter(nonEmpty) : d.filter(nonEmpty).map(toInt);
// the other one goes second
var s = d[0]=="" ? d.filter(nonEmpty).map(toInt) : a.filter(nonEmpty);
// intersperse/merge
var r = [];
for (var i=0; i<Math.max(f.length, s.length); i++) {
if (i < f.length) r.push(f[i]);
if (i < s.length) r.push(s[i]);
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment