Skip to content

Commit

Permalink
feat@fix r.diff.with
Browse files Browse the repository at this point in the history
  • Loading branch information
selfrefactor committed Dec 4, 2024
1 parent 0b16151 commit 3fad305
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion source/differenceWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function differenceWithFn(
fn, a, b
){
const willReturn = []
const [ first, second ] = a.length > b.length ? [ a, b ] : [ b, a ]
const [ first, second ] = a.length >= b.length ? [ a, b ] : [ b, a ]

first.forEach(item => {
const hasItem = second.some(secondItem => fn(item, secondItem))
Expand Down
26 changes: 14 additions & 12 deletions source/differenceWith.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { differenceWith } from './differenceWith.js'
import { differenceWith } from './differenceWith.js';

test('happy', () => {
const foo = [ { a : 1 }, { a : 2 }, { a : 3 } ]
const bar = [ { a : 3 }, { a : 4 } ]
const fn = function (r, s){
return r.a === s.a
}
const result = differenceWith(
fn, foo, bar
)
expect(result).toEqual([ { a : 1 }, { a : 2 } ])
})
const fn = (a, b) => a.x === b.x;

test('same length of list', () => {
const result = differenceWith(fn, [{ x: 1 }, { x: 2 }], [{ x: 1 }, { x: 3 }]);
expect(result).toEqual([{ x: 2 }]);
});

test('different length of list', () => {
const foo = [{ x: 1 }, { x: 2 }, { x: 3 }];
const bar = [{ x: 3 }, { x: 4 }];
const result = differenceWith(fn, foo, bar);
expect(result).toEqual([{ x: 1 }, { x: 2 }]);
});

0 comments on commit 3fad305

Please sign in to comment.