distance-iterator.js 1.5 KB

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