Skip to content

Instantly share code, notes, and snippets.

@corymartin
Last active December 16, 2015 00:29
Show Gist options
  • Save corymartin/5347482 to your computer and use it in GitHub Desktop.
Save corymartin/5347482 to your computer and use it in GitHub Desktop.
Tiny array range api
/**
* range(2, 6) // [2,3,4,5,6]
* range(5).to(8) // [5,6,7,8]
* range(4).until(10) // [4,5,6,7,8,9]
*/
var range = function(s, e) {
if (e == null) {
return {
to: function(e) {
return range(s, e);
}
, until: function(e) {
return range(s, e >= s ? e-1 : e+1);
}
}
}
var arr = [];
var ispos = e >= s;
for (; ispos ? s <= e : e <= s; ispos ? s++ : s--) {
arr.push(s);
}
return arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment