Removed FilterUtils in favor of the new FilterService API

pull/973/head
Cagatay Civici 2021-02-08 15:02:05 +03:00
parent 0a27a0ce27
commit 4983330e7a
6 changed files with 213 additions and 223 deletions

View File

@ -1,4 +1,5 @@
import FilterMatchMode from './FilterMatchMode';
import FilterOperator from './FilterOperator';
import FilterService from './FilterService';
export {FilterMatchMode,FilterOperator};
export {FilterMatchMode,FilterOperator,FilterService};

View File

@ -0,0 +1,202 @@
import {ObjectUtils} from 'primevue/utils';
const FilterService = {
filters: {
startsWith(value, filter, filterLocale) {
if (filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.slice(0, filterValue.length) === filterValue;
},
contains(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.indexOf(filterValue) !== -1;
},
notContains(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.indexOf(filterValue) === -1;
},
endsWith(value, filter, filterLocale) {
if (filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.indexOf(filterValue, stringValue.length - filterValue.length) !== -1;
},
equals(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() === filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) == ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
},
notEquals(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return false;
}
if (value === undefined || value === null) {
return true;
}
if (value.getTime && filter.getTime)
return value.getTime() !== filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) != ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
},
in(value, filter) {
if (filter === undefined || filter === null || filter.length === 0) {
return true;
}
for (let i = 0; i < filter.length; i++) {
if (ObjectUtils.equals(value, filter[i])) {
return true;
}
}
return false;
},
between(value, filter) {
if (filter == null || filter[0] == null || filter[1] == null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime)
return filter[0].getTime() <= value.getTime() && value.getTime() <= filter[1].getTime();
else
return filter[0] <= value && value <= filter[1];
},
lt(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() < filter.getTime();
else
return value < filter;
},
lte(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() <= filter.getTime();
else
return value <= filter;
},
gt(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() > filter.getTime();
else
return value > filter;
},
gte(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() >= filter.getTime();
else
return value >= filter;
},
is(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() === filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) == ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
},
isNot(value, filter, filterLocale) {
return this.filters.notEquals(value, filter, filterLocale);
},
before(value, filter, filterLocale) {
return this.filters.lt(value, filter, filterLocale);
},
after(value, filter, filterLocale) {
return this.filters.gt(value, filter, filterLocale);
}
},
register(rule, fn) {
this.filters[rule] = fn;
}
}
export default FilterService;

View File

