Challenge:
Resolve git conflict by resolveGitConflict(linesArray, resolveMode)
, where
linesArray
is an array of strings
Example:
[
"context1",
"context2",
"<<<<<<<",
"my changes1",
"=======",
"their changes1",
">>>>>>>",
"context3",
"context4",
"<<<<<<<",
"my changes2",
"=======",
"their changes2",
">>>>>>>",
"context5",
"context6"
]
resolveMode - a string indicate:
- m: Keep only current branch's changes
- t: Keep only other branch's changes
- tm: Keep both changes, but put other branch's changes before current branch's changes
- mt - Keep both changes, but put other branch's changes after current branch's changes
- none - Keep no changes
Review
We have three solutions for that challenge. Please compare all of them and tell which one is better.
Solution 1
const resolveGitConflict = (linesArray, resolveMode, t = []) =>
linesArray.reduce((p, c) => p.concat(
t.length ?
c === '>>>>>>>' ?
(c = [].concat(resolveMode[0] ? t.splice(+(resolveMode[0] === 't'), 1)[0].concat(resolveMode[1] ? t[0] : []) : [])) &&
(t = []) && c
: c === '=======' ? t[1] = [] : t[+!!t[1]].push(c) && []
: c === '<<<<<<<' ? t[0] = [] : [c]
), [])
Solution 2
function resolveGitConflict(linesArray, mode) {
var [changes, curMode] = [{m: [], t: []}, ''];
return linesArray.map(line => {
var newLines = [];
if (line === '<<<<<<<' || line === '=======')
curMode = line === '<<<<<<<'? 'm' : 't';
else if (line === '>>>>>>>') {
newLines = Array.from(mode).map(c => changes[c] || []).flat(1);
[changes.m, changes.t, curMode] = [[], [], ''];
} else
(changes[curMode] || newLines).push(line);
return newLines;
}).flat(1);
}
Solution 3
function resolveGitConflict(lines, mode) {
return lines.reduce((p, c) => {
let last = p[p.length-1]
if(typeof last === 'object' ) {
if(c === '>>>>>>>') last = [].concat(last[mode[0]] || [], last[mode[1]] || [])
else if(c === '=======') last.t = []
else last[last.t ? 't' : 'm'].push(c)
return p.slice(0, -1).concat(last)
} else if(c === '<<<<<<<') c = {m: []}
return p.concat(c)
}, [])
}