clone.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { FunctionCov, ProcessCov, RangeCov, ScriptCov } from "./types";
  2. /**
  3. * Creates a deep copy of a process coverage.
  4. *
  5. * @param processCov Process coverage to clone.
  6. * @return Cloned process coverage.
  7. */
  8. export function cloneProcessCov(processCov: Readonly<ProcessCov>): ProcessCov {
  9. const result: ScriptCov[] = [];
  10. for (const scriptCov of processCov.result) {
  11. result.push(cloneScriptCov(scriptCov));
  12. }
  13. return {
  14. result,
  15. };
  16. }
  17. /**
  18. * Creates a deep copy of a script coverage.
  19. *
  20. * @param scriptCov Script coverage to clone.
  21. * @return Cloned script coverage.
  22. */
  23. export function cloneScriptCov(scriptCov: Readonly<ScriptCov>): ScriptCov {
  24. const functions: FunctionCov[] = [];
  25. for (const functionCov of scriptCov.functions) {
  26. functions.push(cloneFunctionCov(functionCov));
  27. }
  28. return {
  29. scriptId: scriptCov.scriptId,
  30. url: scriptCov.url,
  31. functions,
  32. };
  33. }
  34. /**
  35. * Creates a deep copy of a function coverage.
  36. *
  37. * @param functionCov Function coverage to clone.
  38. * @return Cloned function coverage.
  39. */
  40. export function cloneFunctionCov(functionCov: Readonly<FunctionCov>): FunctionCov {
  41. const ranges: RangeCov[] = [];
  42. for (const rangeCov of functionCov.ranges) {
  43. ranges.push(cloneRangeCov(rangeCov));
  44. }
  45. return {
  46. functionName: functionCov.functionName,
  47. ranges,
  48. isBlockCoverage: functionCov.isBlockCoverage,
  49. };
  50. }
  51. /**
  52. * Creates a deep copy of a function coverage.
  53. *
  54. * @param rangeCov Range coverage to clone.
  55. * @return Cloned range coverage.
  56. */
  57. export function cloneRangeCov(rangeCov: Readonly<RangeCov>): RangeCov {
  58. return {
  59. startOffset: rangeCov.startOffset,
  60. endOffset: rangeCov.endOffset,
  61. count: rangeCov.count,
  62. };
  63. }