summaryrefslogtreecommitdiff
path: root/desktop/node_modules/dir-compare/build/src/fileCompareHandler/lines/compare/compareLines.js
blob: 132a71ef77f9326fa35311529cbb0912b6152e37 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareLines = void 0;
const TRIM_WHITE_SPACES_REGEXP = /^[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+|[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+$/g;
const TRIM_LINE_ENDING_REGEXP = /\r\n|\n$/g;
const REMOVE_WHITE_SPACES_REGEXP = /[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/g;
function compareLines(lines1, lines2, options) {
    if (options.ignoreEmptyLines) {
        lines1 = removeEmptyLines(lines1);
        lines2 = removeEmptyLines(lines2);
    }
    const len = Math.min(lines1.length, lines2.length);
    let i = 0;
    for (; i < len; i++) {
        const isEqual = compareLine(options, lines1[i], lines2[i]);
        if (!isEqual) {
            return { isEqual: false, restLines1: [], restLines2: [] };
        }
    }
    return {
        isEqual: true,
        restLines1: lines1.slice(i),
        restLines2: lines2.slice(i)
    };
}
exports.compareLines = compareLines;
function compareLine(options, line1, line2) {
    if (options.ignoreLineEnding) {
        line1 = trimLineEnding(line1);
        line2 = trimLineEnding(line2);
    }
    if (options.ignoreWhiteSpaces) {
        line1 = trimSpaces(line1);
        line2 = trimSpaces(line2);
    }
    if (options.ignoreAllWhiteSpaces) {
        line1 = removeSpaces(line1);
        line2 = removeSpaces(line2);
    }
    return line1 === line2;
}
// Trims string like '   abc   \n' into 'abc\n'
function trimSpaces(s) {
    const { content, lineEnding } = separateEol(s);
    const trimmed = content.replace(TRIM_WHITE_SPACES_REGEXP, '');
    return trimmed + lineEnding;
}
function trimLineEnding(s) {
    return s.replace(TRIM_LINE_ENDING_REGEXP, '');
}
function removeSpaces(s) {
    return s.replace(REMOVE_WHITE_SPACES_REGEXP, '');
}
function removeEmptyLines(lines) {
    return lines.filter(line => !isEmptyLine(line));
}
function isEmptyLine(line) {
    return line === '\n' || line === '\r\n';
}
function separateEol(s) {
    const len = s.length;
    let lineEnding = '';
    let content = s;
    if (s[len - 1] === '\n') {
        if (s[len - 2] === '\r') {
            return {
                lineEnding: '\r\n',
                content: s.slice(0, len - 2)
            };
        }
        {
            lineEnding = '\n';
            content = s.slice(0, len - 1);
        }
    }
    return { content, lineEnding };
}
//# sourceMappingURL=compareLines.js.map