-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54e5323
commit 6e36f9e
Showing
22 changed files
with
466 additions
and
481 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,228 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ObjectUtils } from '../utils/objectutils'; | ||
|
||
@Injectable({providedIn: 'root'}) | ||
export class FilterService { | ||
|
||
filter(value: any[], fields: any[], filterValue: string, filterMatchMode: string, filterLocale?: string) { | ||
let filteredItems: any[] = []; | ||
let filterText = ObjectUtils.removeAccents(filterValue).toLocaleLowerCase(filterLocale); | ||
|
||
if (value) { | ||
for (let item of value) { | ||
for (let field of fields) { | ||
let fieldValue = ObjectUtils.removeAccents(String(ObjectUtils.resolveFieldData(item, field))).toLocaleLowerCase(filterLocale); | ||
|
||
if (this.filters[filterMatchMode](fieldValue, filterText, filterLocale)) { | ||
filteredItems.push(item); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return filteredItems; | ||
} | ||
|
||
public filters = { | ||
startsWith: (value, filter, filterLocale?):boolean => { | ||
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?):boolean => { | ||
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?):boolean => { | ||
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?):boolean => { | ||
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?):boolean => { | ||
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?):boolean => { | ||
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: any[]):boolean => { | ||
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: any[]):boolean => { | ||
if (filter == null || filter[0] == null || filter[0] == 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, filterLocale?):boolean => { | ||
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, filterLocale?):boolean => { | ||
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, filterLocale?):boolean => { | ||
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, filterLocale?):boolean => { | ||
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?):boolean => { | ||
return this.filters.equals(value, filter, filterLocale); | ||
}, | ||
|
||
isNot: (value, filter, filterLocale?): boolean => { | ||
return this.filters.notEquals(value, filter, filterLocale); | ||
}, | ||
|
||
before: (value, filter, filterLocale?): boolean => { | ||
return this.filters.lt(value, filter, filterLocale); | ||
}, | ||
|
||
after: (value, filter, filterLocale?): boolean => { | ||
return this.filters.gt(value, filter, filterLocale); | ||
} | ||
|
||
} | ||
|
||
register(rule: string, fn: Function) { | ||
this.filters[rule] = fn; | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.