diff --git a/src/app/services/filter.helper.spec.ts b/src/app/services/filter.helper.spec.ts new file mode 100644 index 0000000..9196698 --- /dev/null +++ b/src/app/services/filter.helper.spec.ts @@ -0,0 +1,66 @@ +import {Song} from '../modules/songs/services/song'; +import {filterSong} from './filter.helper'; + +describe('Filter Helper', () => { + const song: Song = { + title: 'Song Title', + text: "This is a songtext, aa?bb!cc,dd.ee'ff", + legalOwner: '', + label: '', + id: '', + legalType: '', + artist: '', + comment: '', + edits: [], + final: false, + flags: '', + key: '', + number: 1, + legalOwnerId: '', + origin: '', + status: '', + tempo: 10, + type: '', + termsOfUse: '', + }; + + it('should not find song', () => { + void expect(filterSong(song, 'nope')).toBe(false); + }); + + it('should find song by title', () => { + void expect(filterSong(song, 'Song Title')).toBe(true); + }); + + it('should find song by text', () => { + void expect(filterSong(song, 'This is a songtext')).toBe(true); + }); + + it('should find with spaces', () => { + void expect(filterSong(song, 'SongTitle')).toBe(true); + }); + + it('should find ordinal invariant', () => { + void expect(filterSong(song, 'songtitle')).toBe(true); + }); + + it('should find ? invariant', () => { + void expect(filterSong(song, 'aabb')).toBe(true); + }); + + it('should find ! invariant', () => { + void expect(filterSong(song, 'bbcc')).toBe(true); + }); + + it('should find , invariant', () => { + void expect(filterSong(song, 'ccdd')).toBe(true); + }); + + it('should find . invariant', () => { + void expect(filterSong(song, 'ddee')).toBe(true); + }); + + it('should find apostroph invariant', () => { + void expect(filterSong(song, 'eeff')).toBe(true); + }); +}); diff --git a/src/app/services/filter.helper.ts b/src/app/services/filter.helper.ts index b059e86..4c5d0ef 100644 --- a/src/app/services/filter.helper.ts +++ b/src/app/services/filter.helper.ts @@ -1,9 +1,7 @@ import {Song} from '../modules/songs/services/song'; export function filterSong(song: Song, filterValue: string): boolean { - if (!filterValue) { - return true; - } + if (!filterValue) return true; const textMatch = !!song.text && normalize(song.text).indexOf(normalize(filterValue)) !== -1; const titleMatch = !!song.title && normalize(song.title).indexOf(normalize(filterValue)) !== -1; @@ -12,5 +10,5 @@ export function filterSong(song: Song, filterValue: string): boolean { } function normalize(input: string): string { - return input.toLowerCase().replace(/\s/g, ''); + return input.toLowerCase().replace(/[\s?!.,']/g, ''); }