@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.
Files changed (89) hide show
  1. package/README.md +94 -0
  2. package/flipContainedRange.d.ts +1 -1
  3. package/index.js +233 -85
  4. package/index.mjs +233 -85
  5. package/index.umd.js +233 -85
  6. package/package.json +1 -2
  7. package/src/adjustRangeToDeletionOfAnotherRange.test.js +166 -105
  8. package/src/adjustRangeToInsert.js +29 -26
  9. package/src/adjustRangeToInsert.test.js +197 -109
  10. package/src/adjustRangeToRotation.js +4 -5
  11. package/src/adjustRangeToRotation.test.js +203 -119
  12. package/src/checkIfNonCircularRangesOverlap.js +18 -15
  13. package/src/checkIfNonCircularRangesOverlap.test.js +67 -42
  14. package/src/checkIfPotentiallyCircularRangesOverlap.js +22 -12
  15. package/src/checkIfPotentiallyCircularRangesOverlap.test.js +115 -70
  16. package/src/collapseOverlapsGeneratedFromRangeComparisonIfPossible.js +72 -41
  17. package/src/collapseOverlapsGeneratedFromRangeComparisonIfPossible.test.js +183 -84
  18. package/src/convertRangeIndices.js +24 -20
  19. package/src/convertRangeIndices.test.js +40 -21
  20. package/src/convertRangeTo0Based.js +7 -4
  21. package/src/convertRangeTo1Based.js +8 -4
  22. package/src/doesRangeSpanEntireSequence.js +4 -4
  23. package/src/doesRangeSpanOrigin.js +3 -3
  24. package/src/expandOrContractCircularRangeToPosition.js +45 -38
  25. package/src/expandOrContractNonCircularRangeToPosition.js +24 -21
  26. package/src/expandOrContractRangeByLength.js +16 -11
  27. package/src/expandOrContractRangeByLength.test.js +109 -71
  28. package/src/expandOrContractRangeToPosition.js +17 -9
  29. package/src/flipContainedRange.js +133 -72
  30. package/src/flipContainedRange.test.js +143 -117
  31. package/src/generateRandomRange.js +17 -15
  32. package/src/generateRandomRange.test.js +21 -21
  33. package/src/getAnnotationRangeType.js +25 -21
  34. package/src/getAnnotationRangeType.test.js +56 -57
  35. package/src/getEachPositionInRangeAsArray.js +13 -13
  36. package/src/getEachPositionInRangeAsArray.test.js +12 -8
  37. package/src/getLengthOfOverlappingRegionsBetweenTwoRanges.js +16 -8
  38. package/src/getLengthOfOverlappingRegionsBetweenTwoRanges.test.js +32 -24
  39. package/src/getMiddleOfRange.js +1 -1
  40. package/src/getMiddleOfRange.test.js +5 -13
  41. package/src/getOverlapOfNonCircularRanges.js +30 -30
  42. package/src/getOverlapsOfPotentiallyCircularRanges.js +5 -5
  43. package/src/getOverlapsOfPotentiallyCircularRanges.test.js +3 -23
  44. package/src/getPositionFromAngle.js +12 -7
  45. package/src/getRangeAngles.js +1 -1
  46. package/src/getRangeAngles.test.js +10 -11
  47. package/src/getRangeLength.test.js +5 -5
  48. package/src/getRangesBetweenTwoRanges.js +31 -22
  49. package/src/getSequenceWithinRange.js +13 -13
  50. package/src/getSequenceWithinRange.test.js +43 -45
  51. package/src/getShortestDistanceBetweenTwoPositions.js +10 -6
  52. package/src/getShortestDistanceBetweenTwoPositions.test.js +11 -13
  53. package/src/getYOffsetForPotentiallyCircularRange.js +33 -21
  54. package/src/getYOffsetsForPotentiallyCircularRanges.js +26 -19
  55. package/src/getYOffsetsForPotentiallyCircularRanges.test.js +42 -29
  56. package/src/getZeroedRangeOverlaps.js +30 -15
  57. package/src/getZeroedRangeOverlaps.test.js +75 -36
  58. package/src/index.js +51 -51
  59. package/src/index.test.js +6 -8
  60. package/src/invertRange.test.js +103 -93
  61. package/src/isPositionCloserToRangeStartThanRangeEnd.js +18 -6
  62. package/src/isPositionCloserToRangeStartThanRangeEnd.test.js +36 -16
  63. package/src/isPositionWithinRange.js +4 -4
  64. package/src/isRangeOrPositionWithinRange.js +18 -12
  65. package/src/isRangeOrPositionWithinRange.test.js +6 -6
  66. package/src/isRangeWithinRange.js +2 -3
  67. package/src/loopEachPositionInRange.js +3 -3
  68. package/src/modulatePositionByRange.js +8 -8
  69. package/src/modulatePositionByRange.test.js +10 -11
  70. package/src/modulateRangeBySequenceLength.js +7 -7
  71. package/src/modulateRangeBySequenceLength.test.js +39 -16
  72. package/src/normalizePositionByRangeLength.js +22 -18
  73. package/src/normalizePositionByRangeLength.test.js +23 -23
  74. package/src/normalizePositionByRangeLength1Based.js +7 -4
  75. package/src/normalizePositionByRangeLength1Based.test.js +9 -9
  76. package/src/normalizeRange.js +7 -7
  77. package/src/normalizeRange.test.js +9 -9
  78. package/src/provideInclusiveOptions.js +36 -23
  79. package/src/reversePositionInRange.js +16 -12
  80. package/src/splitRangeIntoTwoPartsIfItIsCircular.js +31 -28
  81. package/src/splitRangeIntoTwoPartsIfItIsCircular.test.js +22 -11
  82. package/src/translateRange.js +18 -8
  83. package/src/translateRange.test.js +18 -19
  84. package/src/trimNonCicularRangeByAnotherNonCircularRange.js +45 -42
  85. package/src/trimNumberToFitWithin0ToAnotherNumber.js +13 -10
  86. package/src/trimRangeByAnotherRange.js +20 -19
  87. package/src/trimRangeByAnotherRange.test.js +6 -6
  88. package/src/zeroSubrangeByContainerRange.js +29 -19
  89. package/src/zeroSubrangeByContainerRange.test.js +57 -47
