@@ -70,16 +70,14 @@ export class MultiRange {
70
70
/**
71
71
* Appends a range to this instance.
72
72
*/
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' ) ;
76
76
} else if ( value instanceof MultiRange ) {
77
77
for ( let r of value . ranges ) this . appendRange ( r [ 0 ] , r [ 1 ] ) ;
78
78
return this ;
79
- } else if ( typeof value === 'string' ) {
79
+ } else {
80
80
return this . append ( new MultiRange ( value ) ) ;
81
- } else if ( typeof value !== 'undefined' ) {
82
- throw new TypeError ( 'Invalid input' ) ;
83
81
}
84
82
}
85
83
@@ -101,16 +99,14 @@ export class MultiRange {
101
99
/**
102
100
* Subtracts a range from this instance.
103
101
*/
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' ) ;
107
105
} else if ( value instanceof MultiRange ) {
108
106
for ( let r of value . ranges ) this . subtractRange ( r [ 0 ] , r [ 1 ] ) ;
109
107
return this ;
110
- } else if ( typeof value === 'string' ) {
108
+ } else {
111
109
return this . subtract ( new MultiRange ( value ) ) ;
112
- } else if ( typeof value !== 'undefined' ) {
113
- throw new TypeError ( 'Invalid input' ) ;
114
110
}
115
111
}
116
112
@@ -144,10 +140,10 @@ export class MultiRange {
144
140
* Note that this modifies the original object
145
141
* rather than returning the new MultiRange object.
146
142
*/
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 ) {
151
147
let result = new MultiRange ( ) ;
152
148
let that = new MultiRange ( value ) ;
153
149
let jstart = 0 ; // used for optimization
@@ -165,6 +161,8 @@ export class MultiRange {
165
161
}
166
162
this . ranges = result . ranges ;
167
163
return this ;
164
+ } else {
165
+ return this . intersect ( new MultiRange ( value ) ) ;
168
166
}
169
167
}
170
168
@@ -248,12 +246,10 @@ export class MultiRange {
248
246
* @param value Value to be checked
249
247
* @return True if the specified value is included in the range.
250
248
*/
251
- public has ( value : number | string | ( number | Range ) [ ] | MultiRange ) : boolean
249
+ public has ( value : Initializer ) : boolean
252
250
{
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' ) ;
257
253
} else if ( value instanceof MultiRange ) {
258
254
let s = 0 ;
259
255
let len = this . ranges . length ;
@@ -267,7 +263,7 @@ export class MultiRange {
267
263
}
268
264
return true ;
269
265
} else {
270
- throw new TypeError ( 'Invalid input' ) ;
266
+ return this . has ( new MultiRange ( value ) ) ;
271
267
}
272
268
}
273
269
@@ -302,18 +298,20 @@ export class MultiRange {
302
298
* @param cmp The data to compare.
303
299
* @return True if cmp is exactly the same as this instance.
304
300
*/
305
- public equals ( cmp : MultiRange | string ) : boolean
301
+ public equals ( cmp : Initializer ) : boolean
306
302
{
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 ) {
310
306
if ( cmp === this ) return true ;
311
307
if ( this . ranges . length !== cmp . ranges . length ) return false ;
312
308
for ( let i = 0 ; i < this . ranges . length ; i ++ ) {
313
309
if ( this . ranges [ i ] [ 0 ] !== cmp . ranges [ i ] [ 0 ] || this . ranges [ i ] [ 1 ] !== cmp . ranges [ i ] [ 1 ] )
314
310
return false ;
315
311
}
316
312
return true ;
313
+ } else {
314
+ return this . equals ( new MultiRange ( cmp ) ) ;
317
315
}
318
316
}
319
317
0 commit comments