From 30a72f11666669cc0c3c7d8234af6b19fab3ad8d Mon Sep 17 00:00:00 2001 From: waterplea Date: Fri, 30 Oct 2020 18:13:30 +0300 Subject: [PATCH] feat(cdk): `TUI_IDENTITY_MATCHER` consider two empty arrays equal --- projects/cdk/constants/matcher.ts | 12 ++++++++++-- projects/cdk/constants/test/matcher.spec.ts | 16 +++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/projects/cdk/constants/matcher.ts b/projects/cdk/constants/matcher.ts index 5fbfec129638..d50330aa44cf 100644 --- a/projects/cdk/constants/matcher.ts +++ b/projects/cdk/constants/matcher.ts @@ -27,8 +27,16 @@ export const TUI_STRICT_MATCHER: TuiMatcher = ( /** * Default handler to match equality of two elements + * ATTENTION: considers two empty arrays equal + * * @param item1 first element * @param item2 second element */ -export const TUI_IDENTITY_MATCHER: TuiIdentityMatcher = (item1, item2) => - item1 === item2; +export const TUI_IDENTITY_MATCHER: TuiIdentityMatcher = (item1, item2) => + item1 === item2 || bothEmpty(item1, item2); + +function bothEmpty(item1: unknown, item2: unknown): boolean { + return ( + item1 instanceof Array && item2 instanceof Array && !item1.length && !item2.length + ); +} diff --git a/projects/cdk/constants/test/matcher.spec.ts b/projects/cdk/constants/test/matcher.spec.ts index d3474b6620c6..ec32f57c4687 100644 --- a/projects/cdk/constants/test/matcher.spec.ts +++ b/projects/cdk/constants/test/matcher.spec.ts @@ -55,9 +55,7 @@ describe('Matcher functions', () => { describe('TUI_STRICT_MATCHER', () => { it('returns false by not equal stringified values, case insensitive', () => { - const stringify = (item: Item) => item.toString(); - - expect(strictMatcher(item, search, stringify)).toBe(false); + expect(strictMatcher(item, search)).toBe(false); }); it('returns true by equal stringified values, case insensitive', () => { @@ -69,11 +67,19 @@ describe('Matcher functions', () => { }); describe('TUI_IDENTITY_MATCHER', () => { - it('returns true if the parametrs are the same', () => { + it('returns true if the parameters are the same', () => { expect(identityMatcher(item, item)).toBe(true); }); - it('returns false if the parametrs are not the same object', () => { + it('returns true if both parameters are empty arrays', () => { + expect(identityMatcher([], [])).toBe(true); + }); + + it('returns false if both parameters are equal arrays', () => { + expect(identityMatcher([0], [0])).toBe(false); + }); + + it('returns false if the parameters are not the same object', () => { const anotherItemWithSameFields = new Item(item.name, item.price); expect(identityMatcher(item, anotherItemWithSameFields)).toBe(false);