Last active
November 26, 2024 13:24
-
-
Save bas080/62dde4d6853f7e8c970ae1e9d3da798f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ConflictSymbol = Symbol('conflict') | |
const isEmpty = x => x.length === 0 | |
const conflicts = x => x[ConflictSymbol] | |
const hasConflicts = x => !isEmpty(x[ConflictSymbol]) | |
const mergeArrays = (local, working, remote) => { | |
const merged = []; | |
merged[ConflictSymbol] = [] | |
remote.forEach((remoteValue, index) => { | |
const localValue = local[index] | |
const workingValue = working[index]; | |
if (localValue !== remoteValue && workingValue !== localValue) { | |
merged[ConflictSymbol] = [...(merged[ConflictSymbol]), { | |
index: index, | |
local: localValue, | |
working: workingValue, | |
remote: remoteValue | |
}] | |
} | |
// Merge: Prefer the working value if it's different from local, otherwise take remote | |
merged[index] = (localValue === remoteValue) ? workingValue : remoteValue; | |
}) | |
return merged | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment