OffsetChecker.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import Utils from "../Utils";
  8. import OperationError from "../errors/OperationError";
  9. /**
  10. * Offset checker operation
  11. */
  12. class OffsetChecker extends Operation {
  13. /**
  14. * OffsetChecker constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "Offset checker";
  19. this.module = "Default";
  20. this.description = "Compares multiple inputs (separated by the specified delimiter) and highlights matching characters which appear at the same position in all samples.";
  21. this.inputType = "string";
  22. this.outputType = "html";
  23. this.args = [
  24. {
  25. "name": "Sample delimiter",
  26. "type": "binaryString",
  27. "value": "\\n\\n"
  28. }
  29. ];
  30. }
  31. /**
  32. * @param {string} input
  33. * @param {Object[]} args
  34. * @returns {html}
  35. */
  36. run(input, args) {
  37. const sampleDelim = args[0],
  38. samples = input.split(sampleDelim),
  39. outputs = new Array(samples.length);
  40. let i = 0,
  41. s = 0,
  42. match = false,
  43. inMatch = false,
  44. chr;
  45. if (!samples || samples.length < 2) {
  46. throw new OperationError("Not enough samples, perhaps you need to modify the sample delimiter or add more data?");
  47. }
  48. // Initialise output strings
  49. outputs.fill("", 0, samples.length);
  50. // Loop through each character in the first sample
  51. for (i = 0; i < samples[0].length; i++) {
  52. chr = samples[0][i];
  53. match = false;
  54. // Loop through each sample to see if the chars are the same
  55. for (s = 1; s < samples.length; s++) {
  56. if (samples[s][i] !== chr) {
  57. match = false;
  58. break;
  59. }
  60. match = true;
  61. }
  62. // Write output for each sample
  63. for (s = 0; s < samples.length; s++) {
  64. if (samples[s].length <= i) {
  65. if (inMatch) outputs[s] += "</span>";
  66. if (s === samples.length - 1) inMatch = false;
  67. continue;
  68. }
  69. if (match && !inMatch) {
  70. outputs[s] += "<span class='hl5'>" + Utils.escapeHtml(samples[s][i]);
  71. if (samples[s].length === i + 1) outputs[s] += "</span>";
  72. if (s === samples.length - 1) inMatch = true;
  73. } else if (!match && inMatch) {
  74. outputs[s] += "</span>" + Utils.escapeHtml(samples[s][i]);
  75. if (s === samples.length - 1) inMatch = false;
  76. } else {
  77. outputs[s] += Utils.escapeHtml(samples[s][i]);
  78. if (inMatch && samples[s].length === i + 1) {
  79. outputs[s] += "</span>";
  80. if (samples[s].length - 1 !== i) inMatch = false;
  81. }
  82. }
  83. if (samples[0].length - 1 === i) {
  84. if (inMatch) outputs[s] += "</span>";
  85. outputs[s] += Utils.escapeHtml(samples[s].substring(i + 1));
  86. }
  87. }
  88. }
  89. return outputs.join(sampleDelim);
  90. }
  91. }
  92. export default OffsetChecker;