-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #1865 - New FilterService Utility
- Loading branch information
1 parent
16f1df6
commit a4c1bc3
Showing
11 changed files
with
750 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const FilterOperator = { | ||
AND: 'and', | ||
OR: 'or' | ||
} | ||
|
||
export default FilterOperator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
module.exports = require('./Api.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.