aboutsummaryrefslogtreecommitdiffstats
path: root/src/web
diff options
context:
space:
mode:
Diffstat (limited to 'src/web')
-rw-r--r--src/web/src/lib/search.js110
-rw-r--r--src/web/src/lib/search.test.js156
2 files changed, 266 insertions, 0 deletions
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