Skip to content

Commit

Permalink
don't change functions to objects
Browse files Browse the repository at this point in the history
This commit fixes issue #2
  • Loading branch information
Istador committed May 8, 2022
1 parent 61de85d commit 29d8fc5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import mapValues from 'lodash-es/mapValues.js'
import mergeWith from 'lodash-es/mergeWith.js'
import isObject from 'lodash-es/isObject.js'

// marker object to return undefined from mergeWith, without it triggering the default merge algorithm
const UNDEFINED = new Object()
Expand All @@ -13,7 +12,7 @@ const toUndefined = v => (
Array.isArray(v)
? v.map(toUndefined)
: (
isObject(v)
typeof v === 'object'
? mapValues(v, toUndefined)
: v
)
Expand Down
19 changes: 19 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ describe('merge', () => {
expect(merge({ a: 1 }, { a: [ undefined ] })).toEqual({ a: [ undefined ] })
})

it('keep function', () => {
const f = () => {}
expect({ f, a: 'a' }).toEqual(merge({ f }, { a: 'a' }))
})

it('replace function w/ function', () => {
const f1 = () => {}
const f2 = () => {}
expect(merge({ f: f1 }, { f: f2 })).toEqual({ f: f2 })
})

it('replace function w/ value', () => {
expect(merge({ f: () => {} }, { f: 2 })).toEqual({ f: 2 })
})

it('replace function w/ undefined', () => {
expect(merge({ f: () => {} }, { f: undefined })).toEqual({ f: undefined })
})

it('extended example', () => {
const a = { a: 1, a1: 1, a2: undefined, b: 1, c: 1, arr: [ 1, 1, 1 ], obj: { x: 1 } }
const b = { b: 2, b1: 2, b2: undefined, arr: [ 2, 2 ], obj: { x: 2 } }
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lodash-es-merge-undefined",
"version": "1.0.3",
"version": "1.0.4",
"description": "a version of merge that does overwrite undefined values",
"type": "module",
"jsnext:main": "index.js",
Expand Down

0 comments on commit 29d8fc5

Please sign in to comment.