From 4983330e7af070542d0cecd648c04a8a362401cd Mon Sep 17 00:00:00 2001 From: Cagatay Civici Date: Mon, 8 Feb 2021 15:02:05 +0300 Subject: [PATCH] Removed FilterUtils in favor of the new FilterService API --- src/components/api/Api.js | 3 +- src/components/api/FilterService.js | 202 +++++++++++++++++++++++ src/components/datatable/DataTable.vue | 8 +- src/components/treetable/TreeTable.vue | 9 +- src/components/utils/FilterUtils.js | 211 ------------------------- src/components/utils/Utils.js | 3 +- 6 files changed, 213 insertions(+), 223 deletions(-) create mode 100644 src/components/api/FilterService.js delete mode 100755 src/components/utils/FilterUtils.js diff --git a/src/components/api/Api.js b/src/components/api/Api.js index cedfb0ea2..398c1db5d 100644 --- a/src/components/api/Api.js +++ b/src/components/api/Api.js @@ -1,4 +1,5 @@ import FilterMatchMode from './FilterMatchMode'; import FilterOperator from './FilterOperator'; +import FilterService from './FilterService'; -export {FilterMatchMode,FilterOperator}; \ No newline at end of file +export {FilterMatchMode,FilterOperator,FilterService}; \ No newline at end of file diff --git a/src/components/api/FilterService.js b/src/components/api/FilterService.js new file mode 100644 index 000000000..25bd0261b --- /dev/null +++ b/src/components/api/FilterService.js @@ -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; \ No newline at end of file diff --git a/src/components/datatable/DataTable.vue b/src/components/datatable/DataTable.vue index 167367b8d..54f0920a0 100755 --- a/src/components/datatable/DataTable.vue +++ b/src/components/datatable/DataTable.vue @@ -132,8 +132,8 @@