Created
September 12, 2014 23:30
-
-
Save mcorrigan/e15075b7bfd3bf95426d to your computer and use it in GitHub Desktop.
JavaScript Range Function
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
// something comparable to PHP range method. Supports numbers and letters. | |
function range(start, end, step) { | |
start || alert('must have 1+ params'); | |
if (!end) {end = start; start = 0;} | |
var step = step || 1; | |
var range = []; | |
var is_num = !isNaN(start); | |
start = !is_num ? start.charCodeAt(0) : start; | |
end = !is_num ? end.charCodeAt(0) : end; | |
step = !is_num ? Math.max(Math.round(step), 1) : step; | |
step = start > end ? step * -1 : step; | |
while (start != end + step){ | |
range.push(is_num ? start : String.fromCharCode(start)); | |
start += step; | |
if ((step < 0 && start < end) || (step > 0 && end < start)) { | |
break; | |
} | |
} | |
return range; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment