@teselagen/range-utils 0.3.7 → 0.3.9
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.cjs +2619 -0
- package/index.d.ts +52 -51
- package/index.js +1791 -6100
- package/index.umd.cjs +2623 -0
- package/package.json +2 -3
- 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 +52 -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/trimAnnStartEndToFitSeqLength.js +9 -0
- package/src/trimAnnStartEndToFitSeqLength.test.js +35 -0
- 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
- package/trimAnnStartEndToFitSeqLength.d.ts +1 -0
- package/trimAnnStartEndToFitSeqLength.test.d.ts +1 -0
- package/index.mjs +0 -6928
- package/index.umd.js +0 -6932
@@ -1,22 +1,41 @@
|
|
1
|
-
import convertRangeIndices from
|
2
|
-
import chai from
|
1
|
+
import convertRangeIndices from "./convertRangeIndices";
|
2
|
+
import chai from "chai";
|
3
3
|
chai.should();
|
4
|
-
describe(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
}
|
4
|
+
describe("convertRangeIndices", function () {
|
5
|
+
it("should correctly convert various types of ranges", function () {
|
6
|
+
convertRangeIndices(
|
7
|
+
{ start: 9, end: 0 },
|
8
|
+
{ inclusive1BasedStart: true }
|
9
|
+
).should.deep.equal({ start: 8, end: 0 });
|
10
|
+
convertRangeIndices(
|
11
|
+
{ start: 9, end: 0 },
|
12
|
+
{ inclusive1BasedStart: true },
|
13
|
+
{ inclusive1BasedEnd: true }
|
14
|
+
).should.deep.equal({ start: 8, end: 1 });
|
15
|
+
convertRangeIndices(
|
16
|
+
{ start: 9, end: 0 },
|
17
|
+
{ inclusive1BasedEnd: true },
|
18
|
+
{ inclusive1BasedEnd: true }
|
19
|
+
).should.deep.equal({ start: 9, end: 0 });
|
20
|
+
convertRangeIndices(
|
21
|
+
{ start: 4, end: 5 },
|
22
|
+
{ inclusive1BasedEnd: true },
|
23
|
+
{ inclusive1BasedStart: true }
|
24
|
+
).should.deep.equal({ start: 5, end: 4 });
|
25
|
+
convertRangeIndices(
|
26
|
+
{
|
27
|
+
start: "1",
|
28
|
+
end: "28"
|
29
|
+
},
|
30
|
+
{ inclusive1BasedStart: true, inclusive1BasedEnd: true },
|
31
|
+
{}
|
32
|
+
).should.deep.equal({ start: 0, end: 27 });
|
33
|
+
});
|
34
|
+
it("should not remove other attributes on the range object", function () {
|
35
|
+
convertRangeIndices(
|
36
|
+
{ start: 4, end: 5, someOtherAttribute: "yay" },
|
37
|
+
{ inclusive1BasedEnd: true },
|
38
|
+
{ inclusive1BasedStart: true }
|
39
|
+
).should.deep.equal({ start: 5, end: 4, someOtherAttribute: "yay" });
|
40
|
+
});
|
41
|
+
});
|
@@ -1,5 +1,8 @@
|
|
1
|
-
import convertRangeIndices from
|
1
|
+
import convertRangeIndices from "./convertRangeIndices";
|
2
2
|
|
3
|
-
export default function convertRangeTo0Based
|
4
|
-
return convertRangeIndices(range, {
|
5
|
-
|
3
|
+
export default function convertRangeTo0Based(range) {
|
4
|
+
return convertRangeIndices(range, {
|
5
|
+
inclusive1BasedStart: true,
|
6
|
+
inclusive1BasedEnd: true
|
7
|
+
});
|
8
|
+
}
|
@@ -1,5 +1,9 @@
|
|
1
|
-
import convertRangeIndices from
|
1
|
+
import convertRangeIndices from "./convertRangeIndices";
|
2
2
|
|
3
|
-
export default function convertRangeTo1Based
|
4
|
-
return convertRangeIndices(
|
5
|
-
|
3
|
+
export default function convertRangeTo1Based(range) {
|
4
|
+
return convertRangeIndices(
|
5
|
+
range,
|
6
|
+
{},
|
7
|
+
{ inclusive1BasedStart: true, inclusive1BasedEnd: true }
|
8
|
+
);
|
9
|
+
}
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import getRangeLength from
|
1
|
+
import getRangeLength from "./getRangeLength";
|
2
2
|
|
3
|
-
export default function doesRangeSpanEntireSequence
|
3
|
+
export default function doesRangeSpanEntireSequence(range, sequenceLength) {
|
4
4
|
if (getRangeLength(range) === sequenceLength) {
|
5
|
-
|
5
|
+
return true;
|
6
6
|
}
|
7
|
-
}
|
7
|
+
}
|
@@ -1,3 +1,3 @@
|
|
1
|
-
export default function doesRangeSpanOrigin
|
2
|
-
return range.start > range.end
|
3
|
-
}
|
1
|
+
export default function doesRangeSpanOrigin(range) {
|
2
|
+
return range.start > range.end;
|
3
|
+
}
|
@@ -1,41 +1,48 @@
|
|
1
|
-
import normalizePositionByRangeLength from
|
2
|
-
import {assign} from "lodash";
|
1
|
+
import normalizePositionByRangeLength from "./normalizePositionByRangeLength";
|
2
|
+
import { assign } from "lodash-es";
|
3
3
|
|
4
|
-
export default function expandOrContractCircularRangeToPosition(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
4
|
+
export default function expandOrContractCircularRangeToPosition(
|
5
|
+
range,
|
6
|
+
position,
|
7
|
+
maxLength
|
8
|
+
) {
|
9
|
+
// 0 1 2 3 4 5 6 7 8 9
|
10
|
+
// r r r r r - - r r r
|
11
|
+
//0 1 2 3 4 5 6 7 8 9 10
|
12
|
+
// |
|
13
|
+
const newRange = assign({}, range);
|
14
|
+
let endMoved = true;
|
15
|
+
if (range.end >= position) {
|
16
|
+
if (position + maxLength - range.start > range.end - position) {
|
17
|
+
newRange.end = normalizePositionByRangeLength(
|
18
|
+
position - 1,
|
19
|
+
maxLength,
|
20
|
+
false
|
21
|
+
);
|
22
|
+
} else {
|
23
|
+
newRange.start = position;
|
24
|
+
endMoved = false;
|
25
|
+
}
|
26
|
+
} else {
|
27
|
+
if (range.start < position) {
|
28
|
+
if (range.end + maxLength - position > position - range.start) {
|
29
|
+
newRange.start = position;
|
30
|
+
endMoved = false;
|
31
|
+
} else {
|
32
|
+
newRange.end = position - 1;
|
33
|
+
}
|
19
34
|
} else {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
} else {
|
28
|
-
//position somewhere between end and start
|
29
|
-
if (range.start - position > position - range.end) {
|
30
|
-
newRange.end = position - 1;
|
31
|
-
} else {
|
32
|
-
endMoved = false;
|
33
|
-
newRange.start = position;
|
34
|
-
}
|
35
|
-
}
|
35
|
+
//position somewhere between end and start
|
36
|
+
if (range.start - position > position - range.end) {
|
37
|
+
newRange.end = position - 1;
|
38
|
+
} else {
|
39
|
+
endMoved = false;
|
40
|
+
newRange.start = position;
|
41
|
+
}
|
36
42
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
};
|
43
|
+
}
|
44
|
+
return {
|
45
|
+
newRange: newRange,
|
46
|
+
endMoved: endMoved
|
47
|
+
};
|
48
|
+
}
|
@@ -1,25 +1,28 @@
|
|
1
|
-
import {assign} from "lodash";
|
1
|
+
import { assign } from "lodash-es";
|
2
2
|
|
3
|
-
export default function expandOrContractNonCircularRangeToPosition
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
export default function expandOrContractNonCircularRangeToPosition(
|
4
|
+
range,
|
5
|
+
position
|
6
|
+
) {
|
7
|
+
const newRange = assign({}, range);
|
8
|
+
let endMoved = true;
|
9
|
+
if (range.start > position) {
|
10
|
+
newRange.start = position;
|
11
|
+
endMoved = false;
|
12
|
+
} else {
|
13
|
+
if (range.end < position) {
|
14
|
+
newRange.end = position - 1;
|
15
|
+
} else {
|
16
|
+
if (position - range.start > range.end - position) {
|
17
|
+
newRange.end = position - 1;
|
18
|
+
} else {
|
7
19
|
newRange.start = position;
|
8
20
|
endMoved = false;
|
9
|
-
|
10
|
-
if (range.end < position) {
|
11
|
-
newRange.end = position - 1;
|
12
|
-
} else {
|
13
|
-
if (position - range.start > range.end - position) {
|
14
|
-
newRange.end = position - 1;
|
15
|
-
} else {
|
16
|
-
newRange.start = position;
|
17
|
-
endMoved = false;
|
18
|
-
}
|
19
|
-
}
|
21
|
+
}
|
20
22
|
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
};
|
23
|
+
}
|
24
|
+
return {
|
25
|
+
newRange: newRange,
|
26
|
+
endMoved: endMoved
|
27
|
+
};
|
28
|
+
}
|
@@ -1,12 +1,17 @@
|
|
1
|
-
import {clone} from
|
2
|
-
import normalizeRange from
|
1
|
+
import { clone } from "lodash-es";
|
2
|
+
import normalizeRange from "./normalizeRange";
|
3
3
|
|
4
|
-
export default function expandOrContractRangeByLength(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
export default function expandOrContractRangeByLength(
|
5
|
+
range,
|
6
|
+
shiftBy,
|
7
|
+
shiftStart,
|
8
|
+
sequenceLength
|
9
|
+
) {
|
10
|
+
const rangeToReturn = clone(range);
|
11
|
+
if (shiftStart) {
|
12
|
+
rangeToReturn.start -= shiftBy;
|
13
|
+
} else {
|
14
|
+
rangeToReturn.end += shiftBy;
|
15
|
+
}
|
16
|
+
return normalizeRange(rangeToReturn, sequenceLength);
|
17
|
+
}
|
@@ -1,77 +1,115 @@
|
|
1
|
-
import expandOrContractRangeByLength from
|
2
|
-
import chai from
|
3
|
-
const expect = chai.expect;
|
1
|
+
import expandOrContractRangeByLength from "./expandOrContractRangeByLength";
|
2
|
+
import chai from "chai";
|
4
3
|
chai.should();
|
5
4
|
|
6
|
-
describe(
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
5
|
+
describe("expandOrContractRangeByLength", function () {
|
6
|
+
it("shift start by 1 ", function () {
|
7
|
+
const expandedRange = expandOrContractRangeByLength(
|
8
|
+
{ start: 3, end: 4 },
|
9
|
+
1,
|
10
|
+
true,
|
11
|
+
10
|
12
|
+
);
|
13
|
+
expandedRange.should.deep.equal({
|
14
|
+
start: 2,
|
15
|
+
end: 4
|
16
|
+
});
|
17
|
+
});
|
18
|
+
it("shift end by 1 ", function () {
|
19
|
+
const expandedRange = expandOrContractRangeByLength(
|
20
|
+
{ start: 3, end: 4 },
|
21
|
+
1,
|
22
|
+
false,
|
23
|
+
10
|
24
|
+
);
|
25
|
+
expandedRange.should.deep.equal({
|
26
|
+
start: 3,
|
27
|
+
end: 5
|
28
|
+
});
|
29
|
+
});
|
30
|
+
it("shift end by 6 ", function () {
|
31
|
+
const expandedRange = expandOrContractRangeByLength(
|
32
|
+
{ start: 3, end: 4 },
|
33
|
+
6,
|
34
|
+
false,
|
35
|
+
10
|
36
|
+
);
|
37
|
+
expandedRange.should.deep.equal({
|
38
|
+
start: 3,
|
39
|
+
end: 0
|
40
|
+
});
|
41
|
+
});
|
28
42
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
43
|
+
it("circular range", function () {
|
44
|
+
const expandedRange = expandOrContractRangeByLength(
|
45
|
+
{ start: 6, end: 4 },
|
46
|
+
1,
|
47
|
+
false,
|
48
|
+
10
|
49
|
+
);
|
50
|
+
expandedRange.should.deep.equal({
|
51
|
+
start: 6,
|
52
|
+
end: 5
|
53
|
+
});
|
54
|
+
});
|
55
|
+
it("circular range", function () {
|
56
|
+
const expandedRange = expandOrContractRangeByLength(
|
57
|
+
{ start: 6, end: 4 },
|
58
|
+
1,
|
59
|
+
true,
|
60
|
+
10
|
61
|
+
);
|
62
|
+
expandedRange.should.deep.equal({
|
63
|
+
start: 5,
|
64
|
+
end: 4
|
65
|
+
});
|
66
|
+
});
|
67
|
+
it("circular range", function () {
|
68
|
+
const expandedRange = expandOrContractRangeByLength(
|
69
|
+
{ start: 6, end: 4 },
|
70
|
+
1,
|
71
|
+
true,
|
72
|
+
10
|
73
|
+
);
|
74
|
+
expandedRange.should.deep.equal({
|
75
|
+
start: 5,
|
76
|
+
end: 4
|
77
|
+
});
|
78
|
+
});
|
50
79
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
80
|
+
it("negative shiftBy", function () {
|
81
|
+
const expandedRange = expandOrContractRangeByLength(
|
82
|
+
{ start: 6, end: 4 },
|
83
|
+
-1,
|
84
|
+
true,
|
85
|
+
10
|
86
|
+
);
|
87
|
+
expandedRange.should.deep.equal({
|
88
|
+
start: 7,
|
89
|
+
end: 4
|
90
|
+
});
|
91
|
+
});
|
92
|
+
it("negative shiftBy", function () {
|
93
|
+
const expandedRange = expandOrContractRangeByLength(
|
94
|
+
{ start: 6, end: 4 },
|
95
|
+
-1,
|
96
|
+
false,
|
97
|
+
10
|
98
|
+
);
|
99
|
+
expandedRange.should.deep.equal({
|
100
|
+
start: 6,
|
101
|
+
end: 3
|
102
|
+
});
|
103
|
+
});
|
65
104
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
105
|
+
// it('should error if trying to expand more than possible', function () {
|
106
|
+
// var error = false;
|
107
|
+
// try {
|
108
|
+
// var range = expandOrContractRangeByLength({start: 6,end:4}, 10, false, 10)
|
109
|
+
// console.log('range:', range)
|
110
|
+
// } catch (e) {
|
111
|
+
// error = true;
|
112
|
+
// }
|
113
|
+
// expect(error).to.be.true;
|
114
|
+
// });
|
76
115
|
});
|
77
|
-
|
@@ -1,10 +1,18 @@
|
|
1
|
-
import expandOrContractCircularRangeToPosition from
|
2
|
-
import expandOrContractNonCircularRangeToPosition from
|
1
|
+
import expandOrContractCircularRangeToPosition from "./expandOrContractCircularRangeToPosition";
|
2
|
+
import expandOrContractNonCircularRangeToPosition from "./expandOrContractNonCircularRangeToPosition";
|
3
3
|
|
4
|
-
export default function expandOrContractRangeToPosition(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
export default function expandOrContractRangeToPosition(
|
5
|
+
range,
|
6
|
+
position,
|
7
|
+
maxLength
|
8
|
+
) {
|
9
|
+
if (range.start > range.end) {
|
10
|
+
return expandOrContractCircularRangeToPosition(range, position, maxLength);
|
11
|
+
} else {
|
12
|
+
return expandOrContractNonCircularRangeToPosition(
|
13
|
+
range,
|
14
|
+
position,
|
15
|
+
maxLength
|
16
|
+
);
|
17
|
+
}
|
18
|
+
}
|