range.js
This library provides a ES6 - ready lazy ranges.
Until ES6 is implemented, it is transpiled down to ES5.
Installation
npm install range-js
Usage
Node.js
var Range = Range;
Vanilla Browser JS
Browser builds are found in dist/browser/index.js.
The Range constructor is exposed as window.IterateableRange.
Babelify (Browserify + Babel)
If you are using Babelify you can import the module in ES6 - Style.
;
Basic Usage
var range = 1 10; // => Range { start: 1, end: 10, step: 1 } console // => 10console; // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Range objects expose @@iterator so you can use for ... of to iterate.
var alphabet = 'a' 'z'; for let letter of alphabet console;
... but generally you'll be using the forEach method.
var alphabet = 'a' 'z'; alphabet;
Step
You can specify a step number for the range.
let range = 1 100 5; console; // => [1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96]console; // => 20
Infinity + Limit
The end of the range may be Infinity but only in combination with the limit (4th) parameter equal to a positive integer.
let range = 0 Infinity 10000 10; console; // => [0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000]console; // => 10