compare.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { FunctionCov, RangeCov, ScriptCov } from "./types";
  2. /**
  3. * Compares two script coverages.
  4. *
  5. * The result corresponds to the comparison of their `url` value (alphabetical sort).
  6. */
  7. export function compareScriptCovs(a: Readonly<ScriptCov>, b: Readonly<ScriptCov>): number {
  8. if (a.url === b.url) {
  9. return 0;
  10. } else if (a.url < b.url) {
  11. return -1;
  12. } else {
  13. return 1;
  14. }
  15. }
  16. /**
  17. * Compares two function coverages.
  18. *
  19. * The result corresponds to the comparison of the root ranges.
  20. */
  21. export function compareFunctionCovs(a: Readonly<FunctionCov>, b: Readonly<FunctionCov>): number {
  22. return compareRangeCovs(a.ranges[0], b.ranges[0]);
  23. }
  24. /**
  25. * Compares two range coverages.
  26. *
  27. * The ranges are first ordered by ascending `startOffset` and then by
  28. * descending `endOffset`.
  29. * This corresponds to a pre-order tree traversal.
  30. */
  31. export function compareRangeCovs(a: Readonly<RangeCov>, b: Readonly<RangeCov>): number {
  32. if (a.startOffset !== b.startOffset) {
  33. return a.startOffset - b.startOffset;
  34. } else {
  35. return b.endOffset - a.endOffset;
  36. }
  37. }