UNPKG

@amplitude/ampli

Version:

Amplitude CLI

62 lines (61 loc) 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveMergeConflicts = exports.hasMergeConflicts = void 0; const conflictStartRegex = RegExp(`^<<<<<<< HEAD$`, 'm'); const conflictSplitterRegex = RegExp(`^=======$`, 'm'); const conflictEndRegex = RegExp(`^>>>>>>> .+$`, 'm'); function hasMergeConflicts(text) { return [conflictStartRegex, conflictSplitterRegex, conflictEndRegex].every(r => r.test(text)); } exports.hasMergeConflicts = hasMergeConflicts; function resolveMergeConflicts(text) { let currentText = text; const ourParts = []; const theirParts = []; while (currentText) { const startMatch = conflictStartRegex.exec(currentText); const splitterMatch = conflictSplitterRegex.exec(currentText); const endMatch = conflictEndRegex.exec(currentText); if (!startMatch || !splitterMatch || !endMatch) { ourParts.push(currentText); theirParts.push(currentText); break; } const { startIndex: startFromIndex, endIndex: startToIndex } = expandTrailingNewLines(currentText, startMatch.index, startMatch.index + startMatch[0].length - 1); const { startIndex: splitterFromIndex, endIndex: splitterToIndex } = expandTrailingNewLines(currentText, splitterMatch.index, splitterMatch.index + splitterMatch[0].length - 1, startFromIndex === 0); const { startIndex: endFromIndex, endIndex: endToIndex } = expandTrailingNewLines(currentText, endMatch.index, endMatch.index + endMatch[0].length - 1); const commonPart = currentText.substring(0, startFromIndex); ourParts.push(commonPart); ourParts.push(currentText.substring(startToIndex + 1, splitterFromIndex)); theirParts.push(commonPart); theirParts.push(currentText.substring(splitterToIndex + 1, endFromIndex)); currentText = currentText.substring(endToIndex + 1); } return { ours: ourParts.join(''), theirs: theirParts.join(''), }; } exports.resolveMergeConflicts = resolveMergeConflicts; function expandTrailingNewLines(text, startIndex, endIndex, expandEnd = false) { let start = startIndex; if (start >= 2 && text.substr(start - 2, 2) === '\r\n') { start -= 2; } else if (start >= 1 && (text.substr(start - 1, 1) === '\n' || text.substr(start - 1, 1) === '\r')) { start -= 1; } let end = endIndex; if (start === 0 || expandEnd) { if (end < text.length - 2 && text.substr(end + 1, 2) === '\r\n') { end += 2; } else if (end < text.length - 1 && (text.substr(end + 1, 1) === '\n' || text.substr(end + 1, 1) === '\r')) { end += 1; } } return { startIndex: start, endIndex: end, }; }