Skip to content

Commit

Permalink
fix: diff function when root is [] (#111)
Browse files Browse the repository at this point in the history
* fix: diff function when root is []

fix the behavior of the diff function when the root element is an empty array.
now, if root is an emtpy array [] it will be returned as is, instead if root is null, other will be returned

* chore: bump version to 8.0.1 (patch)
  • Loading branch information
matteosacchetto authored Oct 19, 2022
1 parent 904a710 commit 282ca62
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "8.0.0",
"version": "8.0.1",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export const diff = <T>(
t as unknown as string | number | symbol
): T[] => {
if (!root?.length && !other?.length) return []
if (!root?.length) return [...other]
if (root?.length === undefined) return [...other]
if (!other?.length) return [...root]
const bKeys = other.reduce(
(acc, item) => ({
Expand Down
8 changes: 8 additions & 0 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,14 @@ describe('array module', () => {
const result = _.diff(null, null)
assert.deepEqual(result, [])
})
test('handles empty array root', () => {
const result = _.diff([], ['a'])
assert.deepEqual(result, [])
})
test('handles empty array other', () => {
const result = _.diff(['a'], [])
assert.deepEqual(result, ['a'])
})
test('returns all items from root that dont exist in other', () => {
const result = _.diff(
['a', 'b', 'c'],
Expand Down

0 comments on commit 282ca62

Please sign in to comment.