Skip to content

Commit

Permalink
Fixed #1865 - New FilterService Utility
Browse files Browse the repository at this point in the history
  • Loading branch information
tugcekucukoglu committed Dec 10, 2021
1 parent 16f1df6 commit a4c1bc3
Show file tree
Hide file tree
Showing 11 changed files with 750 additions and 88 deletions.
50 changes: 50 additions & 0 deletions src/components/api/Api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export const FilterMatchMode: {
STARTS_WITH?: string,
CONTAINS?: string,
NOT_CONTAINS?: string,
ENDS_WITH?: string,
EQUALS?: string,
NOT_EQUALS?: string,
IN?: string,
LESS_THAN?: string,
LESS_THAN_OR_EQUAL_TO?: string,
GREATER_THAN?: string,
GREATER_THAN_OR_EQUAL_TO?: string,
BETWEEN?: string,
DATE_IS?: string,
DATE_IS_NOT?: string,
DATE_BEFORE?: string,
DATE_AFTER?: string
}

export const FilterOperator: {
AND?: string,
OR?: string
}

interface FiltersOptions {
startsWith?(value: any, filter: any, filterLocale: string): boolean,
contains?(value: any, filter: any, filterLocale: string): boolean,
notContains?(value: any, filter: any, filterLocale: string): boolean,
endsWith?(value: any, filter: any, filterLocale: string): boolean,
equals?(value: any, filter: any, filterLocale: string): boolean,
notEquals?(value: any, filter: any, filterLocale: string): boolean,
in?(value: any, filter: any): boolean,
between?(value: any, filter: any): boolean,
lt?(value: any, filter: any): boolean,
lte?(value: any, filter: any): boolean,
gt?(value: any, filter: any): boolean,
gte?(value: any, filter: any): boolean,
dateIs?(value: any, filter: any): boolean,
dateIsNot?(value: any, filter: any): boolean,
dateBefore?(value: any, filter: any): boolean,
dateAfter?(value: any, filter: any): boolean
}

interface FilterServiceOptions {
filter?(value: any[], fields: any[], filterValue: any, filterMatchMode: string, filterLocale: string): any[],
filters?: FiltersOptions,
register?(rule: string, fn: any): void
}

export const FilterService: FilterServiceOptions;
5 changes: 5 additions & 0 deletions src/components/api/Api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import FilterMatchMode from './FilterMatchMode';
import FilterOperator from './FilterOperator';
import FilterService from './FilterService';

export {FilterMatchMode,FilterOperator,FilterService};
20 changes: 20 additions & 0 deletions src/components/api/FilterMatchMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const FilterMatchMode = {
STARTS_WITH : 'startsWith',
CONTAINS : 'contains',
NOT_CONTAINS : 'notContains',
ENDS_WITH : 'endsWith',
EQUALS : 'equals',
NOT_EQUALS : 'notEquals',
IN : 'in',
LESS_THAN : 'lt',
LESS_THAN_OR_EQUAL_TO : 'lte',
GREATER_THAN : 'gt',
GREATER_THAN_OR_EQUAL_TO : 'gte',
BETWEEN : 'between',
DATE_IS : 'dateIs',
DATE_IS_NOT : 'dateIsNot',
DATE_BEFORE : 'dateBefore',
DATE_AFTER : 'dateAfter'
}

export default FilterMatchMode;
6 changes: 6 additions & 0 deletions src/components/api/FilterOperator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const FilterOperator = {
AND: 'and',
OR: 'or'
}

export default FilterOperator;
240 changes: 240 additions & 0 deletions src/components/api/FilterService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import ObjectUtils from '../utils/ObjectUtils';

const FilterService = {
filter(value, fields, filterValue, filterMatchMode, filterLocale) {
let filteredItems = [];

if (value) {
for (let item of value) {
for (let field of fields) {
let fieldValue = ObjectUtils.resolveFieldData(item, field);

if (this.filters[filterMatchMode](fieldValue, filterValue, filterLocale)) {
filteredItems.push(item);
break;
}
}
}
}

return filteredItems;
},
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;
},
dateIs(value, filter) {
if (filter === undefined || filter === null) {
return true;
}

if (value === undefined || value === null) {
return false;
}

return value.toDateString() === filter.toDateString();
},
dateIsNot(value, filter) {
if (filter === undefined || filter === null) {
return true;
}

if (value === undefined || value === null) {
return false;
}

return value.toDateString() !== filter.toDateString();
},
dateBefore(value, filter) {
if (filter === undefined || filter === null) {
return true;
}

if (value === undefined || value === null) {
return false;
}

return value.getTime() < filter.getTime();
},
dateAfter(value, filter) {
if (filter === undefined || filter === null) {
return true;
}

if (value === undefined || value === null) {
return false;
}

return value.getTime() > filter.getTime();
}
},
register(rule, fn) {
this.filters[rule] = fn;
}
}

export default FilterService;
1 change: 1 addition & 0 deletions src/components/api/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Api';
2 changes: 2 additions & 0 deletions src/components/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require('./Api.js');
4 changes: 4 additions & 0 deletions src/components/api/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Vue from 'vue';
import PrimeVue from 'primevue/config';

Vue.use(PrimeVue);
Loading

0 comments on commit a4c1bc3

Please sign in to comment.