Skip to content

Commit 0f25d10

Browse files
committed
Improve docs
1 parent 1d06c4d commit 0f25d10

File tree

2 files changed

+66
-72
lines changed

2 files changed

+66
-72
lines changed

README.md

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,48 @@ A library which parses and manipulates comma-delimited positive integer ranges (
66

77
Such strings are typically used in print dialogs to indicate which pages to print.
88

9-
Supported operations include
9+
Supported operations include:
1010

11-
- Addition (e.g., '1-2' + '3-5' => '1-5')
12-
- Subtraction (e.g., '1-10' - '5-9' => '1-4,10')
13-
- Inclusion check (e.g., '5' is in '1-10')
14-
- Intersection (e.g., '1-5''2-8' => '2-5')
11+
- Addition (e.g., `1-2,6` + `3-5` => `1-6`)
12+
- Subtraction (e.g., `1-10` - `5-9` => `1-4,10`)
13+
- Inclusion check (e.g., `3,7-9` is in `1-10`)
14+
- Intersection (e.g., `1-5``2-8` => `2-5`)
1515
- Array creation
1616

1717
Internal data are always *sorted and normalized* to the smallest possible
1818
representation.
1919

2020
## Usage
2121

22+
### Basic Example
23+
2224
Install via npm: `npm install multi-integer-range`
2325

26+
```js
27+
var MultiRange = require('multi-integer-range').MultiRange;
28+
29+
var pages = new MultiRange('1-5,12-15');
30+
pages.append(6).append([7,8]).append('9-11').subtract(2);
31+
console.log(pages.toString()); // '1,3-15'
32+
console.log(pages.has('5,9,12-14')); // true
33+
34+
// output
35+
console.log(pages.toArray()); // [5,9,12,13,14]
36+
console.log(pages.getRanges()); // [[5,5],[9,9],[12,14]]
37+
console.log(pages.isContinuous()); // false
38+
```
39+
2440
### Initialization
2541

26-
Most of the methods (including the constructor) take one *Initializer* parameter.
42+
Some methods (and the constructor) take one *Initializer* parameter.
2743
An initializer is an integer array, an array of `[number, number]` tuples,
2844
a string, a single integer, or another MultiRange instance.
2945

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.
46+
Pass it to the constructor to create a MultiRange object,
47+
or pass nothing to create an empty MultiRange object.
3248

33-
```
34-
Initializer:
49+
```ts
50+
type Initializer =
3551
string |
3652
number |
3753
( number | [number,number] )[] |
@@ -66,61 +82,30 @@ var mr = new MultiRange('3,\t8-3,2,3,\n10, 9 - 7 ');
6682
console.log(mr.toString()); // prints '2-10'
6783
```
6884

69-
### Manipulation and Comparison
70-
71-
```js
72-
var mr = new MultiRange('1-3,7-9');
73-
74-
// Addition
75-
mr.append('4-5')
76-
.append(6)
77-
.append(new MultiRange('10-11'))
78-
.appendRange(12, 15); // append 12-15
79-
80-
console.log('' + mr); // prints '1-15'
81-
82-
// Subtraction
83-
mr.subtract('2-8');
84-
console.log('' + mr); // prints '1,9-15'
85-
86-
// Intersection
87-
mr.intersect('1-14');
88-
console.log('' + mr); // prints '1,9-14'
89-
90-
// Equality check
91-
console.log(mr.equals('1,9-12,13,14')); // true
92-
93-
// Inclusion check
94-
console.log(mr.has(10)); // true
95-
console.log(mr.has(100)); // false
96-
console.log(mr.has('1,10-12')); // true
97-
98-
// Length (the total number of integers)
99-
console.log(mr.length()); // prints 8
100-
101-
// Continuity
102-
console.log(mr.isContinuous()); // false
103-
```
104-
105-
### Output
106-
107-
There are several ways to get the content of the MultiRange object.
108-
109-
```js
110-
var mr = new MultiRange([1,2,3,5,6,7,8,10]);
111-
112-
// As a string
113-
console.log(mr.toString()); // '1-3,5-8,10'
114-
115-
// Or concat an empty string to implicitly call #toString()
116-
console.log('' + mr); // '1-3,5-8,10'
117-
118-
// As an array which holds every integer (of course slow for large range)
119-
console.log(mr.toArray()); // [1,2,3,5,6,7,8,10]
120-
121-
// As an array of 2-element arrays
122-
console.log(mr.getRanges()); // [[1,3],[5,8],[10,10]]
123-
```
85+
### Methods
86+
87+
Manipulation methods are mutable and chainable by design.
88+
That is, for example, when you call `append(5)`, it will change
89+
the internal representation and return the modified self,
90+
rather than returning a new instance.
91+
To get the copy of the instance, use `clone()`, or alternatively the copy constructor (`var copy = new MultiRange(orig)`).
92+
93+
- `new MultiRange(data?: Initializer)` Creates a new MultiRange object.
94+
- `clone(): MultiRange` Clones this instance.
95+
- `append(value: Initializer): MultiRange` Appends to this instance.
96+
- `appendRange(min: number, max: number): MultiRange` Appends one range specified by the two parameters.
97+
- `subtract(value: Initializer): MultiRange` Subtracts from this instance.
98+
- `subtractRange(min: number, max: number): MultiRange` Subtracts one range specified by the two parameters.
99+
- `intersect(value: Initializer): MultiRange` Remove integers which are not included in `value` (aka intersection).
100+
- `has(value: Initializer): boolean` Checks if the instance contains the specified value.
101+
- `hasRange(min: number, max: number): boolean` Checks if the instance contains the range specified by the two parameters.
102+
- `isContinuous(): boolean` Checks if the current instance is continuous. Note that this returns false if the current range is empty.
103+
- `length(): number` Calculates how many numbers are effectively included in this instance. (`multirange('1-10,51-60,90').length()` returns 21)
104+
- `equals(cmp: Initializer): boolean` Checks if two MultiRange data are identical.
105+
- `toString(): string` Returns the string respresentation of this MultiRange.
106+
- `getRanges(): [number, number][]` Exports the whole range data as an array of [number, number] arrays.
107+
- `toArray(): number[]` Builds an array of integer which holds all integers in this MultiRange. Note that this may be slow and memory-consuming for large ranges such as '1-10000'.
108+
- `getIterator(): Object` Returns ES6-compatible iterator. See the description below.
124109

125110
### Iteration
126111

@@ -163,6 +148,8 @@ npm test
163148

164149
Report any bugs and suggestions using GitHub issues.
165150

151+
**Performance Considerations**: This library works efficiently for large ranges as long as they're *mostly* continuous (e.g., `1-10240000,20480000-50960000`). However, this library is not intended to be efficient with a heavily fragmentated set of integers which are scarcely continuous (for example, random 10000 integers between 1 to 1000000).
152+
166153
## Author
167154

168155
Soichiro Miki (https://github.com/smikitky)

multi-integer-range.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export class MultiRange {
6666
}
6767

6868
/**
69-
* Appends a range to this instance.
69+
* Appends to this instance.
70+
* @parasm value The data to append.
7071
*/
7172
public append(value: Initializer): MultiRange {
7273
if (typeof value === 'undefined') {
@@ -95,7 +96,8 @@ export class MultiRange {
9596
}
9697

9798
/**
98-
* Subtracts a range from this instance.
99+
* Subtracts from this instance.
100+
* @param value The data to subtract.
99101
*/
100102
public subtract(value: Initializer): MultiRange {
101103
if (typeof value === 'undefined') {
@@ -135,8 +137,7 @@ export class MultiRange {
135137

136138
/**
137139
* Remove integers which are not included in the given ranges (aka intersection).
138-
* Note that this modifies the original object
139-
* rather than returning the new MultiRange object.
140+
* @param value The data to calculate the intersetion.
140141
*/
141142
public intersect(value: Initializer): MultiRange {
142143
if (typeof value === 'undefined') {
@@ -231,7 +232,7 @@ export class MultiRange {
231232
}
232233

233234
/**
234-
* Returns the range data.
235+
* Exports the whole range data as an array of arrays.
235236
*/
236237
public getRanges(): Range[]
237238
{
@@ -241,9 +242,9 @@ export class MultiRange {
241242
}
242243

243244
/**
244-
* Checks if the specified value is included in the current range.
245+
* Checks if the instance contains the specified value.
245246
* @param value Value to be checked
246-
* @return True if the specified value is included in the range.
247+
* @return True if the specified value is included in the instance.
247248
*/
248249
public has(value: Initializer): boolean
249250
{
@@ -266,6 +267,12 @@ export class MultiRange {
266267
}
267268
}
268269

270+
/**
271+
* Checks if the instance contains the range specified by the two parameters.
272+
* @param min The minimum value of the range to subtract.
273+
* @param max The minimum value of the range to subtract.
274+
* @return True if the specified value is included in the instance.
275+
*/
269276
public hasRange(min: number, max: number): boolean
270277
{
271278
return this.has(new MultiRange([[min, max]]));

0 commit comments

Comments
 (0)