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
Copy file name to clipboardExpand all lines: README.md
+20-24
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,16 @@
6
6
7
7
A small library that parses comma-delimited integer ranges (such as `"1-3,8-10"`) and manipulates such range data. This type of data is commonly used to specify which lines to highlight or which pages to print.
The fundamental data structure of this package is a **normalized** array of `[min, max]` tuples, as shown below. Here, "normalized" means the range data is in the smallest possible representation and is sorted in ascending order. You can denote an unbounded (aka infinite) range using the JavaScript constant `Infinity`.
61
+
The fundamental data structure of this package is a **normalized** array of `[min, max]` tuples, as shown below. Here, 'normalized' means the range data is in the smallest possible representation and is sorted in ascending order. You can denote an unbounded (aka infinite) range using the JavaScript constant `Infinity`.
62
62
63
63
<!-- prettier-ignore -->
64
64
```ts
65
65
typeRange=readonly [min: number, max: number];
66
66
typeMultiIntegerRange=readonlyRange[];
67
-
typeMIR=MultiIntegerRange; // short alias
68
67
69
68
// Examples of normalized MultiIntegerRanges
70
69
[[1, 3], [5, 6], [9, 12]] // 1-3,5-6,9-12
@@ -80,7 +79,7 @@ type MIR = MultiIntegerRange; // short alias
80
79
[[Infinity, Infinity]] // makes no sense
81
80
```
82
81
83
-
Most functions take one or two **normalized**`MultiIntegerRange`s as shown above to work correctly. To produce a valid normalized `MultiIntegerRange`, you can use `normalize()`, `parse()` or `initialize()`. (You can write a normalized `MultiIntgerRange` by hand as shown above, too.)
82
+
Most functions take one or two **normalized**`MultiIntegerRange`s as shown above to work correctly. To produce a valid normalized `MultiIntegerRange`, you can use `normalize()`, `parse()` or `initialize()`. You can write a normalized `MultiIntgerRange` by hand as shown above, too.
84
83
85
84
`normalize(data?: number | (number | Range)[])` creates a normalized `MultiIntegerRange` from a single integer or an unsorted array of integers/`Range`s. This and `initialize` are the only functions that can safely take an unsorted array. Do not pass unnormalized range data to other functions.
86
85
@@ -125,7 +124,9 @@ console.log(
125
124
126
125
See [api-reference.md](api-reference.md).
127
126
128
-
## Iteration
127
+
## Tips
128
+
129
+
### Iteration
129
130
130
131
Since a `MultiIntegerRange` is just an array of `Range`s, if you naively iterate over it (e.g., in a for-of loop), you'll simply get each `Range` tuple one by one. To iterate each integer contained in the `MultiIntegerRange` instead, use `iterate()` like so:
Intersection is especially useful to "trim" unbounded ranges.
153
152
154
153
```ts
155
154
const userInput ='-5,15-';
156
-
const pagesInMyDoc = [[1, 20]]; //(1-20)
155
+
const pagesInMyDoc = [[1, 20]]; // 1-20
157
156
const pagesToPrint =mr.intersect(
158
157
mr.parse(userInput, { parseUnbounded: true }),
159
158
pagesInMyDoc
@@ -165,23 +164,17 @@ for (const page of mr.iterate(pagesToPrint)) await printPage(page);
165
164
166
165
For compatibility purposes, version 5 exports the `MultiRange` class and `multirange` function, which is mostly compatible with the 4.x API but has been rewritten to use the new functional API under the hood. See the [4.x documentation](https://github.com/smikitky/node-multi-integer-range/tree/v4.0.9) for the usage. The use of this compatibility layer is discouraged because it is not tree-shakable and has no performance merit. Use this only during migration. These may be removed in the future.
167
166
168
-
## Changelog
169
-
170
-
See [CHANGELOG.md](CHANGELOG.md).
171
-
172
167
## Caveats
173
168
174
-
**Performance Considerations**: This library works efficiently for large ranges
175
-
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 fragmented set of integers that are scarcely continuous (e.g., random 10000 integers between 1 to 1000000).
169
+
**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 fragmented set of integers that are scarcely continuous (e.g., random 10000 integers between 1 to 1000000).
176
170
177
-
**No Integer Type Checks**: Make sure you are not passing floating-point `number`s
178
-
to this library. For example, don't do `normalize(3.14)`. For performance reasons, the library does not check if a passed number is an integer. Passing a float will result in unexpected and unrecoverable behavior.
171
+
**No Integer Type Checks**: Make sure you are not passing floating-point `number`s to this library. For example, don't do `normalize(3.14)`. For performance reasons, the library does not check if a passed number is an integer. Passing a float will result in unexpected and unrecoverable behavior.
179
172
180
173
## Comparison with Similar Libraries
181
174
182
-
[range-parser](https://www.npmjs.com/package/range-parser) specializes in parsing range requests in HTTP headers as defined in RFC 7233. It comes with behavior that cannot be turned off and is inappropriate for other purposes. For example, `'-5'` means "last 5 bytes".
175
+
[range-parser](https://www.npmjs.com/package/range-parser) specializes in parsing range requests in HTTP headers as defined in RFC 7233, and it behaves in a way that is usually inappropriate for other purposes. For example, `'-5'` means "last 5 bytes".
183
176
184
-
[parse-numeric-range](https://www.npmjs.com/package/parse-numeric-range) is fine for small ranges, but it always builds a "flat" array, so it is very inefficient for large ranges such as byte ranges. Also, whether you like it or not, it handles overlapping or descending ranges as-is without normalization. For example, `'4-2,1-3'` results in `[4, 3, 2, 1, 2, 3]`.
177
+
[parse-numeric-range](https://www.npmjs.com/package/parse-numeric-range) is fine for small ranges, but it always builds a "flat" array, which makes it very inefficient for large ranges such as byte ranges. Also, whether you like it or not, it handles overlapping or descending ranges as-is, without normalization. For example, `'4-2,1-3'` results in `[4, 3, 2, 1, 2, 3]`.
185
178
186
179
multi-integer-range is a general-purpose library for handling this type of data structure. It has a default parser that is intuitive enough for many purposes, but you can also use a custom parser. Its real value lies in its ability to treat normalized ranges as intermediate forms, allowing for a variety of mathematical operations. See the [API reference](api-reference.md).
187
180
@@ -190,12 +183,11 @@ multi-integer-range is a general-purpose library for handling this type of data
0 commit comments