Last active
April 25, 2020 14:52
-
-
Save adriancmiranda/97fb27cec831dab9fc8975aa60cbbf7d to your computer and use it in GitHub Desktop.
The range type represents a sequence of numbers and is commonly used for looping a specific number of times in for loops.
This file contains hidden or 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
export function range(start, stop, step = 1, circularFill = false, map = (value) => value) { | |
if (typeof stop === 'undefined') { | |
stop = start; | |
start = 0; | |
} | |
if (step > 0 && start >= stop) { | |
step = -step; | |
} | |
if (step < 0 && start <= stop) { | |
return []; | |
} | |
let index = start; | |
const result = []; | |
if (circularFill) { | |
const size = start + stop; | |
for (index; step > 0 ? index < size : index > size; index += step) { | |
result.push(map(index % stop)); | |
} | |
return result; | |
} | |
for (index; step > 0 ? index < stop : index > stop; index += step) { | |
result.push(map(index)); | |
} | |
return result; | |
} | |
export default range; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Range examples: