All files / dist handleMergeError.js

0% Statements 0/51
0% Branches 0/37
0% Functions 0/3
0% Lines 0/51
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function handleMergeError(err, target, offendingKey, message) {
    // Rethrow if:
    // - offending key already exists on target
    // - object not sealed
    // - is extensible
    // - error not a TypeError
    if (Object.hasOwnProperty.call(target, offendingKey) ||
        !Object.isSealed(target) ||
        Object.isExtensible(target) ||
        !(err instanceof TypeError))
        throw err;
    var offendingKeyLower = offendingKey.toLowerCase();
    // Iterate through keys in target
    // For each key, compare with the offending key
    var bestMatch = Object.keys(target).reduce(function (currBestMatch, currKey) {
        var totalMatching = getTotalMatching(currKey.toLowerCase(), offendingKeyLower);
        var delta = Math.abs(currKey.length - offendingKey.length);
        if (totalMatching > currBestMatch.totalMatching ||
            (totalMatching === currBestMatch.totalMatching && delta < currBestMatch.delta)) {
            // If a greater number of matching characters, or the same
            // number, but a lesser delta, usurp the best match
            return { key: currKey, delta: delta, totalMatching: totalMatching };
        }
        return currBestMatch;
    }, { key: '', delta: Infinity, totalMatching: 0 });
    var suggestion = bestMatch && bestMatch.totalMatching > 1 ? bestMatch.key : '';
    throw new TypeError(message(offendingKey, suggestion));
}
/**
 * Returns the number of common, consecutive characters
 * between two strings.
 */
function getTotalMatching(possibleKey, offendingKey) {
    var longer = possibleKey.length > offendingKey.length ? possibleKey : offendingKey;
    var shorter = longer === possibleKey ? offendingKey : possibleKey;
    var leftPointer = 0;
    var leftInnerPointer = 0;
    var leftTotalMatching = 0;
    var lastCommonIndex = -1;
    for (; leftPointer < longer.length; leftPointer++) {
        while (leftTotalMatching === 0 &&
            longer[leftPointer] !== shorter[leftInnerPointer] &&
            leftInnerPointer < shorter.length) {
            // No match at present, move innerPointer through all possible
            // indices until a match is found
            leftInnerPointer++;
        }
        if (longer[leftPointer] === shorter[leftInnerPointer]) {
            // Match found
            if (lastCommonIndex !== leftPointer - 1) {
                // If beginning of a new match, reset total common
                leftTotalMatching = 0;
            }
            lastCommonIndex = leftPointer;
            leftTotalMatching++;
            leftInnerPointer++;
        }
        else if (leftTotalMatching > 1) {
            // No match, but at least two common characters found, end
            break;
        }
        else {
            // No match at this index, reset
            leftTotalMatching = leftInnerPointer = 0;
        }
    }
    lastCommonIndex = -1;
    var rightPointer = 0;
    var rightInnerPointer = 0;
    var rightTotalMatching = 0;
    var longerLastIndex = longer.length - 1;
    var shorterLastIndex = shorter.length - 1;
    // As above, but from right to left
    for (; rightPointer < longer.length - leftPointer; rightPointer++) {
        while (rightTotalMatching === 0 &&
            longer[longerLastIndex - rightPointer] !== shorter[shorterLastIndex - rightInnerPointer] &&
            rightInnerPointer < shorter.length) {
            rightInnerPointer++;
        }
        if (longer[longerLastIndex - rightPointer] === shorter[shorterLastIndex - rightInnerPointer]) {
            if (lastCommonIndex !== rightPointer - 1)
                rightTotalMatching = 0;
            lastCommonIndex = rightPointer;
            rightTotalMatching++;
            rightInnerPointer++;
        }
        else if (rightTotalMatching > 1) {
            break;
        }
        else {
            rightTotalMatching = rightInnerPointer = 0;
        }
    }
    return leftTotalMatching + rightTotalMatching;
}
exports.getTotalMatching = getTotalMatching;
exports.default = handleMergeError;
//# sourceMappingURL=handleMergeError.js.map