fix filter songs to add
This commit is contained in:
66
src/app/services/filter.helper.spec.ts
Normal file
66
src/app/services/filter.helper.spec.ts
Normal file
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import {Song} from '../modules/songs/services/song';
|
import {Song} from '../modules/songs/services/song';
|
||||||
|
|
||||||
export function filterSong(song: Song, filterValue: string): boolean {
|
export function filterSong(song: Song, filterValue: string): boolean {
|
||||||
if (!filterValue) {
|
if (!filterValue) return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const textMatch = !!song.text && normalize(song.text).indexOf(normalize(filterValue)) !== -1;
|
const textMatch = !!song.text && normalize(song.text).indexOf(normalize(filterValue)) !== -1;
|
||||||
const titleMatch = !!song.title && normalize(song.title).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 {
|
function normalize(input: string): string {
|
||||||
return input.toLowerCase().replace(/\s/g, '');
|
return input.toLowerCase().replace(/[\s?!.,']/g, '');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user