feat: add contact filter to search by name
This commit is contained in:
32
src/app/pipes/contacts-filter-pipe.spec.ts
Normal file
32
src/app/pipes/contacts-filter-pipe.spec.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { pipe } from 'rxjs';
|
||||
import { ContactsFilterPipe } from './contacts-filter-pipe';
|
||||
import { ContactDTO } from '../models/ContactDTO';
|
||||
|
||||
describe('ContactsFilterPipe', () => {
|
||||
let pipe: ContactsFilterPipe;
|
||||
let CONTACTS_MOCK = [
|
||||
new ContactDTO(1,'mock')
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
pipe = new ContactsFilterPipe();
|
||||
});
|
||||
|
||||
it('create an instance', () => {
|
||||
expect(pipe).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return an empty array if value is null', () => {
|
||||
expect(pipe.transform(null, '')).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return the passed array without transformation if name is empty (\'\')', () => {
|
||||
expect(pipe.transform(CONTACTS_MOCK, '')).toEqual(CONTACTS_MOCK);
|
||||
});
|
||||
|
||||
it('should return entries wich name contains the given string name', () => {
|
||||
const ARG_MOCK = 'mo';
|
||||
const transformationResult = pipe.transform(CONTACTS_MOCK, ARG_MOCK);
|
||||
expect(transformationResult.find( c => c.name.includes(ARG_MOCK))).toBeTruthy();
|
||||
});
|
||||
});
|
||||
15
src/app/pipes/contacts-filter-pipe.ts
Normal file
15
src/app/pipes/contacts-filter-pipe.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { ContactDTO } from '../models/ContactDTO';
|
||||
|
||||
@Pipe({
|
||||
name: 'contactsFilter'
|
||||
})
|
||||
export class ContactsFilterPipe implements PipeTransform {
|
||||
|
||||
transform(value: ContactDTO[]|null, name: string): ContactDTO[] {
|
||||
if(value === null) return [];
|
||||
if(name === '') return value;
|
||||
return value.filter( contact => contact.name.toLowerCase().includes(name.toLowerCase()));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user