Add filterutils

pull/12/head
cagataycivici 2019-05-28 15:56:58 +03:00
parent 481a0fcbf3
commit c5dce96fbd
2 changed files with 81 additions and 82 deletions

View File

@ -0,0 +1,81 @@
export default class FilterUtils {
static startsWith(value, filter) {
if(filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if(value === undefined || value === null) {
return false;
}
let filterValue = filter.toLowerCase();
return value.toString().toLowerCase().slice(0, filterValue.length) === filterValue;
}
static contains(value, filter) {
if(filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if(value === undefined || value === null) {
return false;
}
return value.toString().toLowerCase().indexOf(filter.toLowerCase()) !== -1;
}
static endsWith(value, filter) {
if(filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if(value === undefined || value === null) {
return false;
}
let filterValue = filter.toString().toLowerCase();
return value.toString().toLowerCase().indexOf(filterValue, value.toString().length - filterValue.length) !== -1;
}
static equals(value, filter) {
if(filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if(value === undefined || value === null) {
return false;
}
return value.toString().toLowerCase() === filter.toString().toLowerCase();
}
static notEquals(value, filter) {
if(filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return false;
}
if(value === undefined || value === null) {
return true;
}
return value.toString().toLowerCase() !== filter.toString().toLowerCase();
}
static in(value, filter) {
if(filter === undefined || filter === null || filter.length === 0) {
return true;
}
if(value === undefined || value === null) {
return false;
}
for(let i = 0; i < filter.length; i++) {
if(filter[i] === value)
return true;
}
return false;
}
}

View File

@ -130,86 +130,4 @@ export default class ObjectUtils {
return false;
}
static filterConstraints = {
startsWith(value, filter) {
if(filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if(value === undefined || value === null) {
return false;
}
let filterValue = filter.toLowerCase();
return value.toString().toLowerCase().slice(0, filterValue.length) === filterValue;
},
contains(value, filter) {
if(filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if(value === undefined || value === null) {
return false;
}
return value.toString().toLowerCase().indexOf(filter.toLowerCase()) !== -1;
},
endsWith(value, filter) {
if(filter === undefined || filter === null || filter.trim() === '') {
return true;
}
if(value === undefined || value === null) {
return false;
}
let filterValue = filter.toString().toLowerCase();
return value.toString().toLowerCase().indexOf(filterValue, value.toString().length - filterValue.length) !== -1;
},
equals(value, filter) {
if(filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return true;
}
if(value === undefined || value === null) {
return false;
}
return value.toString().toLowerCase() === filter.toString().toLowerCase();
},
notEquals(value, filter) {
if(filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) {
return false;
}
if(value === undefined || value === null) {
return true;
}
return value.toString().toLowerCase() !== filter.toString().toLowerCase();
},
in(value, filter) {
if(filter === undefined || filter === null || filter.length === 0) {
return true;
}
if(value === undefined || value === null) {
return false;
}
for(let i = 0; i < filter.length; i++) {
if(filter[i] === value)
return true;
}
return false;
}
}
}