From 00e6bddc25de9176faacd36d1a5ecc72ec250a80 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jun 2021 17:44:30 -0500 Subject: add functions for response filtering --- src/web/src/lib/search.js | 110 +++++++++++++++++++++++++++++ src/web/src/lib/search.test.js | 156 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 src/web/src/lib/search.test.js (limited to 'src/web') diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index 2179dcd2..efd7c89a 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -1,5 +1,8 @@ import api from './api'; +export const CBR = 'CBR'; +export const VBR = 'VBR'; + export const search = ({ id, searchText }) => { return api.post(`/searches`, { id, searchText }); }; @@ -10,4 +13,111 @@ export const getStatus = async ({ id, includeResponses = false }) => { export const getResponses = async ({ id }) => { return (await api.get(`/searches/${encodeURIComponent(id)}/responses`)).data; +}; + +export const isConstantBitRate = (bitRate) => { + // because the caller most likely already O^2 this uses an ugly but fast approach + // https://en.wikipedia.org/wiki/MP3#Bit_rate + switch (bitRate) { + case 8: + case 16: + case 24: + case 32: + case 40: + case 48: + case 56: + case 64: + case 80: + case 96: + case 112: + case 128: + case 144: + case 160: + case 192: + case 224: + case 256: + case 320: + return true; + default: + return false; + } +} + +export const parseFiltersFromString = (string) => { + const filters = { + minBitRate: 0, + minFileSize: 0, + minLength: 0, + minFilesInFolder: 0, + include: [], + exclude: [], + isVBR: false, + isCBR: false + }; + + filters.minBitRate = getNthMatch(string, /(minbr|minbitrate):([0-9]+)/i, 2) || filters.minBitRate; + filters.minFileSize = getNthMatch(string, /(minfs|minfilesize):([0-9]+)/i, 2) || filters.minFileSize; + filters.minLength = getNthMatch(string, /(minlen|minlength):([0-9]+)/i, 2) || filters.minLength; + filters.minFilesInFolder = getNthMatch(string, /(minfif|minfilesinfolder):([0-9]+)/i, 2) || filters.minFilesInFolder; + + filters.isVBR = !!string.match(/isvbr/i); + filters.isCBR = !!string.match(/iscbr/i); + + const isReservedWord = (word) => word.includes(':') || word === 'isvbr' || word === 'iscbr'; + + let terms = string.toLowerCase().split(' ').filter(term => !isReservedWord(term)); + + filters.include = terms.filter(term => !term.startsWith('-')); + filters.exclude = terms.filter(term => term.startsWith('-')).map(term => term.slice(1)); + + return filters; +}; + +const getNthMatch = (string, regex, n) => { + const match = string.match(regex); + + if (match) { + return parseInt(match[n], 10); + } +} + +export const filterResponse = ({ + filters = { + minBitRate: 0, + minFileSize: 0, + minLength: 0, + include: [], + exclude: [], + isVBR: false, + isCBR: false + }, + response = { + files: [] + } +}) => { + let { files = [] } = response; + + if (response.fileCount + response.lockedFileCount < filters.minFilesInFolder) { + return { ...response, files: [] } + } + + console.log(filters); + + files = files.filter(file => { + const { bitRate, size, length, filename } = file; + const { isCBR, isVBR, minBitRate, minFileSize, minLength, include = [], exclude = [] } = filters; + + if (isCBR && !isConstantBitRate(bitRate)) return false; + if (isVBR && isConstantBitRate(bitRate)) return false; + if (bitRate < minBitRate) return false; + if (size < minFileSize) return false; + 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; + + return true; + }); + + return { ...response, files }; }; \ No newline at end of file diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js new file mode 100644 index 00000000..f0a32330 --- /dev/null +++ b/src/web/src/lib/search.test.js @@ -0,0 +1,156 @@ +import * as search from './search'; + +describe('isConstantBitRate', () => { + it('returns true given a valid MPEG-3 bitrate', () => { + const bitrates = [8,16,24,32,40,48,56,64,80,96,112,128,144,160,192,224,256,320]; + + bitrates.forEach(br => { + expect(search.isConstantBitRate(br)).toBe(true); + }); + }); + + it('returns false given an invalid MPEG-3 bitrate', () => { + const bitrates = [5,7,65,150,321]; + + bitrates.forEach(br => { + expect(search.isConstantBitRate(br)).toBe(false); + }); + }); + + it('returns false given null or undefined', () => { + expect(search.isConstantBitRate(null)).toBe(false); + expect(search.isConstantBitRate()).toBe(false); + }); +}); + +describe('filterResponse', () => { + it('removes VBR files if "iscbr" is specified', () => { + const response = { + files: [ + { bitRate: 123 }, + { bitRate: 320 } + ] + }; + + const filters = { isCBR: true }; + + expect(search.filterResponse({ response, filters })).toStrictEqual({ + files: [ + { bitRate: 320 } + ] + }); + }); + + it('removes CBR files if "isvbr" is specified', () => { + const response = { + files: [ + { bitRate: 123 }, + { bitRate: 320 } + ] + }; + + const filters = { isVBR: true }; + + expect(search.filterResponse({ response, filters })).toStrictEqual({ + files: [ + { bitRate: 123 } + ] + }); + }); + + it('removes all files if "iscbr" and "isvbr" are both specified', () => { + const response = { + files: [ + { bitRate: 123 }, + { bitRate: 320 } + ] + }; + + const filters = { isCBR: true, isVBR: true }; + + expect(search.filterResponse({ response, filters })).toStrictEqual({ + files: [] + }); + }); +}); + +describe('parseFiltersFromString', () => { + it('returns correct minBitrate', () => { + expect(search.parseFiltersFromString('foo minbr:42 bar')).toMatchObject({ + minBitRate: 42 + }); + + expect(search.parseFiltersFromString('foo minbitrate:123 bar')).toMatchObject({ + minBitRate: 123 + }); + }); + + it('returns correct minFileSize', () => { + expect(search.parseFiltersFromString('foo minfs:42 bar')).toMatchObject({ + minFileSize: 42 + }); + + expect(search.parseFiltersFromString('foo minfilesize:123 bar')).toMatchObject({ + minFileSize: 123 + }); + }); + + it('returns correct minLength', () => { + expect(search.parseFiltersFromString('foo minlen:42 bar')).toMatchObject({ + minLength: 42 + }); + + expect(search.parseFiltersFromString('foo minlength:123 bar')).toMatchObject({ + minLength: 123 + }); + }); + + it('returns correct minFilesInFolder', () => { + expect(search.parseFiltersFromString('foo minfif:42 bar')).toMatchObject({ + minFilesInFolder: 42 + }); + + expect(search.parseFiltersFromString('foo minfilesinfolder:123 bar')).toMatchObject({ + minFilesInFolder: 123 + }); + }); + + it('returns correct list of terms', () => { + expect(search.parseFiltersFromString('foo minbr:42 bar')).toMatchObject({ + include: [ 'foo', 'bar'] + }); + + expect(search.parseFiltersFromString('foo iscbr isvbr bar')).toMatchObject({ + include: [ 'foo', 'bar'] + }); + + expect(search.parseFiltersFromString('foo some:thing bar')).toMatchObject({ + include: [ 'foo', 'bar'] + }); + + expect(search.parseFiltersFromString('foo -bar')).toMatchObject({ + include: [ 'foo' ], + exclude: [ 'bar' ] + }); + }); + + it('returns isVBR and isCBR if terms are present', () => { + expect(search.parseFiltersFromString('isvbr')).toMatchObject({ + isVBR: true + }); + + expect(search.parseFiltersFromString('iscbr')).toMatchObject({ + isCBR: true + }); + }); + + it('returns expected filters given a bit of everything', () => { + expect(search.parseFiltersFromString('big -mix of:everything isvbr iscbr minbr:42')).toMatchObject({ + include: [ 'big' ], + exclude: [ 'mix' ], + isVBR: true, + isCBR: true, + minBitRate: 42 + }); + }); +}); \ No newline at end of file -- cgit v1.2.3 From 7a9203111eeeddcc815c0c820bf322daa0afa4cc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jun 2021 17:44:58 -0500 Subject: add note to complete tests --- src/web/src/lib/search.test.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/web') diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js index f0a32330..9461a2df 100644 --- a/src/web/src/lib/search.test.js +++ b/src/web/src/lib/search.test.js @@ -72,6 +72,8 @@ describe('filterResponse', () => { files: [] }); }); + + // todo: more tests }); describe('parseFiltersFromString', () => { -- cgit v1.2.3 From 62b196d7a4f01dfe74ea272b6753607f9f93ad5a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jun 2021 17:49:35 -0500 Subject: filter locked files too, adjust counts --- src/web/src/lib/search.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/web') diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index efd7c89a..6d59197a 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -92,18 +92,17 @@ export const filterResponse = ({ isCBR: false }, response = { - files: [] + files: [], + lockedFiles: [] } }) => { - let { files = [] } = response; + let { files = [], lockedFiles = [] } = response; if (response.fileCount + response.lockedFileCount < filters.minFilesInFolder) { return { ...response, files: [] } } - console.log(filters); - - files = files.filter(file => { + const filterFiles = (files) => files.filter(file => { const { bitRate, size, length, filename } = file; const { isCBR, isVBR, minBitRate, minFileSize, minLength, include = [], exclude = [] } = filters; @@ -119,5 +118,13 @@ export const filterResponse = ({ return true; }); - return { ...response, files }; + const filteredFiles = filterFiles(files); + const filteredLockedFiles = filterFiles(lockedFiles); + + return { + ...response, + fileCount: filteredFiles.length, + lockedFileCount: filteredLockedFiles.length, + files: filteredFiles, + lockedFiles: filteredLockedFiles }; }; \ No newline at end of file -- cgit v1.2.3 From d7abce6360907738c29999f6b2700f7fe64b998d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jun 2021 17:49:58 -0500 Subject: stub filter input and filtering code --- src/web/src/components/Search/Search.css | 5 +++ src/web/src/components/Search/Search.js | 64 ++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 27 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.css b/src/web/src/components/Search/Search.css index eb421948..7c91a08b 100644 --- a/src/web/src/components/Search/Search.css +++ b/src/web/src/components/Search/Search.css @@ -58,4 +58,9 @@ .search-loader { margin-top: 50px !important; +} + +.search-filter { + width: 100%; + margin-top: 1rem; } \ No newline at end of file diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 60720c1a..42b26195 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -32,6 +32,7 @@ const initialState = { hiddenResults: [], hideLocked: true, fetching: false, + resultFilters: undefined }; const sortOptions = { @@ -151,9 +152,11 @@ class Search extends Component { } sortAndFilterResults = () => { - const { results = [], hideNoFreeSlots, resultSort, hideLocked, hiddenResults = [] } = this.state; + const { results = [], hideNoFreeSlots, resultSort, resultFilters = '', hideLocked, hiddenResults = [] } = this.state; const { field, order } = sortOptions[resultSort]; + const filters = search.parseFiltersFromString(resultFilters); + return results .filter(r => !hiddenResults.includes(r.username)) .map(r => { @@ -162,6 +165,7 @@ class Search extends Component { } return r; }) + .map(response => search.filterResponse({ response, filters })) .filter(r => r.fileCount + r.lockedFileCount > 0) .filter(r => !(hideNoFreeSlots && r.freeUploadSlots === 0)) .sort((a, b) => { @@ -220,34 +224,40 @@ class Search extends Component { :
- {(results && results.length > 0) ? - this.setState({ resultSort: value }, () => this.saveState())} - text={sortDropdownOptions.find(o => o.value === resultSort).text} - /> -
- this.setState({ hideLocked: !hideLocked }, () => this.saveState())} - checked={hideLocked} - label='Hide Locked Results' + {(results && results.length > 0) ? + + this.setState({ resultSort: value }, () => this.saveState())} + text={sortDropdownOptions.find(o => o.value === resultSort).text} /> - this.setState({ hideNoFreeSlots: !hideNoFreeSlots }, () => this.saveState())} - checked={hideNoFreeSlots} - label='Hide Results with No Free Slots' +
+ this.setState({ hideLocked: !hideLocked }, () => this.saveState())} + checked={hideLocked} + label='Hide Locked Results' + /> + this.setState({ hideNoFreeSlots: !hideNoFreeSlots }, () => this.saveState())} + checked={hideNoFreeSlots} + label='Hide Results with No Free Slots' + /> +
+ -
-
: } + : + } {sortedAndFilteredResults.slice(0, displayCount).map((r, i) => Date: Sun, 6 Jun 2021 21:06:23 -0500 Subject: add more search tests --- src/web/src/lib/search.js | 26 +++++------- src/web/src/lib/search.test.js | 91 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 20 deletions(-) (limited to 'src/web') diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index 6d59197a..c56a94c4 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -1,8 +1,5 @@ import api from './api'; -export const CBR = 'CBR'; -export const VBR = 'VBR'; - export const search = ({ id, searchText }) => { return api.post(`/searches`, { id, searchText }); }; @@ -16,8 +13,6 @@ export const getResponses = async ({ id }) => { }; export const isConstantBitRate = (bitRate) => { - // because the caller most likely already O^2 this uses an ugly but fast approach - // https://en.wikipedia.org/wiki/MP3#Bit_rate switch (bitRate) { case 8: case 16: @@ -55,6 +50,14 @@ export const parseFiltersFromString = (string) => { isCBR: false }; + const getNthMatch = (string, regex, n) => { + const match = string.match(regex); + + if (match) { + return parseInt(match[n], 10); + } + } + filters.minBitRate = getNthMatch(string, /(minbr|minbitrate):([0-9]+)/i, 2) || filters.minBitRate; filters.minFileSize = getNthMatch(string, /(minfs|minfilesize):([0-9]+)/i, 2) || filters.minFileSize; filters.minLength = getNthMatch(string, /(minlen|minlength):([0-9]+)/i, 2) || filters.minLength; @@ -63,9 +66,8 @@ export const parseFiltersFromString = (string) => { filters.isVBR = !!string.match(/isvbr/i); filters.isCBR = !!string.match(/iscbr/i); - const isReservedWord = (word) => word.includes(':') || word === 'isvbr' || word === 'iscbr'; - - let terms = string.toLowerCase().split(' ').filter(term => !isReservedWord(term)); + let terms = string.toLowerCase().split(' ') + .filter(term => !term.includes(':') && term !== 'isvbr' && term !== 'iscbr'); filters.include = terms.filter(term => !term.startsWith('-')); filters.exclude = terms.filter(term => term.startsWith('-')).map(term => term.slice(1)); @@ -73,14 +75,6 @@ export const parseFiltersFromString = (string) => { return filters; }; -const getNthMatch = (string, regex, n) => { - const match = string.match(regex); - - if (match) { - return parseInt(match[n], 10); - } -} - export const filterResponse = ({ filters = { minBitRate: 0, diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js index 9461a2df..02c8cf99 100644 --- a/src/web/src/lib/search.test.js +++ b/src/web/src/lib/search.test.js @@ -34,7 +34,7 @@ describe('filterResponse', () => { const filters = { isCBR: true }; - expect(search.filterResponse({ response, filters })).toStrictEqual({ + expect(search.filterResponse({ response, filters })).toMatchObject({ files: [ { bitRate: 320 } ] @@ -51,7 +51,7 @@ describe('filterResponse', () => { const filters = { isVBR: true }; - expect(search.filterResponse({ response, filters })).toStrictEqual({ + expect(search.filterResponse({ response, filters })).toMatchObject({ files: [ { bitRate: 123 } ] @@ -68,12 +68,95 @@ describe('filterResponse', () => { const filters = { isCBR: true, isVBR: true }; - expect(search.filterResponse({ response, filters })).toStrictEqual({ + expect(search.filterResponse({ response, filters })).toMatchObject({ files: [] }); }); - // todo: more tests + it('removes files with bitRate less than minBitRate', () => { + const response = { + files: [ + { bitRate: 100 }, + { bitRate: 99 } + ] + }; + + const filters = { minBitRate: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { bitRate: 100 } + ] + }); + }); + + it('removes files with size less than minFileSize', () => { + const response = { + files: [ + { size: 100 }, + { size: 99 } + ] + }; + + const filters = { minFileSize: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { size: 100 } + ] + }); + }); + + it('removes files with length less than minLength', () => { + const response = { + files: [ + { length: 100 }, + { length: 99 } + ] + }; + + const filters = { minLength: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { length: 100 } + ] + }); + }); + + it('removes files with filenames not containing included phrases', () => { + const response = { + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/bar.mp3' } + ] + }; + + const filters = { include: ['foo'] }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/foo.mp3' } + ] + }); + }); + + 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' } + ] + }); + }); }); describe('parseFiltersFromString', () => { -- cgit v1.2.3 From cd55d0ae21a7b34fb71944aa3e44e14ff0e3c029 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jun 2021 22:28:04 -0500 Subject: update filters on input --- src/web/src/components/Search/Search.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 42b26195..73670e34 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -32,7 +32,7 @@ const initialState = { hiddenResults: [], hideLocked: true, fetching: false, - resultFilters: undefined + resultFilters: '' }; const sortOptions = { @@ -72,6 +72,10 @@ class Search extends Component { this.setState({ searchPhrase: data.value }); } + onResultFilterChange = (event, data) => { + this.setState({ resultFilters: data.value }, () => this.saveState()); + } + saveState = () => { try { localStorage.setItem('soulseek-example-search-state', JSON.stringify({ ...this.state, results: [] })); @@ -188,7 +192,7 @@ class Search extends Component { } render = () => { - let { searchState, searchStatus, results = [], displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults = [], fetching } = this.state; + let { searchState, searchStatus, results = [], displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults = [], fetching, resultFilters } = this.state; let pending = fetching || searchState === 'pending'; const sortedAndFilteredResults = this.sortAndFilterResults(); @@ -255,6 +259,9 @@ class Search extends Component { this.setState({ resultFilters: '' }) }} /> : } -- cgit v1.2.3 From 79fd8839d731593cce63b7e81a658b4ac2853e57 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Jun 2021 08:32:37 -0500 Subject: tweak filter label and placeholder --- src/web/src/components/Search/Search.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 73670e34..1ba8552a 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -257,8 +257,9 @@ class Search extends Component { />
this.setState({ resultFilters: '' }) }} -- cgit v1.2.3 From 5f1a6dc3725a2e09521fdbbc0d11356000d01f90 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Jun 2021 08:36:45 -0500 Subject: persist clear --- src/web/src/components/Search/Search.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 1ba8552a..cb334e16 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -76,6 +76,10 @@ class Search extends Component { this.setState({ resultFilters: data.value }, () => this.saveState()); } + clearResultFilter = () => { + this.setState({ resultFilters: '' }, () => this.saveState()); + } + saveState = () => { try { localStorage.setItem('soulseek-example-search-state', JSON.stringify({ ...this.state, results: [] })); @@ -262,7 +266,7 @@ class Search extends Component { label={{ icon: 'filter', content: 'Filter' }} value={resultFilters} onChange={this.onResultFilterChange} - action={!!resultFilters && { icon: 'x', color: 'red', onClick: () => this.setState({ resultFilters: '' }) }} + action={!!resultFilters && { icon: 'x', color: 'red', onClick: this.clearResultFilter }} /> : } -- cgit v1.2.3 From bda9b20ce36b90ab635b09557c4bf97fc9c9ad44 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Jun 2021 08:50:27 -0500 Subject: fix term exclusion filter --- src/web/src/lib/search.js | 2 +- src/web/src/lib/search.test.js | 74 +++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 24 deletions(-) (limited to 'src/web') 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', () => { -- cgit v1.2.3 From 7762f1ccf98b15a748036d213f3ccb822b649028 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Jun 2021 21:32:19 -0500 Subject: make included terms AND instead of OR --- src/web/src/lib/search.js | 5 +++-- src/web/src/lib/search.test.js | 7 ++----- 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'src/web') diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index c85f87c3..ff5f72de 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -106,7 +106,7 @@ export const filterResponse = ({ if (size < minFileSize) return false; if (length < minLength) return false; - if (include.length > 0 && include.filter(term => filename.toLowerCase().includes(term)).length === 0) return false; + if (include.length > 0 && include.filter(term => filename.toLowerCase().includes(term)).length !== include.length) return false; if (exclude.length > 0 && exclude.filter(term => filename.toLowerCase().includes(term)).length !== 0) return false; return true; @@ -120,5 +120,6 @@ export const filterResponse = ({ fileCount: filteredFiles.length, lockedFileCount: filteredLockedFiles.length, files: filteredFiles, - lockedFiles: filteredLockedFiles }; + lockedFiles: filteredLockedFiles + }; }; \ No newline at end of file diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js index cff25251..85e1d5f6 100644 --- a/src/web/src/lib/search.test.js +++ b/src/web/src/lib/search.test.js @@ -137,12 +137,10 @@ describe('filterResponse', () => { }; it('removes files with filenames not containing included phrases', () => { - const filters = { include: ['foo', 'nfo', 'baz'] }; + const filters = { include: ['path', 'to', '.nfo'] }; expect(search.filterResponse({ response, filters })).toMatchObject({ files: [ - { filename: '/path/to/foo.mp3' }, - { filename: '/path/to/baz.mp3' }, { filename: '/path/to/info.nfo' }, ] }); @@ -162,7 +160,7 @@ describe('filterResponse', () => { it('removes a mix of includes and excludes', () => { const filters = { - include: ['.jpg', '.mp3'], + include: ['path', '.mp3'], exclude: ['foo', 'bar'] }; @@ -170,7 +168,6 @@ describe('filterResponse', () => { files: [ { filename: '/path/to/baz.mp3' }, { filename: '/path/to/qux.mp3' }, - { filename: '/path/to/folder.jpg' } ] }); }); -- cgit v1.2.3 From df34ad28b8d1b3293a3be904f2b91c80d2cef3b6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Jun 2021 21:32:57 -0500 Subject: add placeholder --- src/web/src/components/Users/Users.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Users/Users.js b/src/web/src/components/Users/Users.js index 37d9d82b..ef92d0d5 100644 --- a/src/web/src/components/Users/Users.js +++ b/src/web/src/components/Users/Users.js @@ -2,6 +2,7 @@ import React, { useState, useEffect, useLayoutEffect, useRef } from 'react'; import { Item, Segment, + Loader, Input } from 'semantic-ui-react'; @@ -10,6 +11,7 @@ import { activeUserInfoKey } from '../../config'; import * as peers from '../../lib/peers'; import './Users.css'; +import PlaceholderSegment from '../Shared/PlaceholderSegment'; const Users = (props) => { const inputRef = useRef(); @@ -93,11 +95,20 @@ const Users = (props) => { onKeyUp={(e) => e.key === 'Enter' ? setSelectedUsername(usernameInput) : ''} /> - {!fetching && !error && !!user && - - - - } + {fetching ? + : +
+ {error ? + Failed to retrieve information for {selectedUsername} : + !user ? + : + + + + + } +
+ } ); }; -- cgit v1.2.3