Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdk): TUI_IDENTITY_MATCHER consider two empty arrays equal #34

Merged
merged 1 commit into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions projects/cdk/constants/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ export const TUI_STRICT_MATCHER: TuiMatcher<any> = (

/**
* 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<any> = (item1, item2) =>
item1 === item2;
export const TUI_IDENTITY_MATCHER: TuiIdentityMatcher<unknown> = (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
);
}
16 changes: 11 additions & 5 deletions projects/cdk/constants/test/matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
Expand Down