range.js 583 B

1234567891011121314151617181920212223
  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. 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. }
  19. module.exports = range;