codekingpro/portable-devtools
115k
1// Iterator that traverses in the range of [min, max], stepping
2// by distance from a given start position. I.e. for [0, 4], with
3// start of 2, this will iterate 2, 3, 1, 4, 0.
4export default function (start, minLine, maxLine) {
5 let wantForward = true, backwardExhausted = false, forwardExhausted = false, localOffset = 1;
6 return function iterator() {
7 if (wantForward && !forwardExhausted) {
8 if (backwardExhausted) {
9 localOffset++;
10 }
11 else {
12 wantForward = false;
13 }
14 // Check if trying to fit beyond text length, and if not, check it fits
15 // after offset location (or desired location on first iteration)
16 if (start + localOffset <= maxLine) {
17 return start + localOffset;
18 }
19 forwardExhausted = true;
20 }
21 if (!backwardExhausted) {
22 if (!forwardExhausted) {
23 wantForward = true;
24 }
25 // Check if trying to fit before text beginning, and if not, check it fits
26 // before offset location
27 if (minLine <= start - localOffset) {
28 return start - localOffset++;
29 }
30 backwardExhausted = true;
31 return iterator();
32 }
33 // We tried to fit hunk before text beginning and beyond text length, then
34 // hunk can't fit on the text. Return undefined
35 return undefined;
36 };
37}
38 