range.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export var Range;
  6. (function (Range) {
  7. /**
  8. * Returns the intersection between two ranges as a range itself.
  9. * Returns `{ start: 0, end: 0 }` if the intersection is empty.
  10. */
  11. function intersect(one, other) {
  12. if (one.start >= other.end || other.start >= one.end) {
  13. return { start: 0, end: 0 };
  14. }
  15. const start = Math.max(one.start, other.start);
  16. const end = Math.min(one.end, other.end);
  17. if (end - start <= 0) {
  18. return { start: 0, end: 0 };
  19. }
  20. return { start, end };
  21. }
  22. Range.intersect = intersect;
  23. function isEmpty(range) {
  24. return range.end - range.start <= 0;
  25. }
  26. Range.isEmpty = isEmpty;
  27. function intersects(one, other) {
  28. return !isEmpty(intersect(one, other));
  29. }
  30. Range.intersects = intersects;
  31. function relativeComplement(one, other) {
  32. const result = [];
  33. const first = { start: one.start, end: Math.min(other.start, one.end) };
  34. const second = { start: Math.max(other.end, one.start), end: one.end };
  35. if (!isEmpty(first)) {
  36. result.push(first);
  37. }
  38. if (!isEmpty(second)) {
  39. result.push(second);
  40. }
  41. return result;
  42. }
  43. Range.relativeComplement = relativeComplement;
  44. })(Range || (Range = {}));