From 675d28142251a7a317ab5928bf175e8a0eff61fd Mon Sep 17 00:00:00 2001 From: Bejamin Schachter Date: Tue, 5 Mar 2019 12:26:06 -0500 Subject: [PATCH] fix: differenceBy.md --> update differenceBy.test.js --- snippets/differenceBy.md | 8 ++++---- test/differenceBy.test.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/snippets/differenceBy.md b/snippets/differenceBy.md index 83b3b869c..b304aab38 100644 --- a/snippets/differenceBy.md +++ b/snippets/differenceBy.md @@ -2,16 +2,16 @@ Returns the difference between two arrays, after applying the provided function to each array element of both. -Create a `Set` by applying `fn` to each element in `b`, then use `Array.prototype.filter()` in combination with `fn` on `a` to only keep values not contained in the previously created set. +Create a `Set` by applying `fn` to each element in `b`, then use `Array.prototype.map()` to apply `fn` to each element in `a`, then `Array.prototype.filter()` ```js const differenceBy = (a, b, fn) => { const s = new Set(b.map(fn)); - return a.filter(x => !s.has(fn(x))); + return a.map(fn).filter(el => !s.has(el)); }; ``` ```js -differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1.2] -differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [ { x: 2 } ] +differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1] +differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [2] ``` diff --git a/test/differenceBy.test.js b/test/differenceBy.test.js index 719f0df6b..1743f1805 100644 --- a/test/differenceBy.test.js +++ b/test/differenceBy.test.js @@ -5,8 +5,8 @@ test('differenceBy is a Function', () => { expect(differenceBy).toBeInstanceOf(Function); }); test('Works using a native function and numbers', () => { - expect(differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor)).toEqual([1.2]); + expect(differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor)).toEqual([1]); }); test('Works with arrow function and objects', () => { - expect(differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x)).toEqual([{ x: 2 }]); + expect(differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x)).toEqual([2]); });