range.js 573 B

12345678910111213141516171819202122
  1. // Generate an integer Array containing an arithmetic progression. A port of
  2. // the native Python `range()` function. See
  3. // [the Python documentation](https://docs.python.org/library/functions.html#range).
  4. export default function range(start, stop, step) {
  5. if (stop == null) {
  6. stop = start || 0;
  7. start = 0;
  8. }
  9. if (!step) {
  10. step = stop < start ? -1 : 1;
  11. }
  12. var length = Math.max(Math.ceil((stop - start) / step), 0);
  13. var range = Array(length);
  14. for (var idx = 0; idx < length; idx++, start += step) {
  15. range[idx] = start;
  16. }
  17. return range;
  18. }