normalize.mjs 7.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { compareFunctionCovs, compareRangeCovs, compareScriptCovs } from "./compare";
  2. import { RangeTree } from "./range-tree";
  3. /**
  4. * Normalizes a process coverage.
  5. *
  6. * Sorts the scripts alphabetically by `url`.
  7. * Reassigns script ids: the script at index `0` receives `"0"`, the script at
  8. * index `1` receives `"1"` etc.
  9. * This does not normalize the script coverages.
  10. *
  11. * @param processCov Process coverage to normalize.
  12. */
  13. export function normalizeProcessCov(processCov) {
  14. processCov.result.sort(compareScriptCovs);
  15. for (const [scriptId, scriptCov] of processCov.result.entries()) {
  16. scriptCov.scriptId = scriptId.toString(10);
  17. }
  18. }
  19. /**
  20. * Normalizes a process coverage deeply.
  21. *
  22. * Normalizes the script coverages deeply, then normalizes the process coverage
  23. * itself.
  24. *
  25. * @param processCov Process coverage to normalize.
  26. */
  27. export function deepNormalizeProcessCov(processCov) {
  28. for (const scriptCov of processCov.result) {
  29. deepNormalizeScriptCov(scriptCov);
  30. }
  31. normalizeProcessCov(processCov);
  32. }
  33. /**
  34. * Normalizes a script coverage.
  35. *
  36. * Sorts the function by root range (pre-order sort).
  37. * This does not normalize the function coverages.
  38. *
  39. * @param scriptCov Script coverage to normalize.
  40. */
  41. export function normalizeScriptCov(scriptCov) {
  42. scriptCov.functions.sort(compareFunctionCovs);
  43. }
  44. /**
  45. * Normalizes a script coverage deeply.
  46. *
  47. * Normalizes the function coverages deeply, then normalizes the script coverage
  48. * itself.
  49. *
  50. * @param scriptCov Script coverage to normalize.
  51. */
  52. export function deepNormalizeScriptCov(scriptCov) {
  53. for (const funcCov of scriptCov.functions) {
  54. normalizeFunctionCov(funcCov);
  55. }
  56. normalizeScriptCov(scriptCov);
  57. }
  58. /**
  59. * Normalizes a function coverage.
  60. *
  61. * Sorts the ranges (pre-order sort).
  62. * TODO: Tree-based normalization of the ranges.
  63. *
  64. * @param funcCov Function coverage to normalize.
  65. */
  66. export function normalizeFunctionCov(funcCov) {
  67. funcCov.ranges.sort(compareRangeCovs);
  68. const tree = RangeTree.fromSortedRanges(funcCov.ranges);
  69. normalizeRangeTree(tree);
  70. funcCov.ranges = tree.toRanges();
  71. }
  72. /**
  73. * @internal
  74. */
  75. export function normalizeRangeTree(tree) {
  76. tree.normalize();
  77. }
  78. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMvbm9ybWFsaXplLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxtQkFBbUIsRUFBRSxnQkFBZ0IsRUFBRSxpQkFBaUIsRUFBRSxNQUFNLFdBQVcsQ0FBQztBQUNyRixPQUFPLEVBQUUsU0FBUyxFQUFFLE1BQU0sY0FBYyxDQUFDO0FBR3pDOzs7Ozs7Ozs7R0FTRztBQUNILE1BQU0sVUFBVSxtQkFBbUIsQ0FBQyxVQUFzQjtJQUN4RCxVQUFVLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxpQkFBaUIsQ0FBQyxDQUFDO0lBQzFDLEtBQUssTUFBTSxDQUFDLFFBQVEsRUFBRSxTQUFTLENBQUMsSUFBSSxVQUFVLENBQUMsTUFBTSxDQUFDLE9BQU8sRUFBRSxFQUFFO1FBQy9ELFNBQVMsQ0FBQyxRQUFRLEdBQUcsUUFBUSxDQUFDLFFBQVEsQ0FBQyxFQUFFLENBQUMsQ0FBQztLQUM1QztBQUNILENBQUM7QUFFRDs7Ozs7OztHQU9HO0FBQ0gsTUFBTSxVQUFVLHVCQUF1QixDQUFDLFVBQXNCO0lBQzVELEtBQUssTUFBTSxTQUFTLElBQUksVUFBVSxDQUFDLE1BQU0sRUFBRTtRQUN6QyxzQkFBc0IsQ0FBQyxTQUFTLENBQUMsQ0FBQztLQUNuQztJQUNELG1CQUFtQixDQUFDLFVBQVUsQ0FBQyxDQUFDO0FBQ2xDLENBQUM7QUFFRD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