File: html-differ.js

package info (click to toggle)
node-marked 0.8.0%2Bds%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,948 kB
  • sloc: javascript: 3,640; sh: 16; makefile: 16
file content (37 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
const htmlDiffer = new HtmlDiffer({ ignoreSelfClosingSlash: true });

module.exports = {
  isEqual: htmlDiffer.isEqual.bind(htmlDiffer),
  firstDiff: async(actual, expected, padding) => {
    padding = padding || 30;
    const diffHtml = await htmlDiffer.diffHtml(actual, expected);
    const result = diffHtml.reduce((obj, diff) => {
      if (diff.added) {
        if (obj.firstIndex === null) {
          obj.firstIndex = obj.expected.length;
        }
        obj.expected += diff.value;
      } else if (diff.removed) {
        if (obj.firstIndex === null) {
          obj.firstIndex = obj.actual.length;
        }
        obj.actual += diff.value;
      } else {
        obj.actual += diff.value;
        obj.expected += diff.value;
      }

      return obj;
    }, {
      firstIndex: null,
      actual: '',
      expected: ''
    });

    return {
      actual: result.actual.substring(result.firstIndex - padding, result.firstIndex + padding),
      expected: result.expected.substring(result.firstIndex - padding, result.firstIndex + padding)
    };
  }
};