Features:
- The browser and node are both supported.
- Simple: Always returns an array.
- Numeric and decimal ranges.
- Letter ranges.
- Plain syntax
range('a', 'z')
and Ruby-stylerange('a..z')
. - Reversed ranges.
- Ranges with steps.
$ npm install range.js
var range = require('range.js');
Grab lib/range.js and include it in your HTML document. If an AMD or CommonJS loader is present it will be used, otherwise range
is assigned to window.range
.
In case you are using component (you should, it's awesome!):
$ component install js-coder/range.js
var range = require('range.js');
There are two different syntaxes, the plain one and the Ruby style one.
// Plain syntax:
range(from, to, step);
// Ruby style syntax:
range('from..to', step);
For convience, the rest of this documentation will use the plain syntax.
range(1, 5); // [1, 2, 3, 4, 5]
range(-2, 2); // [-2, -1, 0, 1, 2]
range(0, 0.5, 0.1); // Approximately, JS floats are not exact: [0.1, 0.2, 0.3, 0.4, 0.5]
range('a', 'd'); // ['a', 'b', 'c', 'd']
range('A', 'D'); // ['A', 'B', 'C', 'D']
range('y', 'B'); // ['y', 'z', 'A', 'B']
range('Y', 'b'); // ['Y', 'Z', 'a', 'b']
range(0, 9, 3); // [0, 3, 6, 9]
range('a', 'e', 2); // ['a', 'c', 'e']
range(5, 0); // [5, 4, 3, 2, 1, 0]
range('e', 'a'); ['e', 'd', 'c', 'b', 'a']
range('1..5'); // range(1, 5)
range('a..z'); // range('a', 'z')
// Steps:
range('1..5', 2); // range(1, 5, 2)
// Exclusive ranges:
range('1...5'); // [1, 2, 3, 4]
Compares ranges by value.
var a = range(1, 5);
var b = range(1, 5);
var c = range('a..z');
range.equals(a, b); // true
range.equals(a, c); // false
range.overlaps(range('a..z'), range('e..f')); // true
range.overlaps(range(1, 10), range(1, 9)); // true
range.overlaps(range(1, 5), range(6, 10)); // false
The test suite uses the mocha testing framework and the chai assertion library.
Running the tests from the command line:
$ git clone git://github.com/js-coder/range.js.git && cd range.js
$ npm install
$ grunt test
To run the tests in your browser, Clone this repository, and visit ./spec/index.html.
- Add benchmarks