Skip to content

Commit e786ef0

Browse files
committed
Add segmentLength() method and deprecate some methods
1 parent 5f68bcf commit e786ef0

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,24 @@ To get the copy of the instance, use `clone()`, or alternatively the copy constr
9393
- `new MultiRange(data?: Initializer)` Creates a new MultiRange object.
9494
- `clone(): MultiRange` Clones this instance.
9595
- `append(value: Initializer): MultiRange` Appends to this instance.
96-
- `appendRange(min: number, max: number): MultiRange` Appends one range specified by the two parameters.
9796
- `subtract(value: Initializer): MultiRange` Subtracts from this instance.
98-
- `subtractRange(min: number, max: number): MultiRange` Subtracts one range specified by the two parameters.
9997
- `intersect(value: Initializer): MultiRange` Remove integers which are not included in `value` (aka intersection).
10098
- `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)
99+
- `length(): number` Calculates how many numbers are effectively included in this instance. (ie, 5 for '3,5-7,9')
100+
- `segmentLength(): number` Returns the number of range segments (ie, 3 for '3,5-7,9' and 0 for an empty range)
104101
- `equals(cmp: Initializer): boolean` Checks if two MultiRange data are identical.
105102
- `toString(): string` Returns the string respresentation of this MultiRange.
106103
- `getRanges(): [number, number][]` Exports the whole range data as an array of [number, number] arrays.
107104
- `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'.
108105
- `getIterator(): Object` Returns ES6-compatible iterator. See the description below.
109106

107+
The following methods are deprecated and may be removed in future releases:
108+
109+
- `appendRange(min: number, max: number): MultiRange` Use `append([[min, max]])` instead.
110+
- `subtractRange(min: number, max: number): MultiRange` Use `subtract([[min, max]])` instead.
111+
- `hasRange(min: number, max: number): boolean` Use `has([[min, max]])` instead.
112+
- `isContinuous(): boolean` Use `segmentLength() === 1` instead.
113+
110114
### Iteration
111115

112116
**ES6 iterator**: If `Symbol.iterator` is defined in the runtime,

multi-integer-range.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class MultiRange {
8787
* Appends a specified range of integers to this isntance.
8888
* @param min The minimum value of the range to append.
8989
* @param max The minimum value of the range to append.
90+
* @deprecated Use `.append([[min, max]])` instead.
9091
*/
9192
public appendRange(min: number, max: number): MultiRange {
9293
let newRange: Range = [Math.max(0, min), max];
@@ -117,6 +118,7 @@ export class MultiRange {
117118
* Subtracts a specified range of integers from this instance.
118119
* @param min The minimum value of the range to subtract.
119120
* @param max The minimum value of the range to subtract.
121+
* @deprecated Use `.subtract([[min, max]])` instead.
120122
*/
121123
public subtractRange(min: number, max: number): MultiRange
122124
{
@@ -275,16 +277,29 @@ export class MultiRange {
275277
* @param min The minimum value of the range to subtract.
276278
* @param max The minimum value of the range to subtract.
277279
* @return True if the specified value is included in the instance.
280+
* @deprecated Use `.has([[min, max]])` instead.
278281
*/
279282
public hasRange(min: number, max: number): boolean
280283
{
281284
return this.has(new MultiRange([[min, max]]));
282285
}
283286

287+
/**
288+
* Returns the number of range segments.
289+
* For example, the segmentLength of `2-5,7,9-11' is 3.
290+
* Returns 0 for an empty instance.
291+
* @return The number of segments.
292+
*/
293+
public segmentLength(): number
294+
{
295+
return this.ranges.length;
296+
}
297+
284298
/**
285299
* Checks if the current instance is continuous.
286300
* Note that this returns false if the current range is empty.
287301
* @return True if the current range is continuous.
302+
* @deprecated Use `.segmentLength() === 1` instead.
288303
*/
289304
public isContinuous(): boolean
290305
{

test/multi-integer-range.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ describe('MultiRange', function() {
216216
assert.equal(mr('1,3,10-15,20-21').length(), 10);
217217
});
218218

219+
it('#segmentLength', function() {
220+
assert.equal(mr('').segmentLength(), 0);
221+
assert.equal(mr('5').segmentLength(), 1);
222+
assert.equal(mr('5-10').segmentLength(), 1);
223+
assert.equal(mr('1,3,10-15,20-21').segmentLength(), 4);
224+
});
225+
219226
it('#equals', function() {
220227
assert.isTrue(mr('').equals(''));
221228
assert.isTrue(mr('5').equals(mr('5')));

0 commit comments

Comments
 (0)