-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultiRange.ts
More file actions
204 lines (181 loc) · 5.52 KB
/
MultiRange.ts
File metadata and controls
204 lines (181 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as mr from './fp.js';
export type Initializer = string | number | (number | mr.Range)[] | MultiRange;
/**
* Parses and manipulates multiple integer ranges.
* This class exists for compatibility purposes.
* Prefer the function style API instead.
*/
export class MultiRange {
private ranges: mr.MultiIntegerRange;
private options: mr.Options;
/**
* Creates a new MultiRange object.
*/
constructor(data?: Initializer, options?: mr.Options) {
this.ranges = [];
this.options = {
parseNegative: !!(options || {}).parseNegative,
parseUnbounded: !!(options || {}).parseUnbounded
};
if (typeof data === 'string') {
this.ranges = mr.parse(data, options);
} else if (typeof data === 'number' || Array.isArray(data)) {
this.ranges = mr.normalize(data);
} else if (data instanceof MultiRange) {
this.ranges = data.ranges;
if (options === undefined) this.options = data.options;
} else if (data !== undefined) {
throw new TypeError('Invalid input');
}
}
/**
* Clones this instance.
* @returns The cloned instance.
*/
public clone(): MultiRange {
return new MultiRange(this);
}
/**
* Appends to this instance.
* @param value - The data to append.
*/
public append(value: Initializer): MultiRange {
this.ranges = mr.append(
this.ranges,
new MultiRange(value, this.options).ranges
);
return this;
}
/**
* Subtracts from this instance.
* @param value - The data to subtract.
*/
public subtract(value: Initializer): MultiRange {
this.ranges = mr.subtract(
this.ranges,
new MultiRange(value, this.options).ranges
);
return this;
}
/**
* Remove integers which are not included in `value`,
* yielding the intersection of this and `value`.
* @param value - The data to calculate the intersetion.
*/
public intersect(value: Initializer): MultiRange {
this.ranges = mr.intersect(
this.ranges,
new MultiRange(value, this.options).ranges
);
return this;
}
/**
* Exports the whole range data as an array of arrays.
* @returns An copied array of range segments.
*/
public getRanges(): number[][] {
const result: number[][] = [];
for (let r of this.ranges) result.push([r[0], r[1]]);
return result;
}
/**
* Checks if this instance contains the specified value.
* @param value - Value to be checked.
* @returns True if the specified value is included in the instance.
*/
public has(value: Initializer): boolean {
if (value === undefined) throw new TypeError('Invalid input');
return mr.has(this.ranges, new MultiRange(value, this.options).ranges);
}
/**
* Returns the number of range segments.
* For example, the segmentLength of `2-5,7,9-11` is 3.
* @returns The number of segments. Returns 0 for an empty instance.
*/
public segmentLength(): number {
return this.ranges.length;
}
/**
* Calculates how many numbers are effectively included in this instance.
* For example, the length of `1-10,51-60,90` is 21.
* @returns The number of integer values in this instance.
* Returns `Infinity` for an unbounded range.
*/
public length(): number {
return mr.length(this.ranges);
}
/**
* Checks if two instances of MultiRange are identical.
* @param cmp - The data to compare.
* @returns True if `cmp` is exactly the same as this instance.
*/
public equals(cmp: Initializer): boolean {
if (cmp === undefined) throw new TypeError('Invalid input');
return mr.equals(this.ranges, new MultiRange(cmp, this.options).ranges);
}
/**
* Checks if the current instance is unbounded (i.e., infinite).
*/
public isUnbounded(): boolean {
return mr.isUnbounded(this.ranges);
}
/**
* Returns the minimum integer contained in this insntance.
* Can be -Infinity or undefined.
* @returns The minimum integer of this instance.
*/
public min(): number | undefined {
return mr.min(this.ranges);
}
/**
* Returns the maximum number contained in this insntance.
* Can be Infinity or undefined.
* @returns The maximum integer of this instance.
*/
public max(): number | undefined {
return mr.max(this.ranges);
}
/**
* Removes the smallest integer from this instance and returns it.
* @returns The minimum integer removed from this instance.
*/
public shift(): number | undefined {
const min = this.min();
this.ranges = mr.tail(this.ranges);
return min;
}
/**
* Removes the largest integer from this instance and returns it.
* @returns The maximum integer removed from this instance.
*/
public pop(): number | undefined {
const max = this.max();
this.ranges = mr.init(this.ranges);
return max;
}
/**
* Returns the string respresentation of this MultiRange.
*/
public toString(): string {
return mr.stringify(this.ranges);
}
/**
* Builds a flat array of integers which holds all elements in this instance.
* Note that this may be slow and memory-consuming for large ranges.
* Consider using the iterator whenever possible.
*/
public toArray(): number[] {
return mr.flatten(this.ranges);
}
/**
* Returns an ES6-compatible iterator.
*/
public getIterator(): { next: () => { done?: boolean; value?: number } } {
return mr.iterate(this.ranges)[Symbol.iterator]();
}
public [Symbol.iterator]() {
return mr.iterate(this.ranges)[Symbol.iterator]();
}
}
export const multirange = (data?: Initializer, options?: mr.Options) =>
new MultiRange(data, options);