Diff.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import Utils from "../Utils.mjs";
  8. import * as JsDiff from "diff";
  9. import OperationError from "../errors/OperationError.mjs";
  10. /**
  11. * Diff operation
  12. */
  13. class Diff extends Operation {
  14. /**
  15. * Diff constructor
  16. */
  17. constructor() {
  18. super();
  19. this.name = "Diff";
  20. this.module = "Diff";
  21. this.description = "Compares two inputs (separated by the specified delimiter) and highlights the differences between them.";
  22. this.infoURL = "https://wikipedia.org/wiki/File_comparison";
  23. this.inputType = "string";
  24. this.outputType = "html";
  25. this.args = [
  26. {
  27. "name": "Sample delimiter",
  28. "type": "binaryString",
  29. "value": "\\n\\n"
  30. },
  31. {
  32. "name": "Diff by",
  33. "type": "option",
  34. "value": ["Character", "Word", "Line", "Sentence", "CSS", "JSON"]
  35. },
  36. {
  37. "name": "Show added",
  38. "type": "boolean",
  39. "value": true
  40. },
  41. {
  42. "name": "Show removed",
  43. "type": "boolean",
  44. "value": true
  45. },
  46. {
  47. "name": "Show subtraction",
  48. "type": "boolean",
  49. "value": false
  50. },
  51. {
  52. "name": "Ignore whitespace",
  53. "type": "boolean",
  54. "value": false,
  55. "hint": "Relevant for word and line"
  56. }
  57. ];
  58. }
  59. /**
  60. * @param {string} input
  61. * @param {Object[]} args
  62. * @returns {html}
  63. */
  64. run(input, args) {
  65. const [
  66. sampleDelim,
  67. diffBy,
  68. showAdded,
  69. showRemoved,
  70. showSubtraction,
  71. ignoreWhitespace
  72. ] = args,
  73. samples = input.split(sampleDelim);
  74. let output = "",
  75. diff;
  76. // Node and Webpack load modules slightly differently
  77. const jsdiff = JsDiff.default ? JsDiff.default : JsDiff;
  78. if (!samples || samples.length !== 2) {
  79. throw new OperationError("Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?");
  80. }
  81. switch (diffBy) {
  82. case "Character":
  83. diff = jsdiff.diffChars(samples[0], samples[1]);
  84. break;
  85. case "Word":
  86. if (ignoreWhitespace) {
  87. diff = jsdiff.diffWords(samples[0], samples[1]);
  88. } else {
  89. diff = jsdiff.diffWordsWithSpace(samples[0], samples[1]);
  90. }
  91. break;
  92. case "Line":
  93. if (ignoreWhitespace) {
  94. diff = jsdiff.diffTrimmedLines(samples[0], samples[1]);
  95. } else {
  96. diff = jsdiff.diffLines(samples[0], samples[1]);
  97. }
  98. break;
  99. case "Sentence":
  100. diff = jsdiff.diffSentences(samples[0], samples[1]);
  101. break;
  102. case "CSS":
  103. diff = jsdiff.diffCss(samples[0], samples[1]);
  104. break;
  105. case "JSON":
  106. diff = jsdiff.diffJson(samples[0], samples[1]);
  107. break;
  108. default:
  109. throw new OperationError("Invalid 'Diff by' option.");
  110. }
  111. for (let i = 0; i < diff.length; i++) {
  112. if (diff[i].added) {
  113. if (showAdded) output += "<span class='hl5'>" + Utils.escapeHtml(diff[i].value) + "</span>";
  114. } else if (diff[i].removed) {
  115. if (showRemoved) output += "<span class='hl3'>" + Utils.escapeHtml(diff[i].value) + "</span>";
  116. } else if (!showSubtraction) {
  117. output += Utils.escapeHtml(diff[i].value);
  118. }
  119. }
  120. return output;
  121. }
  122. }
  123. export default Diff;