Skip to content

Instantly share code, notes, and snippets.

@bas080
Last active November 26, 2024 13:24
Show Gist options
  • Save bas080/62dde4d6853f7e8c970ae1e9d3da798f to your computer and use it in GitHub Desktop.
Save bas080/62dde4d6853f7e8c970ae1e9d3da798f to your computer and use it in GitHub Desktop.
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