"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 |