Skip to content

Commit

Permalink
Fix the old array merge algorithm to not try to clone when it shouldn't
Browse files Browse the repository at this point in the history
  • Loading branch information
TehShrike committed Nov 1, 2017
1 parent a6a41cf commit e5ad65f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,27 @@ const emptyTarget = value => Array.isArray(value) ? [] : {}
const clone = (value, options) => merge(emptyTarget(value), value, options)

function oldArrayMerge(target, source, optionsArgument) {
const shouldClone = !optionsArgument || optionsArgument.clone !== false
const destination = target.slice()

source.forEach(function(e, i) {
if (typeof destination[i] === 'undefined') {
const cloneRequested = !optionsArgument || optionsArgument.clone !== false
const shouldClone = cloneRequested && isMergeableObject(e)
destination[i] = shouldClone ? clone(e, optionsArgument) : e
} else if (isMergeableObject(e)) {
destination[i] = merge(target[i], e, optionsArgument)
} else if (target.indexOf(e) === -1) {
destination.push(shouldClone ? clone(e, optionsArgument) : e)
destination.push(e)
}
})
return destination
}

merge(
[{ a: true }],
[{ b: true}, {c: true}],
[{ b: true}, 'ah yup'],
{ arrayMerge: oldArrayMerge }
) // => [{ a: true, b: true }, { c: true }]
) // => [{ a: true, b: true }, 'ah yup']
```

#### clone
Expand Down

0 comments on commit e5ad65f

Please sign in to comment.