@@ -1,22 +1,41 @@
1
- import convertRangeIndices from './convertRangeIndices';
2
- import chai from 'chai';
1
+ import convertRangeIndices from "./convertRangeIndices";
2
+ import chai from "chai";
3
3
  chai.should();
4
- describe('convertRangeIndices', function () {
5
- it('should correctly convert various types of ranges', function () {
6
- convertRangeIndices({start: 9,end:0},{inclusive1BasedStart: true}).should.deep.equal({start: 8,end: 0})
7
- convertRangeIndices({start: 9,end:0},{inclusive1BasedStart: true}, {inclusive1BasedEnd: true}).should.deep.equal({start: 8,end: 1})
8
- convertRangeIndices({start: 9,end:0},{inclusive1BasedEnd: true}, {inclusive1BasedEnd: true}).should.deep.equal({start: 9,end: 0})
9
- convertRangeIndices({start: 4,end:5},{inclusive1BasedEnd: true}, {inclusive1BasedStart: true}).should.deep.equal({start: 5,end: 4})
10
- convertRangeIndices({
11
- start: '1',
12
- end: '28',
13
- },
14
- {inclusive1BasedStart: true, inclusive1BasedEnd: true},
15
- {})
16
- .should.deep.equal({start: 0,end: 27})
17
- });
18
- it('should not remove other attributes on the range object', function () {
19
- convertRangeIndices({start: 4,end:5, someOtherAttribute: 'yay'},{inclusive1BasedEnd: true}, {inclusive1BasedStart: true})
20
- .should.deep.equal({start: 5,end: 4, someOtherAttribute: 'yay'})
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 './convertRangeIndices';
1
+ import convertRangeIndices from "./convertRangeIndices";
2
2
 
3
- export default function convertRangeTo0Based (range) {
4
- return convertRangeIndices(range, {inclusive1BasedStart: true, inclusive1BasedEnd: true})
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 './convertRangeIndices';
1
+ import convertRangeIndices from "./convertRangeIndices";
2
2
 
3
- export default function convertRangeTo1Based (range) {
4
- return convertRangeIndices(range, {}, {inclusive1BasedStart: true, inclusive1BasedEnd: true})
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 './getRangeLength';
1
+ import getRangeLength from "./getRangeLength";
2
2
 
3
- export default function doesRangeSpanEntireSequence (range, sequenceLength) {
3
+ export default function doesRangeSpanEntireSequence(range, sequenceLength) {
4
4
  if (getRangeLength(range) === sequenceLength) {
5
- return true
5
+ return true;
6
6
  }
7
- };
7
+ }
@@ -1,3 +1,3 @@
1
- export default function doesRangeSpanOrigin (range) {
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 './normalizePositionByRangeLength';
2
- import {assign} from "lodash";
1
+ import normalizePositionByRangeLength from "./normalizePositionByRangeLength";
2
+ import { assign } from "lodash";
3
3
 
4
- export default function expandOrContractCircularRangeToPosition(range, position, maxLength) {
5
-
6
- // 0 1 2 3 4 5 6 7 8 9
7
- // r r r r r - - r r r
8
- //0 1 2 3 4 5 6 7 8 9 10
9
- // |
10
- const newRange = assign({}, range);
11
- let endMoved = true;
12
- if (range.end >= position) {
13
- if (position + maxLength - range.start > range.end - position) {
14
- newRange.end = normalizePositionByRangeLength(position - 1, maxLength, false);
15
- } else {
16
- newRange.start = position;
17
- endMoved = false;
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
- if (range.start < position) {
21
- if (range.end + maxLength - position > position - range.start) {
22
- newRange.start = position;
23
- endMoved = false;
24
- } else {
25
- newRange.end = position - 1;
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
- return ({
38
- newRange: newRange,
39
- endMoved: endMoved
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";
2
2
 
3
- export default function expandOrContractNonCircularRangeToPosition (range, position) {
4
- const newRange = assign({},range);
5
- let endMoved = true;
6
- if (range.start > position) {
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
- } else {
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
- return ({
22
- newRange: newRange,
23
- endMoved: endMoved
24
- })
25
- };
23
+ }
24
+ return {
25
+ newRange: newRange,
26
+ endMoved: endMoved
27
+ };
28
+ }
@@ -1,12 +1,17 @@
1
- import {clone} from 'lodash';
2
- import normalizeRange from './normalizeRange';
1
+ import { clone } from "lodash";
2
+ import normalizeRange from "./normalizeRange";
3
3
 
4
- export default function expandOrContractRangeByLength(range, shiftBy, shiftStart, sequenceLength) {
5
- const rangeToReturn = clone(range);
6
- if (shiftStart) {
7
- rangeToReturn.start -=shiftBy
8
- } else {
9
- rangeToReturn.end +=shiftBy
10
- }
11
- return normalizeRange(rangeToReturn,sequenceLength)
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 './expandOrContractRangeByLength';
2
- import chai from 'chai';
3
- const expect = chai.expect;
1
+ import expandOrContractRangeByLength from "./expandOrContractRangeByLength";
2
+ import chai from "chai";
4
3
  chai.should();
5
4
 
6
- describe('expandOrContractRangeByLength', function () {
7
- it('shift start by 1 ', function () {
8
- const expandedRange = expandOrContractRangeByLength({start: 3,end:4}, 1, true, 10);
9
- expandedRange.should.deep.equal({
10
- start: 2,
11
- end: 4
12
- })
13
- });
14
- it('shift end by 1 ', function () {
15
- const expandedRange = expandOrContractRangeByLength({start: 3,end:4}, 1, false, 10);
16
- expandedRange.should.deep.equal({
17
- start: 3,
18
- end: 5
19
- })
20
- });
21
- it('shift end by 6 ', function () {
22
- const expandedRange = expandOrContractRangeByLength({start: 3,end:4}, 6, false, 10);
23
- expandedRange.should.deep.equal({
24
- start: 3,
25
- end: 0
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
- it('circular range', function () {
30
- const expandedRange = expandOrContractRangeByLength({start: 6,end:4}, 1, false, 10);
31
- expandedRange.should.deep.equal({
32
- start: 6,
33
- end: 5
34
- })
35
- });
36
- it('circular range', function () {
37
- const expandedRange = expandOrContractRangeByLength({start: 6,end:4}, 1, true, 10);
38
- expandedRange.should.deep.equal({
39
- start: 5,
40
- end: 4
41
- })
42
- });
43
- it('circular range', function () {
44
- const expandedRange = expandOrContractRangeByLength({start: 6,end:4}, 1, true, 10);
45
- expandedRange.should.deep.equal({
46
- start: 5,
47
- end: 4
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
- it('negative shiftBy', function () {
52
- const expandedRange = expandOrContractRangeByLength({start: 6,end:4}, -1, true, 10);
53
- expandedRange.should.deep.equal({
54
- start: 7,
55
- end: 4
56
- })
57
- });
58
- it('negative shiftBy', function () {
59
- const expandedRange = expandOrContractRangeByLength({start: 6,end:4}, -1, false, 10);
60
- expandedRange.should.deep.equal({
61
- start: 6,
62
- end: 3
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
- // it('should error if trying to expand more than possible', function () {
67
- // var error = false;
68
- // try {
69
- // var range = expandOrContractRangeByLength({start: 6,end:4}, 10, false, 10)
70
- // console.log('range:', range)
71
- // } catch (e) {
72
- // error = true;
73
- // }
74
- // expect(error).to.be.true;
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 './expandOrContractCircularRangeToPosition';
2
- import expandOrContractNonCircularRangeToPosition from './expandOrContractNonCircularRangeToPosition';
1
+ import expandOrContractCircularRangeToPosition from "./expandOrContractCircularRangeToPosition";
2
+ import expandOrContractNonCircularRangeToPosition from "./expandOrContractNonCircularRangeToPosition";
3
3
 
4
- export default function expandOrContractRangeToPosition(range, position, maxLength) {
5
- if (range.start > range.end) {
6
- return expandOrContractCircularRangeToPosition(range, position, maxLength)
7
- } else {
8
- return expandOrContractNonCircularRangeToPosition(range, position, maxLength)
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
+ }