mirror of
https://github.com/Sonarr/Sonarr.git
synced 2026-04-18 21:35:27 -04:00
Convert FilterBuilder types to TypeScript
This commit is contained in:
@@ -14,3 +14,21 @@ export const SERIES = 'series';
|
||||
export const SERIES_STATUS = 'seriesStatus';
|
||||
export const SERIES_TYPES = 'seriesType';
|
||||
export const TAG = 'tag';
|
||||
|
||||
export type FilterBuildValueType =
|
||||
| 'bool'
|
||||
| 'bytes'
|
||||
| 'date'
|
||||
| 'default'
|
||||
| 'historyEventType'
|
||||
| 'indexer'
|
||||
| 'language'
|
||||
| 'protocol'
|
||||
| 'quality'
|
||||
| 'qualityProfile'
|
||||
| 'queueStatus'
|
||||
| 'seasonsMonitoredStatus'
|
||||
| 'series'
|
||||
| 'seriesStatus'
|
||||
| 'seriesType'
|
||||
| 'tag';
|
||||
@@ -1,61 +0,0 @@
|
||||
import * as filterTypes from './filterTypes';
|
||||
|
||||
const filterTypePredicates = {
|
||||
[filterTypes.CONTAINS]: function(itemValue, filterValue) {
|
||||
if (Array.isArray(itemValue)) {
|
||||
return itemValue.some((v) => v === filterValue);
|
||||
}
|
||||
|
||||
return itemValue.toLowerCase().contains(filterValue.toLowerCase());
|
||||
},
|
||||
|
||||
[filterTypes.EQUAL]: function(itemValue, filterValue) {
|
||||
return itemValue === filterValue;
|
||||
},
|
||||
|
||||
[filterTypes.GREATER_THAN]: function(itemValue, filterValue) {
|
||||
return itemValue > filterValue;
|
||||
},
|
||||
|
||||
[filterTypes.GREATER_THAN_OR_EQUAL]: function(itemValue, filterValue) {
|
||||
return itemValue >= filterValue;
|
||||
},
|
||||
|
||||
[filterTypes.LESS_THAN]: function(itemValue, filterValue) {
|
||||
return itemValue < filterValue;
|
||||
},
|
||||
|
||||
[filterTypes.LESS_THAN_OR_EQUAL]: function(itemValue, filterValue) {
|
||||
return itemValue <= filterValue;
|
||||
},
|
||||
|
||||
[filterTypes.NOT_CONTAINS]: function(itemValue, filterValue) {
|
||||
if (Array.isArray(itemValue)) {
|
||||
return !itemValue.some((v) => v === filterValue);
|
||||
}
|
||||
|
||||
return !itemValue.toLowerCase().contains(filterValue.toLowerCase());
|
||||
},
|
||||
|
||||
[filterTypes.NOT_EQUAL]: function(itemValue, filterValue) {
|
||||
return itemValue !== filterValue;
|
||||
},
|
||||
|
||||
[filterTypes.STARTS_WITH]: function(itemValue, filterValue) {
|
||||
return itemValue.toLowerCase().startsWith(filterValue.toLowerCase());
|
||||
},
|
||||
|
||||
[filterTypes.NOT_STARTS_WITH]: function(itemValue, filterValue) {
|
||||
return !itemValue.toLowerCase().startsWith(filterValue.toLowerCase());
|
||||
},
|
||||
|
||||
[filterTypes.ENDS_WITH]: function(itemValue, filterValue) {
|
||||
return itemValue.toLowerCase().endsWith(filterValue.toLowerCase());
|
||||
},
|
||||
|
||||
[filterTypes.NOT_ENDS_WITH]: function(itemValue, filterValue) {
|
||||
return !itemValue.toLowerCase().endsWith(filterValue.toLowerCase());
|
||||
}
|
||||
};
|
||||
|
||||
export default filterTypePredicates;
|
||||
115
frontend/src/Helpers/Props/getFilterTypePredicate.ts
Normal file
115
frontend/src/Helpers/Props/getFilterTypePredicate.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { FilterType } from './filterTypes';
|
||||
|
||||
type FilterPredicate<T> = (itemValue: T, filterValue: T) => boolean;
|
||||
|
||||
function getFilterTypePredicate<T>(filterType: FilterType): FilterPredicate<T> {
|
||||
if (filterType === 'contains') {
|
||||
return function (itemValue, filterValue) {
|
||||
if (Array.isArray(itemValue)) {
|
||||
return itemValue.some((v) => v === filterValue);
|
||||
}
|
||||
|
||||
if (typeof itemValue === 'string' && typeof filterValue === 'string') {
|
||||
return itemValue.toLowerCase().includes(filterValue.toLowerCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'equal') {
|
||||
return function (itemValue, filterValue) {
|
||||
return itemValue === filterValue;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'greaterThan') {
|
||||
return function (itemValue, filterValue) {
|
||||
return itemValue > filterValue;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'greaterThanOrEqual') {
|
||||
return function (itemValue, filterValue) {
|
||||
return itemValue >= filterValue;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'lessThan') {
|
||||
return function (itemValue, filterValue) {
|
||||
return itemValue < filterValue;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'lessThanOrEqual') {
|
||||
return function (itemValue, filterValue) {
|
||||
return itemValue <= filterValue;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'notContains') {
|
||||
return function (itemValue, filterValue) {
|
||||
if (Array.isArray(itemValue)) {
|
||||
return !itemValue.some((v) => v === filterValue);
|
||||
}
|
||||
|
||||
if (typeof itemValue === 'string' && typeof filterValue === 'string') {
|
||||
return !itemValue.toLowerCase().includes(filterValue.toLowerCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'notEqual') {
|
||||
return function (itemValue, filterValue) {
|
||||
return itemValue !== filterValue;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'startsWith') {
|
||||
return function (itemValue, filterValue) {
|
||||
if (typeof itemValue === 'string' && typeof filterValue === 'string') {
|
||||
return itemValue.toLowerCase().startsWith(filterValue.toLowerCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'notStartsWith') {
|
||||
return function (itemValue, filterValue) {
|
||||
if (typeof itemValue === 'string' && typeof filterValue === 'string') {
|
||||
return !itemValue.toLowerCase().startsWith(filterValue.toLowerCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'endsWith') {
|
||||
return function (itemValue, filterValue) {
|
||||
if (typeof itemValue === 'string' && typeof filterValue === 'string') {
|
||||
return itemValue.toLowerCase().endsWith(filterValue.toLowerCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
if (filterType === 'notEndsWith') {
|
||||
return function (itemValue, filterValue) {
|
||||
if (typeof itemValue === 'string' && typeof filterValue === 'string') {
|
||||
return !itemValue.toLowerCase().endsWith(filterValue.toLowerCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
return () => {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
export default getFilterTypePredicate;
|
||||
@@ -1,7 +1,6 @@
|
||||
import * as align from './align';
|
||||
import * as filterBuilderTypes from './filterBuilderTypes';
|
||||
import * as filterBuilderValueTypes from './filterBuilderValueTypes';
|
||||
import filterTypePredicates from './filterTypePredicates';
|
||||
import * as filterTypes from './filterTypes';
|
||||
import * as icons from './icons';
|
||||
import * as inputTypes from './inputTypes';
|
||||
@@ -17,7 +16,6 @@ export {
|
||||
inputTypes,
|
||||
filterBuilderTypes,
|
||||
filterBuilderValueTypes,
|
||||
filterTypePredicates,
|
||||
filterTypes,
|
||||
icons,
|
||||
kinds,
|
||||
|
||||
@@ -3,9 +3,6 @@ export const INFO = 'info';
|
||||
export const SUCCESS = 'success';
|
||||
export const WARNING = 'warning';
|
||||
|
||||
export const all = [
|
||||
ERROR,
|
||||
INFO,
|
||||
SUCCESS,
|
||||
WARNING
|
||||
];
|
||||
export const all = [ERROR, INFO, SUCCESS, WARNING];
|
||||
|
||||
export type MessageType = 'error' | 'info' | 'success' | 'warning';
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createAction } from 'redux-actions';
|
||||
import { filterBuilderTypes, filterBuilderValueTypes, filterTypePredicates, filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import { filterBuilderTypes, filterBuilderValueTypes, filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import getFilterTypePredicate from 'Helpers/Props/getFilterTypePredicate';
|
||||
import { createThunk, handleThunks } from 'Store/thunks';
|
||||
import sortByProp from 'Utilities/Array/sortByProp';
|
||||
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
||||
@@ -107,8 +108,7 @@ export const defaultState = {
|
||||
},
|
||||
|
||||
languages: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const languages = item.languages.map((language) => language.name);
|
||||
|
||||
return predicate(languages, filterValue);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import _ from 'lodash';
|
||||
import { createAction } from 'redux-actions';
|
||||
import { batchActions } from 'redux-batched-actions';
|
||||
import { filterBuilderTypes, filterBuilderValueTypes, filterTypePredicates, filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import { filterBuilderTypes, filterBuilderValueTypes, filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import getFilterTypePredicate from 'Helpers/Props/getFilterTypePredicate';
|
||||
import { createThunk, handleThunks } from 'Store/thunks';
|
||||
import sortByProp from 'Utilities/Array/sortByProp';
|
||||
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
||||
@@ -103,7 +104,7 @@ export const filterPredicates = {
|
||||
episodeFileCount / episodeCount * 100 :
|
||||
100;
|
||||
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
|
||||
return predicate(progress, filterValue);
|
||||
},
|
||||
@@ -127,21 +128,21 @@ export const filterPredicates = {
|
||||
},
|
||||
|
||||
ratings: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const { value = 0 } = item.ratings;
|
||||
|
||||
return predicate(value * 10, filterValue);
|
||||
},
|
||||
|
||||
ratingVotes: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const { votes = 0 } = item.ratings;
|
||||
|
||||
return predicate(votes, filterValue);
|
||||
},
|
||||
|
||||
originalLanguage: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const { originalLanguage } = item;
|
||||
|
||||
return predicate(originalLanguage ? originalLanguage.name : '', filterValue);
|
||||
@@ -154,20 +155,20 @@ export const filterPredicates = {
|
||||
releaseGroups = []
|
||||
} = statistics;
|
||||
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
|
||||
return predicate(releaseGroups, filterValue);
|
||||
},
|
||||
|
||||
seasonCount: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const seasonCount = item.statistics ? item.statistics.seasonCount : 0;
|
||||
|
||||
return predicate(seasonCount, filterValue);
|
||||
},
|
||||
|
||||
sizeOnDisk: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const sizeOnDisk = item.statistics && item.statistics.sizeOnDisk ?
|
||||
item.statistics.sizeOnDisk :
|
||||
0;
|
||||
@@ -176,7 +177,7 @@ export const filterPredicates = {
|
||||
},
|
||||
|
||||
hasMissingSeason: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const { seasons = [] } = item;
|
||||
|
||||
const hasMissingSeason = seasons.some((season) => {
|
||||
@@ -203,7 +204,7 @@ export const filterPredicates = {
|
||||
},
|
||||
|
||||
seasonsMonitoredStatus: function(item, filterValue, type) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
const { seasons = [] } = item;
|
||||
|
||||
const { monitoredCount, unmonitoredCount } = seasons.reduce((acc, { seasonNumber, monitored }) => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
import { createSelector } from 'reselect';
|
||||
import { filterTypePredicates, filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import { filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import getFilterTypePredicate from 'Helpers/Props/getFilterTypePredicate';
|
||||
import findSelectedFilters from 'Utilities/Filter/findSelectedFilters';
|
||||
|
||||
function getSortClause(sortKey, sortDirection, sortPredicates) {
|
||||
@@ -56,7 +57,7 @@ function filter(items, state) {
|
||||
accepted = predicate(item, value, type);
|
||||
}
|
||||
} else if (item.hasOwnProperty(key)) {
|
||||
const predicate = filterTypePredicates[type];
|
||||
const predicate = getFilterTypePredicate(type);
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user