Last active
December 16, 2015 00:29
-
-
Save corymartin/5347482 to your computer and use it in GitHub Desktop.
Tiny array range api
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
/** | |
* 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