You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A small library which parses and manipulates comma-delimited integer ranges (such as "1-3,8-10").
6
-
7
6
Such strings are typically used in print dialogs to indicate which pages to print.
8
7
9
8
Supported operations include:
@@ -12,8 +11,9 @@ Supported operations include:
12
11
- Subtraction (e.g., `1-10` - `5-9` => `1-4,10`)
13
12
- Inclusion check (e.g., `3,7-9` is in `1-10`)
14
13
- Intersection (e.g., `1-5` ∩ `2-8` => `2-5`)
14
+
- Unbounded ranges (e.g., `5-` to mean "all integers >= 5")
15
15
- Iteration using `for ... of`
16
-
- Array creation ("flatten")
16
+
- Array creation (a.k.a. "flatten")
17
17
18
18
Internal data are always *sorted and normalized* to the smallest possible
19
19
representation.
@@ -102,6 +102,7 @@ To get the copy of the instance, use `clone()`, or alternatively the copy constr
102
102
-`length(): number` Calculates how many numbers are effectively included in this instance. (ie, 5 for '3,5-7,9')
103
103
-`segmentLength(): number` Returns the number of range segments (ie, 3 for '3,5-7,9' and 0 for an empty range)
104
104
-`equals(cmp: Initializer): boolean` Checks if two MultiRange data are identical.
105
+
-`isUnbounded(): boolean` Returns if the instance is unbounded.
105
106
-`toString(): string` Returns the string respresentation of this MultiRange.
106
107
-`getRanges(): [number, number][]` Exports the whole range data as an array of [number, number] arrays.
107
108
-`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'.
@@ -114,17 +115,59 @@ The following methods are deprecated and may be removed in future releases:
114
115
-`hasRange(min: number, max: number): boolean` Use `has([[min, max]])` instead.
115
116
-`isContinuous(): boolean` Use `segmentLength() === 1` instead.
116
117
118
+
119
+
### Unbounded ranges
120
+
121
+
Starting from version 2.1, you can use unbounded (or infinite) ranges,
122
+
which look like this:
123
+
124
+
```js
125
+
// using the string parser...
126
+
var unbounded1 =newMultiRange('5-'); // all integers >= 5
127
+
var unbounded2 =newMultiRange('-3'); // all integers <= 3
128
+
var unbounded3 =newMultiRange('-'); // all integers
129
+
130
+
// or programmatically, using the JavaScript constant `Infinity`...
131
+
var unbounded4 =newMultiRange([[5, Infinity]]); // all integers >= 5
132
+
var unbounded5 =newMultiRange([[-Infinity, 3]]); // all integers <= 3
133
+
var unbounded6 =newMultiRange([[-Infinity, Infinity]]); // all integers
134
+
```
135
+
136
+
The manipulation methods work just as expected with unbounded ranges:
0 commit comments