@teselagen/range-utils 0.1.21 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +6542 -18010
- package/index.mjs +6593 -18007
- package/index.umd.js +6836 -18306
- package/package.json +1 -1
- package/src/adjustRangeToDeletionOfAnotherRange.js +53 -0
- package/src/adjustRangeToDeletionOfAnotherRange.test.js +107 -0
- package/src/adjustRangeToInsert.js +28 -0
- package/src/adjustRangeToInsert.test.js +111 -0
- package/src/adjustRangeToRotation.js +24 -0
- package/src/adjustRangeToRotation.test.js +123 -0
- package/src/checkIfNonCircularRangesOverlap.js +30 -0
- package/src/checkIfNonCircularRangesOverlap.test.js +45 -0
- package/src/checkIfPotentiallyCircularRangesOverlap.js +16 -0
- package/src/checkIfPotentiallyCircularRangesOverlap.test.js +73 -0
- package/src/collapseOverlapsGeneratedFromRangeComparisonIfPossible.js +47 -0
- package/src/collapseOverlapsGeneratedFromRangeComparisonIfPossible.test.js +124 -0
- package/src/convertRangeIndices.js +22 -0
- package/src/convertRangeIndices.test.js +22 -0
- package/src/convertRangeTo0Based.js +5 -0
- package/src/convertRangeTo1Based.js +5 -0
- package/src/doesRangeSpanEntireSequence.js +7 -0
- package/src/doesRangeSpanOrigin.js +3 -0
- package/src/expandOrContractCircularRangeToPosition.js +41 -0
- package/src/expandOrContractNonCircularRangeToPosition.js +25 -0
- package/src/expandOrContractRangeByLength.js +12 -0
- package/src/expandOrContractRangeByLength.test.js +77 -0
- package/src/expandOrContractRangeToPosition.js +10 -0
- package/src/flipContainedRange.js +85 -0
- package/src/flipContainedRange.test.js +124 -0
- package/src/generateRandomRange.js +20 -0
- package/src/generateRandomRange.test.js +24 -0
- package/src/getAnnotationRangeType.js +24 -0
- package/src/getAnnotationRangeType.test.js +59 -0
- package/src/getEachPositionInRangeAsArray.js +15 -0
- package/src/getEachPositionInRangeAsArray.test.js +9 -0
- package/src/getLengthOfOverlappingRegionsBetweenTwoRanges.js +9 -0
- package/src/getLengthOfOverlappingRegionsBetweenTwoRanges.test.js +24 -0
- package/src/getMiddleOfRange.js +10 -0
- package/src/getMiddleOfRange.test.js +44 -0
- package/src/getOverlapOfNonCircularRanges.js +35 -0
- package/src/getOverlapsOfPotentiallyCircularRanges.js +54 -0
- package/src/getOverlapsOfPotentiallyCircularRanges.test.js +199 -0
- package/src/getPositionFromAngle.js +7 -0
- package/src/getRangeAngles.js +33 -0
- package/src/getRangeAngles.test.js +77 -0
- package/src/getRangeLength.js +14 -0
- package/src/getRangeLength.test.js +30 -0
- package/src/getRangesBetweenTwoRanges.js +28 -0
- package/src/getSequenceWithinRange.js +17 -0
- package/src/getSequenceWithinRange.test.js +49 -0
- package/src/getShortestDistanceBetweenTwoPositions.js +12 -0
- package/src/getShortestDistanceBetweenTwoPositions.test.js +14 -0
- package/src/getYOffsetForPotentiallyCircularRange.js +24 -0
- package/src/getYOffsetsForPotentiallyCircularRanges.js +20 -0
- package/src/getYOffsetsForPotentiallyCircularRanges.test.js +29 -0
- package/src/getZeroedRangeOverlaps.js +17 -0
- package/src/getZeroedRangeOverlaps.test.js +36 -0
- package/src/index.js +51 -0
- package/src/index.test.js +33 -0
- package/src/invertRange.js +21 -0
- package/src/invertRange.test.js +96 -0
- package/src/isPositionCloserToRangeStartThanRangeEnd.js +8 -0
- package/src/isPositionCloserToRangeStartThanRangeEnd.test.js +17 -0
- package/src/isPositionWithinRange.js +31 -0
- package/src/isRangeOrPositionWithinRange.js +29 -0
- package/src/isRangeOrPositionWithinRange.test.js +150 -0
- package/src/isRangeWithinRange.js +17 -0
- package/src/loopEachPositionInRange.js +5 -0
- package/src/modulatePositionByRange.js +10 -0
- package/src/modulatePositionByRange.test.js +12 -0
- package/src/modulateRangeBySequenceLength.js +11 -0
- package/src/modulateRangeBySequenceLength.test.js +16 -0
- package/src/normalizePositionByRangeLength.js +20 -0
- package/src/normalizePositionByRangeLength.test.js +23 -0
- package/src/normalizePositionByRangeLength1Based.js +5 -0
- package/src/normalizePositionByRangeLength1Based.test.js +9 -0
- package/src/normalizeRange.js +11 -0
- package/src/normalizeRange.test.js +9 -0
- package/src/provideInclusiveOptions.js +26 -0
- package/src/reversePositionInRange.js +13 -0
- package/src/splitRangeIntoTwoPartsIfItIsCircular.js +31 -0
- package/src/splitRangeIntoTwoPartsIfItIsCircular.test.js +33 -0
- package/src/translateRange.js +9 -0
- package/src/translateRange.test.js +20 -0
- package/src/trimNonCicularRangeByAnotherNonCircularRange.js +57 -0
- package/src/trimNumberToFitWithin0ToAnotherNumber.js +12 -0
- package/src/trimRangeByAnotherRange.js +102 -0
- package/src/trimRangeByAnotherRange.test.js +314 -0
- package/src/zeroSubrangeByContainerRange.js +36 -0
- package/src/zeroSubrangeByContainerRange.test.js +51 -0
@@ -0,0 +1,314 @@
|
|
1
|
+
import trimRangeByAnotherRange from "./trimRangeByAnotherRange.js";
|
2
|
+
import assert from "assert";
|
3
|
+
|
4
|
+
describe("trimRangeByAnotherRange", function() {
|
5
|
+
it(`returns nothing if invalid inputs are sent in`, () => {
|
6
|
+
assert.equal(
|
7
|
+
null,
|
8
|
+
trimRangeByAnotherRange(
|
9
|
+
null,
|
10
|
+
{
|
11
|
+
start: 2,
|
12
|
+
end: 2
|
13
|
+
},
|
14
|
+
10
|
15
|
+
)
|
16
|
+
);
|
17
|
+
});
|
18
|
+
it("trims non circular ranges that dont fully overlap", function() {
|
19
|
+
assert.deepEqual(
|
20
|
+
trimRangeByAnotherRange(
|
21
|
+
{
|
22
|
+
start: 0,
|
23
|
+
end: 2,
|
24
|
+
name: "happy"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
start: 2,
|
28
|
+
end: 2
|
29
|
+
},
|
30
|
+
10
|
31
|
+
),
|
32
|
+
{
|
33
|
+
start: 0,
|
34
|
+
end: 1,
|
35
|
+
name: "happy"
|
36
|
+
}
|
37
|
+
);
|
38
|
+
});
|
39
|
+
it("it does not trim non circular ranges with overlap contained within it", function() {
|
40
|
+
assert.deepEqual(
|
41
|
+
trimRangeByAnotherRange(
|
42
|
+
{
|
43
|
+
start: 0,
|
44
|
+
end: 10,
|
45
|
+
name: "happy"
|
46
|
+
},
|
47
|
+
{
|
48
|
+
start: 2,
|
49
|
+
end: 2
|
50
|
+
},
|
51
|
+
10
|
52
|
+
),
|
53
|
+
{
|
54
|
+
start: 0,
|
55
|
+
end: 10,
|
56
|
+
name: "happy"
|
57
|
+
}
|
58
|
+
);
|
59
|
+
});
|
60
|
+
it("trims non circular ranges that fully overlap", function() {
|
61
|
+
assert.deepEqual(
|
62
|
+
trimRangeByAnotherRange(
|
63
|
+
{
|
64
|
+
start: 0,
|
65
|
+
end: 2
|
66
|
+
},
|
67
|
+
{
|
68
|
+
start: 0,
|
69
|
+
end: 2
|
70
|
+
},
|
71
|
+
10
|
72
|
+
),
|
73
|
+
undefined
|
74
|
+
);
|
75
|
+
assert.deepEqual(
|
76
|
+
trimRangeByAnotherRange(
|
77
|
+
{
|
78
|
+
start: 3,
|
79
|
+
end: 5
|
80
|
+
},
|
81
|
+
{
|
82
|
+
start: 3,
|
83
|
+
end: 5
|
84
|
+
},
|
85
|
+
10
|
86
|
+
),
|
87
|
+
undefined
|
88
|
+
);
|
89
|
+
assert.deepEqual(
|
90
|
+
trimRangeByAnotherRange(
|
91
|
+
{
|
92
|
+
start: 3,
|
93
|
+
end: 3
|
94
|
+
},
|
95
|
+
{
|
96
|
+
start: 3,
|
97
|
+
end: 3
|
98
|
+
},
|
99
|
+
10
|
100
|
+
),
|
101
|
+
undefined
|
102
|
+
);
|
103
|
+
assert.deepEqual(
|
104
|
+
trimRangeByAnotherRange(
|
105
|
+
{
|
106
|
+
start: 0,
|
107
|
+
end: 0
|
108
|
+
},
|
109
|
+
{
|
110
|
+
start: 0,
|
111
|
+
end: 3
|
112
|
+
},
|
113
|
+
10
|
114
|
+
),
|
115
|
+
undefined
|
116
|
+
);
|
117
|
+
});
|
118
|
+
it("does not trim non circular ranges that dont overlap", function() {
|
119
|
+
assert.deepEqual(
|
120
|
+
trimRangeByAnotherRange(
|
121
|
+
{
|
122
|
+
start: 0,
|
123
|
+
end: 2
|
124
|
+
},
|
125
|
+
{
|
126
|
+
start: 5,
|
127
|
+
end: 6
|
128
|
+
},
|
129
|
+
10
|
130
|
+
),
|
131
|
+
{
|
132
|
+
start: 0,
|
133
|
+
end: 2
|
134
|
+
}
|
135
|
+
);
|
136
|
+
assert.deepEqual(
|
137
|
+
trimRangeByAnotherRange(
|
138
|
+
{
|
139
|
+
start: 3,
|
140
|
+
end: 5,
|
141
|
+
name: "happy"
|
142
|
+
},
|
143
|
+
{
|
144
|
+
start: 0,
|
145
|
+
end: 0
|
146
|
+
},
|
147
|
+
10
|
148
|
+
),
|
149
|
+
{
|
150
|
+
start: 3,
|
151
|
+
name: "happy",
|
152
|
+
end: 5
|
153
|
+
}
|
154
|
+
);
|
155
|
+
});
|
156
|
+
it("does trim circular ranges that overlap", function() {
|
157
|
+
assert.deepEqual(
|
158
|
+
trimRangeByAnotherRange(
|
159
|
+
{
|
160
|
+
start: 3,
|
161
|
+
name: "happy",
|
162
|
+
end: 2
|
163
|
+
},
|
164
|
+
{
|
165
|
+
start: 5,
|
166
|
+
end: 6
|
167
|
+
},
|
168
|
+
10
|
169
|
+
),
|
170
|
+
{
|
171
|
+
start: 3,
|
172
|
+
name: "happy",
|
173
|
+
end: 2
|
174
|
+
}
|
175
|
+
);
|
176
|
+
assert.deepEqual(
|
177
|
+
trimRangeByAnotherRange(
|
178
|
+
{
|
179
|
+
start: 3,
|
180
|
+
end: 2
|
181
|
+
},
|
182
|
+
{
|
183
|
+
start: 3,
|
184
|
+
end: 6
|
185
|
+
},
|
186
|
+
10
|
187
|
+
),
|
188
|
+
{
|
189
|
+
start: 7,
|
190
|
+
end: 2
|
191
|
+
}
|
192
|
+
);
|
193
|
+
assert.deepEqual(
|
194
|
+
trimRangeByAnotherRange(
|
195
|
+
{
|
196
|
+
start: 3,
|
197
|
+
end: 2
|
198
|
+
},
|
199
|
+
{
|
200
|
+
start: 1,
|
201
|
+
end: 6
|
202
|
+
},
|
203
|
+
10
|
204
|
+
),
|
205
|
+
{
|
206
|
+
start: 7,
|
207
|
+
end: 0
|
208
|
+
}
|
209
|
+
);
|
210
|
+
assert.deepEqual(
|
211
|
+
trimRangeByAnotherRange(
|
212
|
+
{
|
213
|
+
start: 3,
|
214
|
+
end: 2
|
215
|
+
},
|
216
|
+
{
|
217
|
+
start: 0,
|
218
|
+
end: 6
|
219
|
+
},
|
220
|
+
10
|
221
|
+
),
|
222
|
+
{
|
223
|
+
start: 7,
|
224
|
+
end: 9
|
225
|
+
}
|
226
|
+
);
|
227
|
+
assert.deepEqual(
|
228
|
+
trimRangeByAnotherRange(
|
229
|
+
{
|
230
|
+
start: 3,
|
231
|
+
end: 2
|
232
|
+
},
|
233
|
+
{
|
234
|
+
start: 2,
|
235
|
+
end: 9
|
236
|
+
},
|
237
|
+
10
|
238
|
+
),
|
239
|
+
{
|
240
|
+
start: 0,
|
241
|
+
end: 1
|
242
|
+
}
|
243
|
+
);
|
244
|
+
assert.deepEqual(
|
245
|
+
trimRangeByAnotherRange(
|
246
|
+
{
|
247
|
+
start: 3,
|
248
|
+
end: 2
|
249
|
+
},
|
250
|
+
// rrrrrrrrrr
|
251
|
+
// 0123456789
|
252
|
+
// --es------
|
253
|
+
// --e s-----
|
254
|
+
{
|
255
|
+
start: 4,
|
256
|
+
end: 2
|
257
|
+
},
|
258
|
+
10
|
259
|
+
),
|
260
|
+
{
|
261
|
+
start: 3,
|
262
|
+
end: 3
|
263
|
+
}
|
264
|
+
);
|
265
|
+
assert.deepEqual(
|
266
|
+
trimRangeByAnotherRange(
|
267
|
+
{
|
268
|
+
start: 1,
|
269
|
+
end: 2
|
270
|
+
},
|
271
|
+
{
|
272
|
+
start: 4,
|
273
|
+
end: 1
|
274
|
+
},
|
275
|
+
10
|
276
|
+
),
|
277
|
+
{
|
278
|
+
start: 2,
|
279
|
+
end: 2
|
280
|
+
}
|
281
|
+
);
|
282
|
+
assert.deepEqual(
|
283
|
+
trimRangeByAnotherRange(
|
284
|
+
{
|
285
|
+
start: 1,
|
286
|
+
end: 5
|
287
|
+
},
|
288
|
+
{
|
289
|
+
start: 4,
|
290
|
+
end: 1
|
291
|
+
},
|
292
|
+
10
|
293
|
+
),
|
294
|
+
{
|
295
|
+
start: 2,
|
296
|
+
end: 3
|
297
|
+
}
|
298
|
+
);
|
299
|
+
assert.deepEqual(
|
300
|
+
trimRangeByAnotherRange(
|
301
|
+
{
|
302
|
+
start: 3,
|
303
|
+
end: 2
|
304
|
+
},
|
305
|
+
{
|
306
|
+
start: 3,
|
307
|
+
end: 2
|
308
|
+
},
|
309
|
+
10
|
310
|
+
),
|
311
|
+
undefined
|
312
|
+
);
|
313
|
+
});
|
314
|
+
});
|
@@ -0,0 +1,36 @@
|
|
1
|
+
//
|
2
|
+
// ac.throw([ac.posInt, ac.posInt, ac.bool], arguments);
|
3
|
+
import trimRangeByAnotherRange from './trimRangeByAnotherRange';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* "zeroes" a subrange of a container range by
|
7
|
+
* adjusting subRange start and end such that it is as if the container range start = 0.
|
8
|
+
* @param {object} subRange {start:
|
9
|
+
* end:
|
10
|
+
* }
|
11
|
+
* @param {object} containerRange {start:
|
12
|
+
* end:
|
13
|
+
* }
|
14
|
+
* @param {int} sequenceLength
|
15
|
+
* @return {object} {start:
|
16
|
+
* end:
|
17
|
+
* }
|
18
|
+
*/
|
19
|
+
export default function zeroSubrangeByContainerRange(subRange, containerRange, sequenceLength) {
|
20
|
+
// ac.throw([ac.range, ac.range, ac.posInt], arguments);
|
21
|
+
//first check to make sure the container range fully contains the subRange
|
22
|
+
const trimmedSubRange = trimRangeByAnotherRange(subRange, containerRange, sequenceLength);
|
23
|
+
if (trimmedSubRange) {
|
24
|
+
throw new Error('subRange must be fully contained by containerRange! Otherwise this function does not make sense');
|
25
|
+
}
|
26
|
+
const newSubrange = {};
|
27
|
+
newSubrange.start = subRange.start - containerRange.start;
|
28
|
+
newSubrange.end = subRange.end - containerRange.start;
|
29
|
+
if (newSubrange.start < 0) {
|
30
|
+
newSubrange.start += sequenceLength;
|
31
|
+
}
|
32
|
+
if (newSubrange.end < 0) {
|
33
|
+
newSubrange.end += sequenceLength;
|
34
|
+
}
|
35
|
+
return newSubrange;
|
36
|
+
};
|
@@ -0,0 +1,51 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
import {expect} from 'chai';
|
4
|
+
|
5
|
+
import zeroSubrangeByContainerRange from './zeroSubrangeByContainerRange.js';
|
6
|
+
describe('zeroSubrangeByContainerRange', function() {
|
7
|
+
it('throws an error if circular subRange does not fit within container range', function() {
|
8
|
+
let error = false;
|
9
|
+
try {
|
10
|
+
zeroSubrangeByContainerRange({
|
11
|
+
start: 20,
|
12
|
+
end: 10
|
13
|
+
}, {
|
14
|
+
start: 10,
|
15
|
+
end: 20
|
16
|
+
}, 30);
|
17
|
+
} catch (e) {
|
18
|
+
error = true;
|
19
|
+
}
|
20
|
+
expect(error).to.be.true;
|
21
|
+
});
|
22
|
+
it('throws an error if non circular subRange does not fit within container range', function() {
|
23
|
+
let error = false;
|
24
|
+
try {
|
25
|
+
zeroSubrangeByContainerRange({
|
26
|
+
start: 9,
|
27
|
+
end: 20
|
28
|
+
}, {
|
29
|
+
start: 10,
|
30
|
+
end: 20
|
31
|
+
}, 30);
|
32
|
+
} catch (e) {
|
33
|
+
error = true;
|
34
|
+
}
|
35
|
+
expect(error).to.be.true;
|
36
|
+
});
|
37
|
+
it('zeros non circular range if fully overlapped', function() {
|
38
|
+
const zeroedSubrange = zeroSubrangeByContainerRange({
|
39
|
+
start: 10,
|
40
|
+
end: 20
|
41
|
+
}, {
|
42
|
+
start: 10,
|
43
|
+
end: 20
|
44
|
+
}, 30);
|
45
|
+
|
46
|
+
expect(zeroedSubrange).to.deep.equal({
|
47
|
+
start: 0,
|
48
|
+
end: 10
|
49
|
+
});
|
50
|
+
});
|
51
|
+
});
|