parent
8482f3da1a
commit
4849d1da10
@ -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;
|
@ -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;
|
Loading…
Reference in new issue