@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,16 +1,39 @@
|
|
1
|
-
import {expect} from
|
2
|
-
import modulateRangeBySequenceLength from
|
3
|
-
describe(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
}
|
1
|
+
import { expect } from "chai";
|
2
|
+
import modulateRangeBySequenceLength from "./modulateRangeBySequenceLength";
|
3
|
+
describe("modulateRangeBySequenceLength", function () {
|
4
|
+
it("should modulate ranges that are outside of the sequence length", function () {
|
5
|
+
//agtc
|
6
|
+
//0123
|
7
|
+
expect(
|
8
|
+
modulateRangeBySequenceLength({ start: 2, end: 5 }, 4)
|
9
|
+
).to.deep.equal({ start: 2, end: 1 });
|
10
|
+
expect(
|
11
|
+
modulateRangeBySequenceLength({ start: 2, end: 4 }, 4)
|
12
|
+
).to.deep.equal({ start: 2, end: 0 });
|
13
|
+
//agtc
|
14
|
+
//12345
|
15
|
+
expect(
|
16
|
+
modulateRangeBySequenceLength({ start: 2, end: 4 }, 4, {
|
17
|
+
inclusive1BasedEnd: true
|
18
|
+
})
|
19
|
+
).to.deep.equal({ start: 2, end: 4 });
|
20
|
+
expect(
|
21
|
+
modulateRangeBySequenceLength({ start: 2, end: 4 }, 4, {
|
22
|
+
inclusive1BasedEnd: true,
|
23
|
+
inclusive1BasedStart: true
|
24
|
+
})
|
25
|
+
).to.deep.equal({ start: 2, end: 4 });
|
26
|
+
expect(
|
27
|
+
modulateRangeBySequenceLength({ start: 5, end: 4 }, 4, {
|
28
|
+
inclusive1BasedEnd: true,
|
29
|
+
inclusive1BasedStart: true
|
30
|
+
})
|
31
|
+
).to.deep.equal({ start: 1, end: 4 });
|
32
|
+
expect(
|
33
|
+
modulateRangeBySequenceLength({ start: 5, end: 5 }, 4, {
|
34
|
+
inclusive1BasedEnd: true,
|
35
|
+
inclusive1BasedStart: true
|
36
|
+
})
|
37
|
+
).to.deep.equal({ start: 1, end: 1 });
|
38
|
+
});
|
39
|
+
});
|
@@ -1,20 +1,24 @@
|
|
1
1
|
//
|
2
2
|
// ac.throw([ac.posInt, ac.posInt, ac.bool], arguments);
|
3
|
-
export default function normalizePositionByRangeLength(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
3
|
+
export default function normalizePositionByRangeLength(
|
4
|
+
pPosition,
|
5
|
+
sequenceLength,
|
6
|
+
isInBetweenPositions
|
7
|
+
) {
|
8
|
+
//isInBetweenPositions refers to:
|
9
|
+
// A T G C
|
10
|
+
// 0 1 2 3 <-- isInBetweenPositions = false is counting the positions themselves
|
11
|
+
//0 1 2 3 4 <-- isInBetweenPositions = true is counting the spaces between positions
|
12
|
+
// ac.throw([ac.number, ac.posInt, ac.bool], arguments);
|
13
|
+
let position = pPosition;
|
14
|
+
if (position < 0) {
|
15
|
+
position += sequenceLength;
|
16
|
+
} else if (position + (isInBetweenPositions ? 0 : 1) > sequenceLength) {
|
17
|
+
position -= sequenceLength;
|
18
|
+
}
|
19
|
+
return position < 0
|
20
|
+
? 0
|
21
|
+
: position > sequenceLength - (isInBetweenPositions ? 0 : 1)
|
22
|
+
? sequenceLength - (isInBetweenPositions ? 0 : 1)
|
23
|
+
: position;
|
24
|
+
}
|
@@ -1,23 +1,23 @@
|
|
1
|
-
import {expect} from
|
2
|
-
import normalizePositionByRangeLength from
|
3
|
-
describe(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
})
|
1
|
+
import { expect } from "chai";
|
2
|
+
import normalizePositionByRangeLength from "./normalizePositionByRangeLength";
|
3
|
+
describe("normalizePositionByRangeLength", function () {
|
4
|
+
it("should normalize positions by their max length, assuming 0-based inclusive indices", function () {
|
5
|
+
expect(normalizePositionByRangeLength(10, 9)).to.equal(1);
|
6
|
+
expect(normalizePositionByRangeLength(9, 9)).to.equal(0);
|
7
|
+
expect(normalizePositionByRangeLength(3572, 2000)).to.equal(1572);
|
8
|
+
});
|
9
|
+
it("should set something more than twice outside the sequence length to either 0 or sequenceLength-1", function () {
|
10
|
+
expect(normalizePositionByRangeLength(-3572, 2000)).to.equal(0);
|
11
|
+
expect(normalizePositionByRangeLength(33572, 2000)).to.equal(1999);
|
12
|
+
});
|
13
|
+
it("should handle isInBetweenPositions === true being passed in ", function () {
|
14
|
+
expect(normalizePositionByRangeLength(9, 9, true)).to.equal(9);
|
15
|
+
});
|
16
|
+
it("should set something more than twice outside the sequence length to either 0 or sequenceLength when isInBetweenPositions is true", function () {
|
17
|
+
expect(normalizePositionByRangeLength(-3572, 2000, true)).to.equal(0);
|
18
|
+
expect(normalizePositionByRangeLength(33572, 2000, true)).to.equal(2000);
|
19
|
+
});
|
20
|
+
it("should not do anything if it does not have to", function () {
|
21
|
+
expect(normalizePositionByRangeLength(10, 99)).to.equal(10);
|
22
|
+
});
|
23
|
+
});
|
@@ -1,5 +1,8 @@
|
|
1
|
-
import modulatePositionByRange from
|
1
|
+
import modulatePositionByRange from "./modulatePositionByRange";
|
2
2
|
|
3
|
-
export default function normalizePositionByRangeLength1Based
|
4
|
-
|
5
|
-
|
3
|
+
export default function normalizePositionByRangeLength1Based(
|
4
|
+
position,
|
5
|
+
sequenceLength
|
6
|
+
) {
|
7
|
+
return modulatePositionByRange(position, { start: 1, end: sequenceLength });
|
8
|
+
}
|
@@ -1,9 +1,9 @@
|
|
1
|
-
import normalizePositionByRangeLength1Based from
|
2
|
-
import {expect} from
|
3
|
-
describe(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
});
|
1
|
+
import normalizePositionByRangeLength1Based from "./normalizePositionByRangeLength1Based";
|
2
|
+
import { expect } from "chai";
|
3
|
+
describe("normalizePositionByRangeLength1Based", function () {
|
4
|
+
it("should normalize a 1 based position by a 1 based range length", function () {
|
5
|
+
expect(normalizePositionByRangeLength1Based(0, 10)).to.equal(10);
|
6
|
+
expect(normalizePositionByRangeLength1Based(-1, 10)).to.equal(9);
|
7
|
+
expect(normalizePositionByRangeLength1Based(11, 10)).to.equal(1);
|
8
|
+
});
|
9
|
+
});
|
package/src/normalizeRange.js
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
//normalize range takes in a range that might be slightly outside of the rangeMax and wraps the start/end as necessary to fit
|
2
|
-
import {assign} from "lodash";
|
2
|
+
import { assign } from "lodash";
|
3
3
|
|
4
|
-
import normalizePositionByRangeLength from
|
4
|
+
import normalizePositionByRangeLength from "./normalizePositionByRangeLength";
|
5
5
|
|
6
6
|
export default function normalizeRange(range, sequenceLength) {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
}
|
7
|
+
return assign({}, range, {
|
8
|
+
start: normalizePositionByRangeLength(range.start, sequenceLength),
|
9
|
+
end: normalizePositionByRangeLength(range.end, sequenceLength)
|
10
|
+
});
|
11
|
+
}
|
@@ -1,9 +1,9 @@
|
|
1
|
-
import normalizeRange from
|
2
|
-
import {expect} from
|
3
|
-
describe(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
});
|
1
|
+
import normalizeRange from "./normalizeRange";
|
2
|
+
import { expect } from "chai";
|
3
|
+
describe("normalizeRange", function () {
|
4
|
+
it("should normalize range correctly", function () {
|
5
|
+
const normalizedRange = normalizeRange({ start: 0, end: -1 }, 10);
|
6
|
+
expect(normalizedRange.start).to.equal(0);
|
7
|
+
expect(normalizedRange.end).to.equal(9);
|
8
|
+
});
|
9
|
+
});
|
@@ -1,26 +1,39 @@
|
|
1
|
-
import {assign} from "lodash";
|
1
|
+
import { assign } from "lodash";
|
2
2
|
|
3
|
-
export default function provideInclusiveOptions
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
}
|
15
|
-
})
|
3
|
+
export default function provideInclusiveOptions(funToWrap) {
|
4
|
+
return function () {
|
5
|
+
const args = Array.prototype.slice.call(arguments);
|
6
|
+
const options = args[args.length - 1];
|
7
|
+
if (
|
8
|
+
options &&
|
9
|
+
(options.inclusive1BasedEnd || options.inclusive1BasedStart)
|
10
|
+
) {
|
11
|
+
args.forEach(function (arg, index) {
|
12
|
+
if (arg && arg.start > -1 && options.inclusive1BasedStart) {
|
13
|
+
args[index] = assign(arg, { start: arg.start - 1 });
|
16
14
|
}
|
17
|
-
|
18
|
-
|
19
|
-
returnVal = assign(returnVal, {start: returnVal.start+1})
|
15
|
+
if (arg && arg.end > -1 && options.inclusive1BasedEnd) {
|
16
|
+
args[index] = assign(arg, { end: arg.end - 1 });
|
20
17
|
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
});
|
19
|
+
}
|
20
|
+
let returnVal = funToWrap.apply(this, args);
|
21
|
+
if (
|
22
|
+
returnVal &&
|
23
|
+
returnVal.start > -1 &&
|
24
|
+
options &&
|
25
|
+
options.inclusive1BasedStart
|
26
|
+
) {
|
27
|
+
returnVal = assign(returnVal, { start: returnVal.start + 1 });
|
28
|
+
}
|
29
|
+
if (
|
30
|
+
returnVal &&
|
31
|
+
returnVal.end > -1 &&
|
32
|
+
options &&
|
33
|
+
options.inclusive1BasedEnd
|
34
|
+
) {
|
35
|
+
returnVal = assign(returnVal, { end: returnVal.end + 1 });
|
36
|
+
}
|
37
|
+
return returnVal;
|
38
|
+
};
|
39
|
+
}
|
@@ -1,13 +1,17 @@
|
|
1
1
|
//
|
2
|
-
export default function reversePositionInRange(
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
2
|
+
export default function reversePositionInRange(
|
3
|
+
position,
|
4
|
+
rangeLength,
|
5
|
+
isInBetweenPositions
|
6
|
+
) {
|
7
|
+
//isInBetweenPositions refers to:
|
8
|
+
// A T G C
|
9
|
+
// 0 1 2 3 <-- isInBetweenPositions = false is counting the positions themselves
|
10
|
+
//0 1 2 3 4 <-- isInBetweenPositions = true is counting the spaces between positions
|
11
|
+
// ac.throw([
|
12
|
+
// ac.posInt,
|
13
|
+
// ac.posInt,
|
14
|
+
// ac.bool
|
15
|
+
// ], arguments);
|
16
|
+
return rangeLength - position - (isInBetweenPositions ? 0 : 1);
|
17
|
+
}
|
@@ -1,31 +1,34 @@
|
|
1
1
|
//
|
2
2
|
// ac.throw([ac.posInt, ac.posInt, ac.bool], arguments);
|
3
3
|
//takes a potentially circular range and returns an array containing the range split on the origin
|
4
|
-
export default function splitRangeIntoTwoPartsIfItIsCircular(
|
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
|
-
};
|
4
|
+
export default function splitRangeIntoTwoPartsIfItIsCircular(
|
5
|
+
range,
|
6
|
+
sequenceLength
|
7
|
+
) {
|
8
|
+
// ac.throw([ac.range, ac.number], arguments);
|
9
|
+
if (sequenceLength !== 0) {
|
10
|
+
sequenceLength = sequenceLength || Infinity;
|
11
|
+
}
|
12
|
+
const ranges = [];
|
13
|
+
if (range.start > range.end) {
|
14
|
+
//the range is cicular, so we return an array of two ranges
|
15
|
+
ranges.push({
|
16
|
+
start: 0,
|
17
|
+
end: range.end,
|
18
|
+
type: "end"
|
19
|
+
});
|
20
|
+
ranges.push({
|
21
|
+
start: range.start,
|
22
|
+
end: sequenceLength - 1,
|
23
|
+
type: "beginning"
|
24
|
+
});
|
25
|
+
} else {
|
26
|
+
//the range isn't circular, so we just return the range
|
27
|
+
ranges.push({
|
28
|
+
start: range.start,
|
29
|
+
end: range.end,
|
30
|
+
type: "beginningAndEnd"
|
31
|
+
});
|
32
|
+
}
|
33
|
+
return ranges;
|
34
|
+
}
|
@@ -18,16 +18,27 @@
|
|
18
18
|
// }
|
19
19
|
// };
|
20
20
|
|
21
|
+
import splitRangeIntoTwoPartsIfItIsCircular from "./splitRangeIntoTwoPartsIfItIsCircular.js";
|
21
22
|
|
22
|
-
import
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
import assert from "assert";
|
24
|
+
describe("splitRangeIntoTwoPartsIfItIsCircular", function () {
|
25
|
+
it("returns an array with one range in it if the array is non-circular", function () {
|
26
|
+
assert.deepEqual(
|
27
|
+
splitRangeIntoTwoPartsIfItIsCircular({ start: 0, end: 100 }, 1000),
|
28
|
+
[{ start: 0, end: 100, type: "beginningAndEnd" }]
|
29
|
+
);
|
30
|
+
assert.deepEqual(
|
31
|
+
splitRangeIntoTwoPartsIfItIsCircular({ start: 10, end: 909 }, 1000),
|
32
|
+
[{ start: 10, end: 909, type: "beginningAndEnd" }]
|
33
|
+
);
|
34
|
+
});
|
35
|
+
it("returns an array with two ranges in it if the array is circular", function () {
|
36
|
+
assert.deepEqual(
|
37
|
+
splitRangeIntoTwoPartsIfItIsCircular({ start: 110, end: 100 }, 1000),
|
38
|
+
[
|
39
|
+
{ start: 0, end: 100, type: "end" },
|
40
|
+
{ start: 110, end: 999, type: "beginning" }
|
41
|
+
]
|
42
|
+
);
|
43
|
+
});
|
33
44
|
});
|
package/src/translateRange.js
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
-
import {assign} from "lodash";
|
2
|
-
import normalizePositionByRangeLength from
|
1
|
+
import { assign } from "lodash";
|
2
|
+
import normalizePositionByRangeLength from "./normalizePositionByRangeLength";
|
3
3
|
|
4
|
-
export default function translateRange(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
}
|
4
|
+
export default function translateRange(
|
5
|
+
rangeToBeAdjusted,
|
6
|
+
translateBy,
|
7
|
+
rangeLength
|
8
|
+
) {
|
9
|
+
return assign({}, rangeToBeAdjusted, {
|
10
|
+
start: normalizePositionByRangeLength(
|
11
|
+
rangeToBeAdjusted.start + translateBy,
|
12
|
+
rangeLength
|
13
|
+
),
|
14
|
+
end: normalizePositionByRangeLength(
|
15
|
+
rangeToBeAdjusted.end + translateBy,
|
16
|
+
rangeLength
|
17
|
+
)
|
18
|
+
});
|
19
|
+
}
|
@@ -1,20 +1,19 @@
|
|
1
|
-
import translateRange from
|
2
|
-
import {expect} from
|
1
|
+
import translateRange from "./translateRange";
|
2
|
+
import { expect } from "chai";
|
3
3
|
|
4
|
-
describe(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
})
|
4
|
+
describe("translateRange", function () {
|
5
|
+
it("should correctly translate a range", function () {
|
6
|
+
expect(translateRange({ start: 0, end: 10 }, 10, 30)).to.deep.equal({
|
7
|
+
start: 10,
|
8
|
+
end: 20
|
9
|
+
});
|
10
|
+
expect(translateRange({ start: 0, end: 10 }, 10, 15)).to.deep.equal({
|
11
|
+
start: 10,
|
12
|
+
end: 5
|
13
|
+
});
|
14
|
+
expect(translateRange({ start: 9, end: 10 }, 10, 15)).to.deep.equal({
|
15
|
+
start: 4,
|
16
|
+
end: 5
|
17
|
+
});
|
18
|
+
});
|
19
|
+
});
|
@@ -1,57 +1,60 @@
|
|
1
1
|
//
|
2
|
-
export default function trimNonCicularRangeByAnotherNonCircularRange(
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
2
|
+
export default function trimNonCicularRangeByAnotherNonCircularRange(
|
3
|
+
rangeToBeTrimmed,
|
4
|
+
trimmingRange
|
5
|
+
) {
|
6
|
+
// ac.throw([ac.range, ac.range], arguments);
|
7
|
+
let outputTrimmedRange;
|
8
|
+
if (!rangeToBeTrimmed) {
|
9
|
+
return outputTrimmedRange;
|
10
|
+
}
|
11
|
+
if (rangeToBeTrimmed.start < trimmingRange.start) {
|
12
|
+
if (rangeToBeTrimmed.end < trimmingRange.start) {
|
10
13
|
// rrr <range to be trimmed
|
11
14
|
// ttt <trimming range
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
outputTrimmedRange = {
|
16
|
+
start: rangeToBeTrimmed.start,
|
17
|
+
end: rangeToBeTrimmed.end
|
18
|
+
};
|
19
|
+
} else {
|
20
|
+
if (rangeToBeTrimmed.end > trimmingRange.end) {
|
18
21
|
// rrrrrr <range to be trimmed
|
19
22
|
// ttt <trimming range
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
outputTrimmedRange = {
|
24
|
+
start: rangeToBeTrimmed.start,
|
25
|
+
end: rangeToBeTrimmed.end
|
26
|
+
};
|
27
|
+
} else {
|
25
28
|
// rrrrrr <range to be trimmed
|
26
29
|
// ttt <trimming range
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
outputTrimmedRange = {
|
31
|
+
start: rangeToBeTrimmed.start,
|
32
|
+
end: trimmingRange.start - 1
|
33
|
+
};
|
34
|
+
}
|
35
|
+
}
|
36
|
+
} else {
|
37
|
+
if (rangeToBeTrimmed.end <= trimmingRange.end) {
|
35
38
|
// rrr <range to be trimmed
|
36
39
|
// ttttt <trimming range
|
37
40
|
//fully deleting the range, so do nothing
|
38
|
-
|
39
|
-
|
41
|
+
} else {
|
42
|
+
if (rangeToBeTrimmed.start > trimmingRange.end) {
|
40
43
|
// rrrrrr <range to be trimmed
|
41
44
|
// ttt <trimming range
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
outputTrimmedRange = {
|
46
|
+
end: rangeToBeTrimmed.end,
|
47
|
+
start: rangeToBeTrimmed.start
|
48
|
+
};
|
49
|
+
} else {
|
47
50
|
// rrrrrr <range to be trimmed
|
48
51
|
// ttt <trimming range
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
}
|
52
|
+
outputTrimmedRange = {
|
53
|
+
end: rangeToBeTrimmed.end,
|
54
|
+
start: trimmingRange.end + 1
|
55
|
+
};
|
56
|
+
}
|
55
57
|
}
|
56
|
-
|
57
|
-
|
58
|
+
}
|
59
|
+
return outputTrimmedRange;
|
60
|
+
}
|
@@ -1,12 +1,15 @@
|
|
1
1
|
//
|
2
2
|
// ac.throw([ac.posInt, ac.posInt, ac.bool], arguments);
|
3
|
-
export default function trimNumberToFitWithin0ToAnotherNumber(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
export default function trimNumberToFitWithin0ToAnotherNumber(
|
4
|
+
numberToBeTrimmed,
|
5
|
+
max
|
6
|
+
) {
|
7
|
+
// ac.throw([ac.number, ac.number], arguments);
|
8
|
+
if (numberToBeTrimmed < 0) {
|
9
|
+
numberToBeTrimmed = 0;
|
10
|
+
}
|
11
|
+
if (numberToBeTrimmed > max) {
|
12
|
+
numberToBeTrimmed = max;
|
13
|
+
}
|
14
|
+
return numberToBeTrimmed;
|
15
|
+
}
|