Skip to content

Commit 0e6102a

Browse files
committed
Use consistent initializing parameters throughtout the class.
1 parent 89be620 commit 0e6102a

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

multi-integer-range.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,14 @@ export class MultiRange {
7070
/**
7171
* Appends a range to this instance.
7272
*/
73-
public append(value: number | string | MultiRange): MultiRange {
74-
if (typeof value === 'number') {
75-
return this.appendRange(value, value);
73+
public append(value: Initializer): MultiRange {
74+
if (typeof value === 'undefined') {
75+
throw new TypeError('Invalid input');
7676
} else if (value instanceof MultiRange) {
7777
for (let r of value.ranges) this.appendRange(r[0], r[1]);
7878
return this;
79-
} else if (typeof value === 'string') {
79+
} else {
8080
return this.append(new MultiRange(value));
81-
} else if (typeof value !== 'undefined') {
82-
throw new TypeError('Invalid input');
8381
}
8482
}
8583

@@ -101,16 +99,14 @@ export class MultiRange {
10199
/**
102100
* Subtracts a range from this instance.
103101
*/
104-
public subtract(value: number | string | MultiRange): MultiRange {
105-
if (typeof value === 'number') {
106-
return this.subtractRange(value, value);
102+
public subtract(value: Initializer): MultiRange {
103+
if (typeof value === 'undefined') {
104+
throw new TypeError('Invalid input');
107105
} else if (value instanceof MultiRange) {
108106
for (let r of value.ranges) this.subtractRange(r[0], r[1]);
109107
return this;
110-
} else if (typeof value === 'string') {
108+
} else {
111109
return this.subtract(new MultiRange(value));
112-
} else if (typeof value !== 'undefined') {
113-
throw new TypeError('Invalid input');
114110
}
115111
}
116112

@@ -144,10 +140,10 @@ export class MultiRange {
144140
* Note that this modifies the original object
145141
* rather than returning the new MultiRange object.
146142
*/
147-
private intersect(value: number | string | MultiRange): MultiRange {
148-
if (typeof value === 'number') {
149-
return this.intersect(new MultiRange([value]));
150-
} else {
143+
private intersect(value: Initializer): MultiRange {
144+
if (typeof value === 'undefined') {
145+
throw new TypeError('Invalid input');
146+
} else if (value instanceof MultiRange) {
151147
let result = new MultiRange();
152148
let that = new MultiRange(value);
153149
let jstart = 0; // used for optimization
@@ -165,6 +161,8 @@ export class MultiRange {
165161
}
166162
this.ranges = result.ranges;
167163
return this;
164+
} else {
165+
return this.intersect(new MultiRange(value));
168166
}
169167
}
170168

@@ -248,12 +246,10 @@ export class MultiRange {
248246
* @param value Value to be checked
249247
* @return True if the specified value is included in the range.
250248
*/
251-
public has(value: number | string | (number|Range)[] | MultiRange): boolean
249+
public has(value: Initializer): boolean
252250
{
253-
if (typeof value === 'number') {
254-
return this.has(new MultiRange([value]));
255-
} else if (typeof value === 'string' || value instanceof Array) {
256-
return this.has(new MultiRange(value));
251+
if (typeof value === 'undefined') {
252+
throw new TypeError('Invalid input');
257253
} else if (value instanceof MultiRange) {
258254
let s = 0;
259255
let len = this.ranges.length;
@@ -267,7 +263,7 @@ export class MultiRange {
267263
}
268264
return true;
269265
} else {
270-
throw new TypeError('Invalid input');
266+
return this.has(new MultiRange(value));
271267
}
272268
}
273269

@@ -302,18 +298,20 @@ export class MultiRange {
302298
* @param cmp The data to compare.
303299
* @return True if cmp is exactly the same as this instance.
304300
*/
305-
public equals(cmp: MultiRange | string): boolean
301+
public equals(cmp: Initializer): boolean
306302
{
307-
if (typeof cmp === 'string') {
308-
return this.equals(new MultiRange(cmp));
309-
} else {
303+
if (typeof cmp === 'undefined') {
304+
throw new TypeError('Invalid input');
305+
} else if (cmp instanceof MultiRange) {
310306
if (cmp === this) return true;
311307
if (this.ranges.length !== cmp.ranges.length) return false;
312308
for (let i = 0; i < this.ranges.length; i++) {
313309
if (this.ranges[i][0] !== cmp.ranges[i][0] || this.ranges[i][1] !== cmp.ranges[i][1])
314310
return false;
315311
}
316312
return true;
313+
} else {
314+
return this.equals(new MultiRange(cmp));
317315
}
318316
}
319317

0 commit comments

Comments
 (0)