feat: add contact filter to search by name

This commit is contained in:
2025-12-14 20:44:54 -03:00
parent 74ba14c3de
commit ccde994797
2 changed files with 47 additions and 0 deletions

View 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();
});
});

View 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()));
}
}