Skip to content

Commit

Permalink
fix: fix comparison between two values ​​that are not objects
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsalles committed May 24, 2021
1 parent 2d066a4 commit d1fd723
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
5 changes: 1 addition & 4 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import perform from './perform'
import wrap from './wrap'

export default function diff (original, derived) {
return perform(
wrap(typeof original === 'object' ? original : {}),
wrap(typeof derived === 'object' ? derived : {})
)
return perform(wrap(original), wrap(derived))
}
4 changes: 4 additions & 0 deletions src/perform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import getProperties from './getProperties'
import heap from './heap'

export default function perform (originalWrapped, derivedWrapped) {
if (originalWrapped.type !== 'object' && derivedWrapped.type !== 'object') {
return [getChange(originalWrapped, derivedWrapped)]
}

const properties = getProperties(originalWrapped.value, derivedWrapped.value)

return properties.reduce((result, property) => {
Expand Down
22 changes: 0 additions & 22 deletions test/diff.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,4 @@ describe('diff', () => {
expect(wrap.mock.calls).toEqual([[original], [derived]])
expect(perform).toBeCalledWith(originalWrapped, derivedWrapped)
})

describe('when "original" isn`t an object', () => {
it('calls the respective "wrap" with an empty object', () => {
const original = 'original'
const derived = {}

diff(original, derived)

expect(wrap.mock.calls).toEqual([[{}], [derived]])
})
})

describe('when "derived" isn`t an object', () => {
it('calls the respective "wrap" with an empty object', () => {
const original = {}
const derived = 'derived'

diff(original, derived)

expect(wrap.mock.calls).toEqual([[original], [{}]])
})
})
})
16 changes: 15 additions & 1 deletion test/perform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ describe('perform', () => {
anotherProperty: 'another value'
}
]
]
],
/* 16 */
['value', 'different value']
].map(objects => objects.map(object => wrap(object)))

const expectations = [
Expand Down Expand Up @@ -385,6 +387,18 @@ describe('perform', () => {
},
previous: 'other value'
}
],
/* 16 */
[
{
type: 'change',
path: {
string: '',
chain: []
},
previous: 'value',
value: 'different value'
}
]
]

Expand Down

0 comments on commit d1fd723

Please sign in to comment.