From 3fad305759411c1e738a2e30324c8d5022db62e0 Mon Sep 17 00:00:00 2001 From: Dejan Toteff Date: Wed, 4 Dec 2024 09:53:46 +0200 Subject: [PATCH] feat@fix r.diff.with --- source/differenceWith.js | 2 +- source/differenceWith.spec.js | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/source/differenceWith.js b/source/differenceWith.js index 407c3e6a..e600929e 100644 --- a/source/differenceWith.js +++ b/source/differenceWith.js @@ -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)) diff --git a/source/differenceWith.spec.js b/source/differenceWith.spec.js index 98fb4025..3f957de6 100644 --- a/source/differenceWith.spec.js +++ b/source/differenceWith.spec.js @@ -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 }]); +});