Skip to content

Commit 790455b

Browse files
committed
Export multirange() shorthand function
1 parent cfdec03 commit 790455b

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ Install via npm: `npm install multi-integer-range`
2323
### Initialization
2424

2525
Initialization can be done with an integer array, a string, or another MultiRange instance.
26+
A shorthand constructor function `multirange()` is also available.
27+
Use whichever you prefer.
2628

2729
```js
2830
var MultiRange = require('multi-integer-range').MultiRange;
2931

30-
var mr = new MultiRange([7, 2, 9, 1, 8, 3]);
32+
var mr1 = new MultiRange([7, 2, 9, 1, 8, 3]);
3133
var mr2 = new MultiRange('1-2, 3, 7-9');
32-
var mr3 = new MultiRange(mr);
34+
var mr3 = new MultiRange(mr1);
35+
36+
// function-style
37+
var multirange = require('multi-integer-range').multirange;
38+
var mr4 = multirange('1,2,3,7,8,9'); // the same as `new MultiRange`
3339
```
3440

3541
Internal data are always sorted and normalized,
36-
so the above three constructor create instances with identical data.
42+
so the above four (`mr1`-`mr4`) hold a instance with identical range data.
3743

3844
The string parser is permissive and accepts space characters
3945
before/after comma/hyphens. Order is not important either, and
@@ -101,8 +107,7 @@ console.log(mr.getRanges()); // [[1,3],[5,8]]
101107
you can simply iterate over the instance using the `for ... of` statement:
102108

103109
```js
104-
var pages = new MultiRange('2,5-7');
105-
for (let page of pages) {
110+
for (let page of multirange('2,5-7')) {
106111
console.log(page);
107112
}
108113
// prints 2, 5, 6, 7
@@ -112,8 +117,7 @@ If `Symbol.iterator` is not defined, you can still access the iterator
112117
implementation and use it manually like this:
113118

114119
```js
115-
var pages = new MultiRange('2,5-7'),
116-
it = pages.getIterator(),
120+
var it = multirange('2,5-7').getIterator(),
117121
page;
118122
while (!(page = it.next()).done) {
119123
console.log(page.value);

multi-integer-range.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,8 @@ export class MultiRange {
275275
if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
276276
MultiRange.prototype[Symbol.iterator] = MultiRange.prototype.getIterator;
277277
}
278+
279+
// A shorthand function to get a new MultiRange instance
280+
export function multirange(data: string | number[] | MultiRange): MultiRange {
281+
return new MultiRange(data);
282+
}

test/multi-integer-range.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var MultiRange = require('../lib/multi-integer-range').MultiRange;
1+
var multi_integer_range = require('../lib/multi-integer-range');
2+
var MultiRange = multi_integer_range.MultiRange;
3+
var multirange = multi_integer_range.multirange;
24
var assert = require('chai').assert;
35

46
describe('MultiRange', function() {
5-
function mr(init) {
6-
return new MultiRange(init);
7-
}
7+
var mr = multirange;
88

99
function t(mr, expected) {
1010
assert.strictEqual(mr.toString(), expected);

0 commit comments

Comments
 (0)