range.js 857 B

12345678910111213141516171819202122232425262728293031323334
  1. var utils = require('../utils')
  2. , nodes = require('../nodes');
  3. /**
  4. * Returns a list of units from `start` to `stop`
  5. * by `step`. If `step` argument is omitted,
  6. * it defaults to 1.
  7. *
  8. * @param {Unit} start
  9. * @param {Unit} stop
  10. * @param {Unit} [step]
  11. * @return {Expression}
  12. * @api public
  13. */
  14. function range(start, stop, step){
  15. utils.assertType(start, 'unit', 'start');
  16. utils.assertType(stop, 'unit', 'stop');
  17. if (step) {
  18. utils.assertType(step, 'unit', 'step');
  19. if (0 == step.val) {
  20. throw new Error('ArgumentError: "step" argument must not be zero');
  21. }
  22. } else {
  23. step = new nodes.Unit(1);
  24. }
  25. var list = new nodes.Expression;
  26. for (var i = start.val; i <= stop.val; i += step.val) {
  27. list.push(new nodes.Unit(i, start.type));
  28. }
  29. return list;
  30. }
  31. range.params = ['start', 'stop', 'step'];
  32. module.exports = range;