aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-03-31 08:43:27 -0500
committerGitHub <noreply@github.com>2022-03-31 08:43:27 -0500
commit63a34dc2fcf3c06a91af5b74dac47ecd2e19f9fa (patch)
tree0bf87f8b8693f3c2c5b07015c7f85b0f0b6640cf /src
parent95a3ee390c527dcd8a27ff743391e340b13d3904 (diff)
parent55001959cf13b82d533f7aa8d8bd046b3c7d36dd (diff)
downloadslskd-63a34dc2fcf3c06a91af5b74dac47ecd2e19f9fa.tar.gz
Merge pull request #402 from slskd/filters
Add 'islossless' search filter, improve handling of 'isvbr' and 'iscbr'
Diffstat (limited to 'src')
-rw-r--r--src/web/src/components/Search/Search.js2
-rw-r--r--src/web/src/lib/search.js50
-rw-r--r--src/web/src/lib/search.test.js73
3 files changed, 59 insertions, 66 deletions
diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js
index ef08306d..713bfe26 100644
--- a/src/web/src/components/Search/Search.js
+++ b/src/web/src/components/Search/Search.js
@@ -262,7 +262,7 @@ class Search extends Component {
</div>
<Input
className='search-filter'
- placeholder='lackluster container -bothersome iscbr|isvbr minbitrate:320 minfilesize:10 minfilesinfolder:8 minlength:5000'
+ placeholder='lackluster container -bothersome iscbr|isvbr islossless|islossy minbitrate:320 minfilesize:10 minfilesinfolder:8 minlength:5000'
label={{ icon: 'filter', content: 'Filter' }}
value={resultFilters}
onChange={this.onResultFilterChange}
diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js
index ab0742ce..306c7041 100644
--- a/src/web/src/lib/search.js
+++ b/src/web/src/lib/search.js
@@ -19,32 +19,6 @@ export const getResponses = async ({ id }) => {
return response;
};
-export const isConstantBitRate = (bitRate) => {
- 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,
@@ -54,7 +28,9 @@ export const parseFiltersFromString = (string) => {
include: [],
exclude: [],
isVBR: false,
- isCBR: false
+ isCBR: false,
+ isLossless: false,
+ isLossy: false,
};
const getNthMatch = (string, regex, n) => {
@@ -72,9 +48,11 @@ export const parseFiltersFromString = (string) => {
filters.isVBR = !!string.match(/isvbr/i);
filters.isCBR = !!string.match(/iscbr/i);
+ filters.isLossless = !!string.match(/islossless/i);
+ filters.isLossy = !!string.match(/islossy/i);
let terms = string.toLowerCase().split(' ')
- .filter(term => !term.includes(':') && term !== 'isvbr' && term !== 'iscbr');
+ .filter(term => !term.includes(':') && term !== 'isvbr' && term !== 'iscbr' && term !== 'islossless' && term !== 'islossy');
filters.include = terms.filter(term => !term.startsWith('-'));
filters.exclude = terms.filter(term => term.startsWith('-')).map(term => term.slice(1));
@@ -90,7 +68,9 @@ export const filterResponse = ({
include: [],
exclude: [],
isVBR: false,
- isCBR: false
+ isCBR: false,
+ isLossless: false,
+ isLossy: false,
},
response = {
files: [],
@@ -104,11 +84,13 @@ export const filterResponse = ({
}
const filterFiles = (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;
+ const { bitRate, size, length, filename, sampleRate, bitDepth, isVariableBitRate } = file;
+ const { isCBR, isVBR, isLossless, isLossy, minBitRate, minFileSize, minLength, include = [], exclude = [] } = filters;
+
+ if (isCBR && (isVariableBitRate === undefined || isVariableBitRate)) return false;
+ if (isVBR && (isVariableBitRate === undefined || !isVariableBitRate)) return false;
+ if (isLossless && (!sampleRate || !bitDepth)) return false;
+ if (isLossy && (sampleRate || bitDepth)) return false;
if (bitRate < minBitRate) return false;
if (size < minFileSize) return false;
if (length < minLength) return false;
diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js
index 85e1d5f6..68afe91c 100644
--- a/src/web/src/lib/search.test.js
+++ b/src/web/src/lib/search.test.js
@@ -1,34 +1,11 @@
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 }
+ { bitRate: 123, isVariableBitRate: true },
+ { bitRate: 320, isVariableBitRate: false }
]
};
@@ -36,7 +13,7 @@ describe('filterResponse', () => {
expect(search.filterResponse({ response, filters })).toMatchObject({
files: [
- { bitRate: 320 }
+ { bitRate: 320, isVariableBitRate: false }
]
});
});
@@ -44,8 +21,8 @@ describe('filterResponse', () => {
it('removes CBR files if "isvbr" is specified', () => {
const response = {
files: [
- { bitRate: 123 },
- { bitRate: 320 }
+ { bitRate: 123, isVariableBitRate: true },
+ { bitRate: 320, isVariableBitRate: false }
]
};
@@ -53,7 +30,7 @@ describe('filterResponse', () => {
expect(search.filterResponse({ response, filters })).toMatchObject({
files: [
- { bitRate: 123 }
+ { bitRate: 123, isVariableBitRate: true }
]
});
});
@@ -61,8 +38,8 @@ describe('filterResponse', () => {
it('removes all files if "iscbr" and "isvbr" are both specified', () => {
const response = {
files: [
- { bitRate: 123 },
- { bitRate: 320 }
+ { isVariableBitrate: true },
+ { isVariableBitrate: false }
]
};
@@ -73,6 +50,40 @@ describe('filterResponse', () => {
});
});
+ it('removes lossy files if "islossless" is specified', () => {
+ const response = {
+ files: [
+ { sampleRate: 41000, bitDepth: 16 },
+ { bitRate: 320, isVariableBitRate: false }
+ ]
+ };
+
+ const filters = { isLossless: true };
+
+ expect(search.filterResponse({ response, filters })).toMatchObject({
+ files: [
+ { sampleRate: 41000, bitDepth: 16 }
+ ]
+ });
+ });
+
+ it('removes lossless files if "islossy" is specified', () => {
+ const response = {
+ files: [
+ { sampleRate: 41000, bitDepth: 16 },
+ { bitRate: 320, isVariableBitRate: false }
+ ]
+ };
+
+ const filters = { isLossy: true };
+
+ expect(search.filterResponse({ response, filters })).toMatchObject({
+ files: [
+ { bitRate: 320, isVariableBitRate: false }
+ ]
+ });
+ });
+
it('removes files with bitRate less than minBitRate', () => {
const response = {
files: [