Skip to content

Commit 03eda6a

Browse files
committed
Improve document
1 parent a25a894 commit 03eda6a

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,21 @@ Install via npm: `npm install multi-integer-range`
2323

2424
### Initialization
2525

26-
Initialization can be done with an integer array, an array of
27-
`[number, number]` tuples, a string, or another MultiRange instance.
26+
Most of the methods (including the constructor) take one *Initializer* parameter.
27+
An initializer is an integer array, an array of `[number, number]` tuples,
28+
a string, a single integer, or another MultiRange instance.
29+
30+
Pass it to the constructor to create a MultiRange object. Of course you can
31+
create an empty MultiRange object if no argument is passed to the constructor.
32+
33+
```
34+
Initializer:
35+
string |
36+
number |
37+
( number | [number,number] )[] |
38+
MultiRange;
39+
```
40+
2841
A shorthand constructor function `multirange()` is also available.
2942
Use whichever you prefer.
3043

@@ -34,15 +47,15 @@ var MultiRange = require('multi-integer-range').MultiRange;
3447
var mr1 = new MultiRange([7, 2, 9, 1, 8, 3]);
3548
var mr2 = new MultiRange('1-2, 3, 7-9');
3649
var mr3 = new MultiRange([[1,3], [7,9]]);
37-
var mr4 = new MultiRange(mr1);
50+
var mr4 = new MultiRange(mr1); // clone
3851

3952
// function-style
4053
var multirange = require('multi-integer-range').multirange;
4154
var mr5 = multirange('1,2,3,7,8,9'); // the same as `new MultiRange`
4255
```
4356

4457
Internal data are always sorted and normalized,
45-
so the above four (`mr1`-`mr5`) hold a instance with identical range data.
58+
so the above five (`mr1`-`mr5`) hold a instance with identical range data.
4659

4760
The string parser is permissive and accepts space characters
4861
before/after comma/hyphens. Order is not important either, and
@@ -94,19 +107,19 @@ console.log(mr.isContinuous()); // false
94107
There are several ways to get the content of the MultiRange object.
95108

96109
```js
97-
var mr = new MultiRange([1,2,3,5,6,7,8]);
110+
var mr = new MultiRange([1,2,3,5,6,7,8,10]);
98111

99112
// As a string
100-
console.log(mr.toString()); // '1-3,5-8'
113+
console.log(mr.toString()); // '1-3,5-8,10'
101114

102115
// Or concat an empty string to implicitly call #toString()
103-
console.log('' + mr); // '1-3,5-8'
116+
console.log('' + mr); // '1-3,5-8,10'
104117

105118
// As an array which holds every integer (of course slow for large range)
106-
console.log(mr.toArray()); // [1,2,3,5,6,7,8]
119+
console.log(mr.toArray()); // [1,2,3,5,6,7,8,10]
107120

108121
// As an array of 2-element arrays
109-
console.log(mr.getRanges()); // [[1,3],[5,8]]
122+
console.log(mr.getRanges()); // [[1,3],[5,8],[10,10]]
110123
```
111124

112125
### Iteration
@@ -148,7 +161,7 @@ npm test
148161

149162
### Bugs
150163

151-
Use GitHub issues.
164+
Report any bugs and suggestions using GitHub issues.
152165

153166
## Author
154167

test/multi-integer-range.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,27 @@ describe('MultiRange', function() {
1818
t(mr('1 -3, 5,\t7-10\n'), '1-3,5,7-10');
1919
});
2020

21-
it('must accept ranges with random/reverse order', function() {
21+
it('must parse string with random/reverse order', function() {
2222
t(mr('1,8,2-4,7,5-6,10-9'), '1-10');
2323
t(mr('10-8,7-5,1-4'), '1-10');
2424
});
2525

26-
it('must initialize with an integer', function() {
26+
it('must initialize with a single integer', function() {
2727
t(mr(2), '2');
2828
});
2929

3030
it('must initialize with an array', function() {
3131
t(mr([]), '');
3232
t(mr([1,10,8,5,9]), '1,5,8-10');
3333
t(mr([[2,5],[7,10]]), '2-5,7-10');
34+
35+
// Mixed array like this is not recommended nor explicitly
36+
// documented, but it works
3437
t(mr([5,[8,10],[12,15]]), '5,8-10,12-15');
3538
});
3639

3740
it('must construct with existing MultiRange', function() {
38-
t(mr(mr('5-10')), '5-10');
41+
t(mr(mr('5-10')), '5-10'); // aka clone
3942
});
4043

4144
it('must throw an error for invalid input', function() {

0 commit comments

Comments
 (0)