Lazy number range generator
JavaScript
Permalink
Failed to load latest commit information.
.editorconfig Init Jan 11, 2017
.gitattributes Init Jan 11, 2017
.gitignore Init Jan 11, 2017
.travis.yml Init Jan 11, 2017
index.js Simplify the implementation Jan 19, 2017
license Init Jan 11, 2017
package.json 1.0.0 Jan 11, 2017
readme.md Init Jan 11, 2017
test.js Init Jan 11, 2017

readme.md

get-range Build Status

Lazy number range generator

Useful for iteration.

Similar to the Python 3 range function.

Install

$ npm install --save get-range

Usage

const getRange = require('get-range');

for (const i of getRange(4)) {
    console.log(i);
}
//=> 0
//=> 1
//=> 2
//=> 3

const r = getRange(0, 4, 2);
r.next().value;
//=> 0
r.next().value;
//=> 2

console.log(...getRange(0, -5, -1));
//=> [0, -1, -2, -3, -4]

Can replace for-loops in many cases:

// Before
for (let i = 0; i < 5; i++) {}

// After
for (const i of getRange(5)) {}

API

getRange(stop)

getRange(start, stop, [step])

Returns a Generator object (which is both an Iterable and Iterator).

start

Type: integer
Default: 0

Start from this number.

stop

Type: integer

Stop after this many numbers.

step

Type: integer
Default: 1
Minimum: 1

Distance between numbers.

License

MIT © Sindre Sorhus