@teselagen/range-utils 0.3.7 → 0.3.8
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/README.md +94 -0
- package/flipContainedRange.d.ts +1 -1
- package/index.js +233 -85
- package/index.mjs +233 -85
- package/index.umd.js +233 -85
- package/package.json +1 -2
- package/src/adjustRangeToDeletionOfAnotherRange.test.js +166 -105
- package/src/adjustRangeToInsert.js +29 -26
- package/src/adjustRangeToInsert.test.js +197 -109
- package/src/adjustRangeToRotation.js +4 -5
- package/src/adjustRangeToRotation.test.js +203 -119
- package/src/checkIfNonCircularRangesOverlap.js +18 -15
- package/src/checkIfNonCircularRangesOverlap.test.js +67 -42
- package/src/checkIfPotentiallyCircularRangesOverlap.js +22 -12
- package/src/checkIfPotentiallyCircularRangesOverlap.test.js +115 -70
- package/src/collapseOverlapsGeneratedFromRangeComparisonIfPossible.js +72 -41
- package/src/collapseOverlapsGeneratedFromRangeComparisonIfPossible.test.js +183 -84
- package/src/convertRangeIndices.js +24 -20
- package/src/convertRangeIndices.test.js +40 -21
- package/src/convertRangeTo0Based.js +7 -4
- package/src/convertRangeTo1Based.js +8 -4
- package/src/doesRangeSpanEntireSequence.js +4 -4
- package/src/doesRangeSpanOrigin.js +3 -3
- package/src/expandOrContractCircularRangeToPosition.js +45 -38
- package/src/expandOrContractNonCircularRangeToPosition.js +24 -21
- package/src/expandOrContractRangeByLength.js +16 -11
- package/src/expandOrContractRangeByLength.test.js +109 -71
- package/src/expandOrContractRangeToPosition.js +17 -9
- package/src/flipContainedRange.js +133 -72
- package/src/flipContainedRange.test.js +143 -117
- package/src/generateRandomRange.js +17 -15
- package/src/generateRandomRange.test.js +21 -21
- package/src/getAnnotationRangeType.js +25 -21
- package/src/getAnnotationRangeType.test.js +56 -57
- package/src/getEachPositionInRangeAsArray.js +13 -13
- package/src/getEachPositionInRangeAsArray.test.js +12 -8
- package/src/getLengthOfOverlappingRegionsBetweenTwoRanges.js +16 -8
- package/src/getLengthOfOverlappingRegionsBetweenTwoRanges.test.js +32 -24
- package/src/getMiddleOfRange.js +1 -1
- package/src/getMiddleOfRange.test.js +5 -13
- package/src/getOverlapOfNonCircularRanges.js +30 -30
- package/src/getOverlapsOfPotentiallyCircularRanges.js +5 -5
- package/src/getOverlapsOfPotentiallyCircularRanges.test.js +3 -23
- package/src/getPositionFromAngle.js +12 -7
- package/src/getRangeAngles.js +1 -1
- package/src/getRangeAngles.test.js +10 -11
- package/src/getRangeLength.test.js +5 -5
- package/src/getRangesBetweenTwoRanges.js +31 -22
- package/src/getSequenceWithinRange.js +13 -13
- package/src/getSequenceWithinRange.test.js +43 -45
- package/src/getShortestDistanceBetweenTwoPositions.js +10 -6
- package/src/getShortestDistanceBetweenTwoPositions.test.js +11 -13
- package/src/getYOffsetForPotentiallyCircularRange.js +33 -21
- package/src/getYOffsetsForPotentiallyCircularRanges.js +26 -19
- package/src/getYOffsetsForPotentiallyCircularRanges.test.js +42 -29
- package/src/getZeroedRangeOverlaps.js +30 -15
- package/src/getZeroedRangeOverlaps.test.js +75 -36
- package/src/index.js +51 -51
- package/src/index.test.js +6 -8
- package/src/invertRange.test.js +103 -93
- package/src/isPositionCloserToRangeStartThanRangeEnd.js +18 -6
- package/src/isPositionCloserToRangeStartThanRangeEnd.test.js +36 -16
- package/src/isPositionWithinRange.js +4 -4
- package/src/isRangeOrPositionWithinRange.js +18 -12
- package/src/isRangeOrPositionWithinRange.test.js +6 -6
- package/src/isRangeWithinRange.js +2 -3
- package/src/loopEachPositionInRange.js +3 -3
- package/src/modulatePositionByRange.js +8 -8
- package/src/modulatePositionByRange.test.js +10 -11
- package/src/modulateRangeBySequenceLength.js +7 -7
- package/src/modulateRangeBySequenceLength.test.js +39 -16
- package/src/normalizePositionByRangeLength.js +22 -18
- package/src/normalizePositionByRangeLength.test.js +23 -23
- package/src/normalizePositionByRangeLength1Based.js +7 -4
- package/src/normalizePositionByRangeLength1Based.test.js +9 -9
- package/src/normalizeRange.js +7 -7
- package/src/normalizeRange.test.js +9 -9
- package/src/provideInclusiveOptions.js +36 -23
- package/src/reversePositionInRange.js +16 -12
- package/src/splitRangeIntoTwoPartsIfItIsCircular.js +31 -28
- package/src/splitRangeIntoTwoPartsIfItIsCircular.test.js +22 -11
- package/src/translateRange.js +18 -8
- package/src/translateRange.test.js +18 -19
- package/src/trimNonCicularRangeByAnotherNonCircularRange.js +45 -42
- package/src/trimNumberToFitWithin0ToAnotherNumber.js +13 -10
- package/src/trimRangeByAnotherRange.js +20 -19
- package/src/trimRangeByAnotherRange.test.js +6 -6
- package/src/zeroSubrangeByContainerRange.js +29 -19
- package/src/zeroSubrangeByContainerRange.test.js +57 -47
@@ -1,7 +1,7 @@
|
|
1
1
|
import getOverlapsOfPotentiallyCircularRanges from "./getOverlapsOfPotentiallyCircularRanges";
|
2
2
|
import splitRangeIntoTwoPartsIfItIsCircular from "./splitRangeIntoTwoPartsIfItIsCircular";
|
3
3
|
import trimNonCicularRangeByAnotherNonCircularRange from "./trimNonCicularRangeByAnotherNonCircularRange";
|
4
|
-
import {extend} from "lodash";
|
4
|
+
import { extend } from "lodash";
|
5
5
|
|
6
6
|
/**
|
7
7
|
* trims range, but does *not* adjust it
|
@@ -31,7 +31,7 @@ export default function trimRangeByAnotherRange(
|
|
31
31
|
rangeToBeTrimmed.start,
|
32
32
|
rangeToBeTrimmed.end,
|
33
33
|
trimmingRange.start,
|
34
|
-
trimmingRange.end
|
34
|
+
trimmingRange.end
|
35
35
|
]) {
|
36
36
|
if (position < 0 || (!position && position !== 0)) {
|
37
37
|
console.warn("invalid range input");
|
@@ -54,26 +54,27 @@ export default function trimRangeByAnotherRange(
|
|
54
54
|
rangeToBeTrimmed,
|
55
55
|
sequenceLength
|
56
56
|
);
|
57
|
-
splitRangesToBeTrimmed.forEach(function(nonCircularRangeToBeTrimmed, index) {
|
58
|
-
overlaps.forEach(function(overlap) {
|
57
|
+
splitRangesToBeTrimmed.forEach(function (nonCircularRangeToBeTrimmed, index) {
|
58
|
+
overlaps.forEach(function (overlap) {
|
59
59
|
if (nonCircularRangeToBeTrimmed) {
|
60
|
-
nonCircularRangeToBeTrimmed =
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
nonCircularRangeToBeTrimmed =
|
61
|
+
trimNonCicularRangeByAnotherNonCircularRange(
|
62
|
+
nonCircularRangeToBeTrimmed,
|
63
|
+
overlap
|
64
|
+
);
|
64
65
|
}
|
65
66
|
});
|
66
67
|
splitRangesToBeTrimmed[index] = nonCircularRangeToBeTrimmed;
|
67
68
|
});
|
68
69
|
//filter out any of the split ranges that have been fully deleted!
|
69
|
-
const outputSplitRanges = splitRangesToBeTrimmed.filter(
|
70
|
-
trimmedRange
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
const outputSplitRanges = splitRangesToBeTrimmed.filter(
|
71
|
+
function (trimmedRange) {
|
72
|
+
if (trimmedRange) {
|
73
|
+
return true;
|
74
|
+
}
|
75
|
+
return false;
|
74
76
|
}
|
75
|
-
|
76
|
-
});
|
77
|
+
);
|
77
78
|
|
78
79
|
let outputTrimmedRange;
|
79
80
|
if (outputSplitRanges.length < 0) {
|
@@ -84,19 +85,19 @@ export default function trimRangeByAnotherRange(
|
|
84
85
|
if (outputSplitRanges[0].start < outputSplitRanges[1].start) {
|
85
86
|
outputTrimmedRange = {
|
86
87
|
start: outputSplitRanges[1].start,
|
87
|
-
end: outputSplitRanges[0].end
|
88
|
+
end: outputSplitRanges[0].end
|
88
89
|
};
|
89
90
|
} else {
|
90
91
|
outputTrimmedRange = {
|
91
92
|
start: outputSplitRanges[0].start,
|
92
|
-
end: outputSplitRanges[1].end
|
93
|
+
end: outputSplitRanges[1].end
|
93
94
|
};
|
94
95
|
}
|
95
96
|
}
|
96
97
|
if (outputTrimmedRange) {
|
97
98
|
return extend({}, rangeToBeTrimmed, {
|
98
99
|
start: outputTrimmedRange.start,
|
99
|
-
end: outputTrimmedRange.end
|
100
|
+
end: outputTrimmedRange.end
|
100
101
|
});
|
101
102
|
}
|
102
|
-
}
|
103
|
+
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import trimRangeByAnotherRange from "./trimRangeByAnotherRange.js";
|
2
2
|
import assert from "assert";
|
3
3
|
|
4
|
-
describe("trimRangeByAnotherRange", function() {
|
4
|
+
describe("trimRangeByAnotherRange", function () {
|
5
5
|
it(`returns nothing if invalid inputs are sent in`, () => {
|
6
6
|
assert.equal(
|
7
7
|
null,
|
@@ -15,7 +15,7 @@ describe("trimRangeByAnotherRange", function() {
|
|
15
15
|
)
|
16
16
|
);
|
17
17
|
});
|
18
|
-
it("trims non circular ranges that dont fully overlap", function() {
|
18
|
+
it("trims non circular ranges that dont fully overlap", function () {
|
19
19
|
assert.deepEqual(
|
20
20
|
trimRangeByAnotherRange(
|
21
21
|
{
|
@@ -36,7 +36,7 @@ describe("trimRangeByAnotherRange", function() {
|
|
36
36
|
}
|
37
37
|
);
|
38
38
|
});
|
39
|
-
it("it does not trim non circular ranges with overlap contained within it", function() {
|
39
|
+
it("it does not trim non circular ranges with overlap contained within it", function () {
|
40
40
|
assert.deepEqual(
|
41
41
|
trimRangeByAnotherRange(
|
42
42
|
{
|
@@ -57,7 +57,7 @@ describe("trimRangeByAnotherRange", function() {
|
|
57
57
|
}
|
58
58
|
);
|
59
59
|
});
|
60
|
-
it("trims non circular ranges that fully overlap", function() {
|
60
|
+
it("trims non circular ranges that fully overlap", function () {
|
61
61
|
assert.deepEqual(
|
62
62
|
trimRangeByAnotherRange(
|
63
63
|
{
|
@@ -115,7 +115,7 @@ describe("trimRangeByAnotherRange", function() {
|
|
115
115
|
undefined
|
116
116
|
);
|
117
117
|
});
|
118
|
-
it("does not trim non circular ranges that dont overlap", function() {
|
118
|
+
it("does not trim non circular ranges that dont overlap", function () {
|
119
119
|
assert.deepEqual(
|
120
120
|
trimRangeByAnotherRange(
|
121
121
|
{
|
@@ -153,7 +153,7 @@ describe("trimRangeByAnotherRange", function() {
|
|
153
153
|
}
|
154
154
|
);
|
155
155
|
});
|
156
|
-
it("does trim circular ranges that overlap", function() {
|
156
|
+
it("does trim circular ranges that overlap", function () {
|
157
157
|
assert.deepEqual(
|
158
158
|
trimRangeByAnotherRange(
|
159
159
|
{
|
@@ -1,6 +1,6 @@
|
|
1
1
|
//
|
2
2
|
// ac.throw([ac.posInt, ac.posInt, ac.bool], arguments);
|
3
|
-
import trimRangeByAnotherRange from
|
3
|
+
import trimRangeByAnotherRange from "./trimRangeByAnotherRange";
|
4
4
|
|
5
5
|
/**
|
6
6
|
* "zeroes" a subrange of a container range by
|
@@ -16,21 +16,31 @@ import trimRangeByAnotherRange from './trimRangeByAnotherRange';
|
|
16
16
|
* end:
|
17
17
|
* }
|
18
18
|
*/
|
19
|
-
export default function zeroSubrangeByContainerRange(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
};
|
19
|
+
export default function zeroSubrangeByContainerRange(
|
20
|
+
subRange,
|
21
|
+
containerRange,
|
22
|
+
sequenceLength
|
23
|
+
) {
|
24
|
+
// ac.throw([ac.range, ac.range, ac.posInt], arguments);
|
25
|
+
//first check to make sure the container range fully contains the subRange
|
26
|
+
const trimmedSubRange = trimRangeByAnotherRange(
|
27
|
+
subRange,
|
28
|
+
containerRange,
|
29
|
+
sequenceLength
|
30
|
+
);
|
31
|
+
if (trimmedSubRange) {
|
32
|
+
throw new Error(
|
33
|
+
"subRange must be fully contained by containerRange! Otherwise this function does not make sense"
|
34
|
+
);
|
35
|
+
}
|
36
|
+
const newSubrange = {};
|
37
|
+
newSubrange.start = subRange.start - containerRange.start;
|
38
|
+
newSubrange.end = subRange.end - containerRange.start;
|
39
|
+
if (newSubrange.start < 0) {
|
40
|
+
newSubrange.start += sequenceLength;
|
41
|
+
}
|
42
|
+
if (newSubrange.end < 0) {
|
43
|
+
newSubrange.end += sequenceLength;
|
44
|
+
}
|
45
|
+
return newSubrange;
|
46
|
+
}
|
@@ -1,51 +1,61 @@
|
|
1
|
+
import { expect } from "chai";
|
1
2
|
|
3
|
+
import zeroSubrangeByContainerRange from "./zeroSubrangeByContainerRange.js";
|
4
|
+
describe("zeroSubrangeByContainerRange", function () {
|
5
|
+
it("throws an error if circular subRange does not fit within container range", function () {
|
6
|
+
let error = false;
|
7
|
+
try {
|
8
|
+
zeroSubrangeByContainerRange(
|
9
|
+
{
|
10
|
+
start: 20,
|
11
|
+
end: 10
|
12
|
+
},
|
13
|
+
{
|
14
|
+
start: 10,
|
15
|
+
end: 20
|
16
|
+
},
|
17
|
+
30
|
18
|
+
);
|
19
|
+
} catch (e) {
|
20
|
+
error = true;
|
21
|
+
}
|
22
|
+
expect(error).to.be.true;
|
23
|
+
});
|
24
|
+
it("throws an error if non circular subRange does not fit within container range", function () {
|
25
|
+
let error = false;
|
26
|
+
try {
|
27
|
+
zeroSubrangeByContainerRange(
|
28
|
+
{
|
29
|
+
start: 9,
|
30
|
+
end: 20
|
31
|
+
},
|
32
|
+
{
|
33
|
+
start: 10,
|
34
|
+
end: 20
|
35
|
+
},
|
36
|
+
30
|
37
|
+
);
|
38
|
+
} catch (e) {
|
39
|
+
error = true;
|
40
|
+
}
|
41
|
+
expect(error).to.be.true;
|
42
|
+
});
|
43
|
+
it("zeros non circular range if fully overlapped", function () {
|
44
|
+
const zeroedSubrange = zeroSubrangeByContainerRange(
|
45
|
+
{
|
46
|
+
start: 10,
|
47
|
+
end: 20
|
48
|
+
},
|
49
|
+
{
|
50
|
+
start: 10,
|
51
|
+
end: 20
|
52
|
+
},
|
53
|
+
30
|
54
|
+
);
|
2
55
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
});
|
56
|
+
expect(zeroedSubrange).to.deep.equal({
|
57
|
+
start: 0,
|
58
|
+
end: 10
|
50
59
|
});
|
60
|
+
});
|
51
61
|
});
|