@ -132,8 +132,8 @@
</template>
<script>
import {ObjectUtils,FilterUtils,DomHandler} from 'primevue/utils';
import {FilterMatchMode,FilterOperator} from 'primevue/api';
import {ObjectUtils,DomHandler} from 'primevue/utils';
import {FilterMatchMode,FilterOperator,FilterService} from 'primevue/api';
import Paginator from 'primevue/paginator';
import ScrollableView from './ScrollableView.vue';
import TableHeader from './TableHeader.vue';
@ -647,7 +647,7 @@ export default {
if (this.filters['global'] && !globalMatch && globalFilterFieldsArray) {
for(let j = 0; j < globalFilterFieldsArray.length; j++) {
let globalFilterField = globalFilterFieldsArray[j];
globalMatch = FilterUtils[this.filters['global'].matchMode || FilterMatchMode.CONTAINS](ObjectUtils.resolveFieldData(data[i], globalFilterField), this.filters['global'].value, this.filterLocale);
globalMatch = FilterService.filters[this.filters['global'].matchMode || FilterMatchMode.CONTAINS](ObjectUtils.resolveFieldData(data[i], globalFilterField), this.filters['global'].value, this.filterLocale);
if (globalMatch) {
break;
@ -682,7 +682,7 @@ export default {
let filterValue = filterMeta.value;
let filterMatchMode = filterMeta.matchMode || FilterMatchMode.STARTS_WITH;
let dataFieldValue = ObjectUtils.resolveFieldData(rowData, field);
let filterConstraint = FilterUtils[filterMatchMode];
let filterConstraint = FilterService.filters[filterMatchMode];
return filterConstraint(dataFieldValue, filterValue, this.filterLocale);
},

View File

@ -75,9 +75,8 @@
</template>
<script>
import {ObjectUtils} from 'primevue/utils';
import {FilterUtils} from 'primevue/utils';
import {DomHandler} from 'primevue/utils';
import {ObjectUtils,DomHandler} from 'primevue/utils';
import {FilterService} from 'primevue/api';
import TreeTableRow from './TreeTableRow.vue';
import Paginator from 'primevue/paginator';
@ -564,7 +563,7 @@ export default {
if (Object.prototype.hasOwnProperty.call(this.filters, this.columnProp(col, 'field'))) {
let filterMatchMode = this.columnProp(col, 'filterMatchMode') || 'startsWith';
let filterValue = this.filters[this.columnProp(col, 'field')];
let filterConstraint = FilterUtils[filterMatchMode];
let filterConstraint = FilterService.filters[filterMatchMode];
let paramsWithoutNode = {filterField, filterValue, filterConstraint, strict};
if ((strict && !(this.findFilteredNodes(copyNode, paramsWithoutNode) || this.isFilterMatched(copyNode, paramsWithoutNode))) ||
@ -581,7 +580,7 @@ export default {
if (this.hasGlobalFilter() && !globalMatch) {
let copyNodeForGlobal = {...copyNode};
let filterValue = this.filters['global'];
let filterConstraint = FilterUtils['contains'];
let filterConstraint = FilterService.filters['contains'];
let globalFilterParamsWithoutNode = {filterField, filterValue, filterConstraint, strict};
if ((strict && (this.findFilteredNodes(copyNodeForGlobal, globalFilterParamsWithoutNode) || this.isFilterMatched(copyNodeForGlobal, globalFilterParamsWithoutNode))) ||

View File

@ -1,211 +0,0 @@
import ObjectUtils from './ObjectUtils';
export default class FilterUtils {
static startsWith(value, filter, filterLocale) {
if (filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.slice(0, filterValue.length) === filterValue;
}
static contains(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.indexOf(filterValue) !== -1;
}
static notContains(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.indexOf(filterValue) === -1;
}
static endsWith(value, filter, filterLocale) {
if (filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if (value === undefined || value === null) {
return false;
}
let filterValue = ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
let stringValue = ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale);
return stringValue.indexOf(filterValue, stringValue.length - filterValue.length) !== -1;
}
static equals(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() === filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) == ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
}
static notEquals(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return false;
}
if (value === undefined || value === null) {
return true;
}
if (value.getTime && filter.getTime)
return value.getTime() !== filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) != ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
}
static in(value, filter) {
if (filter === undefined || filter === null || filter.length === 0) {
return true;
}
for (let i = 0; i < filter.length; i++) {
if (ObjectUtils.equals(value, filter[i])) {
return true;
}
}
return false;
}
static between(value, filter) {
if (filter == null || filter[0] == null || filter[1] == null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime)
return filter[0].getTime() <= value.getTime() && value.getTime() <= filter[1].getTime();
else
return filter[0] <= value && value <= filter[1];
}
static lt(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() < filter.getTime();
else
return value < filter;
}
static lte(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() <= filter.getTime();
else
return value <= filter;
}
static gt(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() > filter.getTime();
else
return value > filter;
}
static gte(value, filter) {
if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() >= filter.getTime();
else
return value >= filter;
}
static is(value, filter, filterLocale) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if (value === undefined || value === null) {
return false;
}
if (value.getTime && filter.getTime)
return value.getTime() === filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) == ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
}
static isNot(value, filter, filterLocale) {
return this.filters.notEquals(value, filter, filterLocale);
}
static before(value, filter, filterLocale) {
return this.filters.lt(value, filter, filterLocale);
}
static after(value, filter, filterLocale) {
return this.filters.gt(value, filter, filterLocale);
}
}

View File

@ -1,7 +1,6 @@
import ConnectedOverlayScrollHandler from './ConnectedOverlayScrollHandler';
import DomHandler from './DomHandler';
import ObjectUtils from './ObjectUtils';
import FilterUtils from './FilterUtils';
import UniqueComponentId from './UniqueComponentId';
export {ConnectedOverlayScrollHandler,DomHandler,ObjectUtils,FilterUtils,UniqueComponentId};
export {ConnectedOverlayScrollHandler,DomHandler,ObjectUtils,UniqueComponentId};