This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from turistikrota/util/find-diff
util/find-diff: add the findDiff utility
- Loading branch information
Showing
6 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Meta } from '@storybook/blocks' | ||
|
||
<Meta title='Utils/Find Diff' /> | ||
|
||
# Find Diff | ||
|
||
Find diff between two objects | ||
|
||
## findDiff(obj1, obj2) | ||
|
||
```js | ||
import { findDiff } from '@turistikrota/ui/utils' | ||
|
||
const obj1 = { a: 1, b: 2, c: 3 } | ||
const obj2 = { a: 1, b: 2, c: 3 } | ||
|
||
findDiff(obj1, obj2) // {} | ||
|
||
const obj3 = { a: 1, b: 2, c: 3 } | ||
const obj4 = { a: 1, b: 2, c: 4 } | ||
|
||
findDiff(obj3, obj4) // { c: 4 } | ||
|
||
const obj1 = { a: [1, 2], b: 3 } | ||
const obj2 = { a: [1, 2, 3], b: 4 } | ||
const result = findDiff(obj1, obj2) // { a: [1, 2, 3], b: 4 } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { findDiff } from '@turistikrota/ui/utils' | ||
|
||
describe('findDiff', () => { | ||
it('should return an empty object for two identical flat objects', () => { | ||
const obj1 = { a: 1, b: 2 } | ||
const obj2 = { a: 1, b: 2 } | ||
const result = findDiff(obj1, obj2) | ||
expect(result).toEqual({}) | ||
}) | ||
|
||
it('should return the differences for two flat objects', () => { | ||
const obj1 = { a: 1, b: 2 } | ||
const obj2 = { a: 2, b: 2, c: 3 } | ||
const result = findDiff(obj1, obj2) | ||
expect(result).toEqual({ a: 2, c: 3 }) | ||
}) | ||
|
||
it('should return the differences for nested objects', () => { | ||
const obj1 = { a: 1, nested: { b: 2, c: 3 } } | ||
const obj2 = { a: 1, nested: { b: 3, d: 4 } } | ||
const result = findDiff(obj1, obj2) | ||
expect(result).toEqual({ nested: { b: 3, d: 4 } }) | ||
}) | ||
|
||
it('should return the differences when an item is removed', () => { | ||
const obj1 = { a: 1, b: 2, c: 3 } | ||
const obj2 = { a: 1, b: 2 } | ||
const result = findDiff(obj1, obj2) | ||
expect(result).toEqual({ c: undefined }) | ||
}) | ||
|
||
it('should handle arrays correctly', () => { | ||
const obj1 = { a: [1, 2], b: 3 } | ||
const obj2 = { a: [1, 2, 3], b: 4 } | ||
const result = findDiff(obj1, obj2) | ||
expect(result).toEqual({ a: [1, 2, 3], b: 4 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters