diff --git a/src/app/core/fuseUtils.ts b/src/app/core/fuseUtils.ts new file mode 100644 index 00000000..71bcb5a0 --- /dev/null +++ b/src/app/core/fuseUtils.ts @@ -0,0 +1,82 @@ +export class FuseUtils +{ + + public static filterArrayByString(mainArr, searchText) + { + searchText = searchText.toLowerCase(); + + return mainArr.filter(itemObj => { + return this.searchInObj(itemObj, searchText); + }); + } + + public static searchInObj(itemObj, searchText) + { + + for ( const prop in itemObj ) + { + if ( !itemObj.hasOwnProperty(prop) ) + { + continue; + } + + const value = itemObj[prop]; + + if ( typeof value === 'string' ) + { + if ( this.searchInSting(value, searchText) ) + { + return true; + } + } + + else if ( Array.isArray(value) ) + { + if ( this.searchInArray(value, searchText) ) + { + return true; + } + + } + + if ( typeof value === 'object' ) + { + if ( this.searchInObj(value, searchText) ) + { + return true; + } + } + } + } + + public static searchInArray(arr, searchText) + { + for ( const value of arr ) + { + if ( typeof value === 'string' ) + { + if ( this.searchInSting(value, searchText) ) + { + return true; + } + } + + if ( typeof value === 'object' ) + { + if ( this.searchInObj(value, searchText) ) + { + return true; + } + } + } + } + + public static searchInSting(value, searchText) + { + if ( value.toLowerCase().includes(searchText) ) + { + return true; + } + return false; + } +} diff --git a/src/app/core/pipes/filter.pipe.ts b/src/app/core/pipes/filter.pipe.ts index bc8835b0..590d9d59 100644 --- a/src/app/core/pipes/filter.pipe.ts +++ b/src/app/core/pipes/filter.pipe.ts @@ -1,152 +1,11 @@ -/** - * Created by vadimdez on 28/06/16. - */ -import { Pipe, Injectable } from '@angular/core'; +import { Pipe, PipeTransform } from '@angular/core'; +import { FuseUtils } from '../fuseUtils'; -@Pipe({ - name: 'filterBy', - pure: false -}) - -@Injectable() -export class FilterPipe { - - private filterByString(filter) { - if (filter) { - filter = filter.toLowerCase(); - } - return value => { - return !filter || (value ? ('' + value).toLowerCase().indexOf(filter) !== -1 : false); - } - } - - private filterByBoolean(filter) { - return value => { - return Boolean(value) === filter; - } - } - - private filterByObject(filter) { - return value => { - for (let key in filter) { - - if (key === '$or') { - if (!this.filterByOr(filter.$or)(this.getValue(value))) { - return false; - } - continue; - } - - if (!value.hasOwnProperty(key) && !Object.getOwnPropertyDescriptor(Object.getPrototypeOf(value), key)) { - return false; - } - - let val = this.getValue(value[key]); - const filterType = typeof filter[key]; - let isMatching; - - if (filterType === 'boolean') { - isMatching = this.filterByBoolean(filter[key])(val); - } else if (filterType === 'string') { - isMatching = this.filterByString(filter[key])(val); - } else if (filterType === 'object') { - isMatching = this.filterByObject(filter[key])(val); - } else { - isMatching = this.filterDefault(filter[key])(val); - } - - if (!isMatching) { - return false; - } - } - - return true; - } - } - - /** - * Filter value by $or - * - * @param filter - * @returns {(value:any)=>boolean} - */ - private filterByOr(filter: any[]) { - return (value: any) => { - let hasMatch = false; - const length = filter.length; - const isArray = value instanceof Array; - - const arrayComparison = (i) => { - return value.indexOf(filter[i]) !== -1; - }; - const otherComparison = (i) => { - return value === filter[i]; - }; - const comparison = isArray ? arrayComparison : otherComparison; - - for (let i = 0; i < length; i++) { - if (comparison(i)) { - hasMatch = true; - break; - } - } - - return hasMatch; - }; - } - - /** - * Checks function's value if type is function otherwise same value - * @param value - * @returns {any} - */ - private getValue(value: any) { - return typeof value === 'function' ? value() : value; - } - - /** - * Defatul filterDefault function - * - * @param filter - * @returns {(value:any)=>boolean} - */ - private filterDefault(filter) { - return value => { - return filter === undefined || filter == value; - } - } - - private isNumber(value) { - return !isNaN(parseInt(value, 10)) && isFinite(value); - } - - transform(array: any[], filter: any): any { - const type = typeof filter; - - if (!array) { - return array; - } - - if (type === 'boolean') { - return array.filter(this.filterByBoolean(filter)); - } - - if (type === 'string') { - if (this.isNumber(filter)) { - return array.filter(this.filterDefault(filter)); - } - - return array.filter(this.filterByString(filter)); - } - - if (type === 'object') { - return array.filter(this.filterByObject(filter)); - } - - if (type === 'function') { - return array.filter(filter); - } - - return array.filter(this.filterDefault(filter)); +@Pipe({name: 'filter'}) +export class FilterPipe implements PipeTransform +{ + transform(mainArr: any[], searchText: string, property: string): any + { + return FuseUtils.filterArrayByString(mainArr, searchText); } } diff --git a/src/app/main/apps/chat/sidenavs/left/chats/chats.component.html b/src/app/main/apps/chat/sidenavs/left/chats/chats.component.html index 04cb1cc8..83b0b320 100644 --- a/src/app/main/apps/chat/sidenavs/left/chats/chats.component.html +++ b/src/app/main/apps/chat/sidenavs/left/chats/chats.component.html @@ -82,7 +82,7 @@ search - + @@ -106,12 +106,12 @@
-
+
Chats