diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-07 08:50:27 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-07 08:50:27 -0500 |
| commit | bda9b20ce36b90ab635b09557c4bf97fc9c9ad44 (patch) | |
| tree | 98d4f669387317f63c5e264e06cb1025a29e735a /src/web | |
| parent | 5f1a6dc3725a2e09521fdbbc0d11356000d01f90 (diff) | |
| download | slskd-bda9b20ce36b90ab635b09557c4bf97fc9c9ad44.tar.gz | |
fix term exclusion filter
Diffstat (limited to 'src/web')
| -rw-r--r-- | src/web/src/lib/search.js | 2 | ||||
| -rw-r--r-- | src/web/src/lib/search.test.js | 74 |
2 files changed, 52 insertions, 24 deletions
diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index c56a94c4..c85f87c3 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -107,7 +107,7 @@ export const filterResponse = ({ if (length < minLength) return false; if (include.length > 0 && include.filter(term => filename.toLowerCase().includes(term)).length === 0) return false; - if (exclude.length > 0 && exclude.filter(term => !filename.toLowerCase().includes(term)).length === 0) return false; + if (exclude.length > 0 && exclude.filter(term => filename.toLowerCase().includes(term)).length !== 0) return false; return true; }); diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js index 02c8cf99..cff25251 100644 --- a/src/web/src/lib/search.test.js +++ b/src/web/src/lib/search.test.js @@ -124,37 +124,55 @@ describe('filterResponse', () => { }); }); - it('removes files with filenames not containing included phrases', () => { + describe('term filtering', () => { const response = { files: [ { filename: '/path/to/foo.mp3' }, - { filename: '/path/to/bar.mp3' } + { filename: '/path/to/bar.mp3' }, + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/qux.mp3' }, + { filename: '/path/to/info.nfo' }, + { filename: '/path/to/folder.jpg' } ] }; - const filters = { include: ['foo'] }; - - expect(search.filterResponse({ response, filters })).toMatchObject({ - files: [ - { filename: '/path/to/foo.mp3' } - ] + it('removes files with filenames not containing included phrases', () => { + const filters = { include: ['foo', 'nfo', 'baz'] }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/info.nfo' }, + ] + }); }); - }); + + it('removes files with filenames containing excluded phrases', () => { + const filters = { exclude: ['bar', 'jpg', 'qux'] }; - it('removes files with filenames containing excluded phrases', () => { - const response = { - files: [ - { filename: '/path/to/foo.mp3' }, - { filename: '/path/to/bar.mp3' } - ] - }; - - const filters = { exclude: ['foo'] }; - - expect(search.filterResponse({ response, filters })).toMatchObject({ - files: [ - { filename: '/path/to/bar.mp3' } - ] + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/info.nfo' }, + ] + }); + }); + + it('removes a mix of includes and excludes', () => { + const filters = { + include: ['.jpg', '.mp3'], + exclude: ['foo', 'bar'] + }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/qux.mp3' }, + { filename: '/path/to/folder.jpg' } + ] + }); }); }); }); @@ -217,6 +235,16 @@ describe('parseFiltersFromString', () => { include: [ 'foo' ], exclude: [ 'bar' ] }); + + expect(search.parseFiltersFromString('-foo -bar -baz qux')).toMatchObject({ + exclude: [ 'foo', 'bar', 'baz' ], + include: [ 'qux' ] + }); + + expect(search.parseFiltersFromString('foo bar baz -qux')).toMatchObject({ + include: [ 'foo', 'bar', 'baz' ], + exclude: [ 'qux' ] + }); }); it('returns isVBR and isCBR if terms are present', () => { |
