From 2b145fd4361d436a4715e05d2d599f1d754daaa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Ulzurrun=20de=20Asanza=20i=20S=C3=A0ez?= Date: Tue, 23 Jul 2019 07:26:41 +0200 Subject: [PATCH 1/3] :white_check_mark: [mergeForEach] Throw error when a non-comparable pair is found --- test/unit/mergeForEach.spec.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/unit/mergeForEach.spec.ts b/test/unit/mergeForEach.spec.ts index 54efde1..a5f3e93 100644 --- a/test/unit/mergeForEach.spec.ts +++ b/test/unit/mergeForEach.spec.ts @@ -9,6 +9,7 @@ chaiUse(sinonChai) describe('mergeForEach', function () { const lhsBarcelona = { id: 0, name: 'Barcelona' } const lhsMadrid = { id: 1, name: 'Madrid' } + const lhsCityWithoutId = { name: 'LHS item without ID' } const rhsMadrid = { id_city: 1, city_name: 'Madrid' } const rhsValencia = { id_city: 2, city_name: 'Valencia' } @@ -84,6 +85,25 @@ describe('mergeForEach', function () { }) }) + describe('When using default comparison function', function () { + describe('When some elements are not comparable', function () { + it('Should throw an error', function () { + expect(function () { + const lhs = [lhsMadrid, lhsBarcelona, lhsCityWithoutId] as any + const rhs = [rhsValencia, rhsMadrid, rhsCityWithoutId] as any + + mergeForEach(lhs, rhs, { + lhsIteratee: 'id', + rhsIteratee: 'id_city', + leftCallback, + innerCallback, + rightCallback + }) + }).to.throw + }) + }) + }) + function buildTestCases< L extends any, R extends any, @@ -120,6 +140,7 @@ describe('mergeForEach', function () { rightCallback }) + expect(leftCallback).to.have.been.calledOnce expect(leftCallback).to.have.been.calledWithExactly(lhsBarcelona) }) @@ -143,8 +164,9 @@ describe('mergeForEach', function () { rightCallback }) - expect(rightCallback).to.have.been.calledWithExactly(rhsValencia) + expect(rightCallback).to.have.been.calledTwice expect(rightCallback).to.have.been.calledWithExactly(rhsCityWithoutId) + expect(rightCallback).to.have.been.calledWithExactly(rhsValencia) }) it('Should not call rightCallback if not provided', function () { @@ -157,7 +179,7 @@ describe('mergeForEach', function () { }) }) - it('Should call innerCallback for non-matching values in both collections', function () { + it('Should call innerCallback for matching values in both collections', function () { mergeForEach(lhs, rhs, { lhsIteratee, rhsIteratee, From f99696339e0e8c73967b07e4e04307606e478e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Ulzurrun=20de=20Asanza=20i=20S=C3=A0ez?= Date: Tue, 23 Jul 2019 07:27:00 +0200 Subject: [PATCH 2/3] :bug: [mergeForEach] Throw error when a non-comparable pair is found --- src/mergeForEach.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/mergeForEach.ts b/src/mergeForEach.ts index 35784f7..f131095 100644 --- a/src/mergeForEach.ts +++ b/src/mergeForEach.ts @@ -61,13 +61,11 @@ function mergeForEach< const lhsValue = getLHSValue(lhsItem) as any const rhsValue = getRHSValue(rhsItem) as any - if (lhsValue < rhsValue) { - return SORTING_ORDER.LHS_BEFORE_RHS - } else if (lhsValue > rhsValue) { - return SORTING_ORDER.LHS_AFTER_RHS - } else { - return SORTING_ORDER.EQUAL - } + if (lhsValue === rhsValue) return SORTING_ORDER.EQUAL + if (lhsValue < rhsValue) return SORTING_ORDER.LHS_BEFORE_RHS + if (lhsValue > rhsValue) return SORTING_ORDER.LHS_AFTER_RHS + + throw new Error(`[@geoblink/lodash-mixins#mergeForEach] Found non-comparable values: ${lhsValue} on LHS and ${rhsValue} on RHS`) } }: { lhsIteratee?: LHSItemKey | ((item: LHSItem) => any), From d9b2662ca6dc74d7642d7d5295cc4822152110dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Ulzurrun=20de=20Asanza=20i=20S=C3=A0ez?= Date: Tue, 23 Jul 2019 07:41:10 +0200 Subject: [PATCH 3/3] :pencil2: [mergeForEach] Fix typo in documentation --- src/mergeForEach.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mergeForEach.ts b/src/mergeForEach.ts index f131095..27ab927 100644 --- a/src/mergeForEach.ts +++ b/src/mergeForEach.ts @@ -14,7 +14,7 @@ export type ComparisonResult = number * Can be used to implement efficient algorithms which profit from sorted data * like `mergeSort` or `mergeJoinWith`. * - * Both collections must be sortable. They will be sorted ascendently using + * Both collections must be sortable. They will be sorted ascendantly using * value returned by the corresponding iteratee. * * @param lhs A collection of elements. @@ -144,7 +144,7 @@ declare module 'lodash' { * Can be used to implement efficient algorithms which profit from sorted * data like `mergeSort` or `mergeJoinWith`. * - * Both collections must be sortable. They will be sorted ascendently using + * Both collections must be sortable. They will be sorted ascendantly using * value returned by the corresponding iteratee. * * @param lhs A collection of elements.