Skip to content

Commit

Permalink
Merge pull request #84 from TehShrike/old-array-merge-example
Browse files Browse the repository at this point in the history
Fix the oldArrayMerge example
  • Loading branch information
TehShrike committed Nov 1, 2017
2 parents dd7f212 + 6b57cc9 commit 18fd679
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,32 @@ output // => { coolThing: ['a', 'b', 'c'] }
To use the old (pre-version-2.0.0) array merging algorithm, pass in this function:

```js
const isMergeableObject = require('is-mergeable-object')
const emptyTarget = value => Array.isArray(value) ? [] : {}
const clone = (value, options) => merge(emptyTarget(value), value, options)

function oldArrayMerge(target, source, optionsArgument) {
var destination = target.slice()
const destination = target.slice()

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

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

#### clone
Expand Down

0 comments on commit 18fd679

Please sign in to comment.