import { Pipe , PipeTransform } from '@angular/core' ; import { GtConfigField } from '../interfaces/gt-config-field' ; import { GtRow } from '../interfaces/gt-row' ; import { GtConfigSetting } from '../interfaces/gt-config-setting' ; import { GtInformation } from '../interfaces/gt-information' ; @ Pipe ( { name : 'gtSearch' } ) export class GtSearchPipe < R extends GtRow > implements PipeTransform { /** Return property */ private getProperty = function ( array , key ) { for ( let i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] . objectKey === key ) { return array [ i ] ; } } } ; transform ( allRows , searchTerms , gtInfo , settings , fields , refreshData ) { if ( ! searchTerms || searchTerms . replace ( /"/g , '' ) . length === 0 ) { const length = allRows === null ? 0 : allRows . length ; gtInfo . recordsAfterSearch = length ; return allRows ; } const searchFunction = { } ; const fieldsTemp = [ ] ; for ( let k = 0 ; k < fields . length ; k ++ ) { const field = fields [ k ] ; const include = this . getProperty ( settings , field . objectKey ) . search === false ? false : true ; if ( include ) { if ( typeof field . search === 'function' ) { searchFunction [ field . objectKey ] = field . search ; } else if ( typeof field . value === 'function' ) { searchFunction [ field . objectKey ] = field . value ; } fieldsTemp . push ( field ) ; } } const filteredRows = [ ] ; searchTerms = typeof searchTerms === 'undefined' ? '' : searchTerms ; const searchTermsArray = searchTerms . toLowerCase ( ) . match ( /(".*"|[^\s]+)/g ) ; for ( let i = 0 ; i < allRows . length ; i ++ ) { const row = allRows [ i ] ; let string = '' ; for ( let j = 0 ; j < fieldsTemp . length ; j ++ ) { const separator = j === 0 ? '' : ' & ' ; string += searchFunction [ fieldsTemp [ j ] . objectKey ] ? separator + searchFunction [ fieldsTemp [ j ] . objectKey ] ( row , j ) : separator + row [ fieldsTemp [ j ] . objectKey ] ; } string = string . toLowerCase ( ) ; let match = true ; for ( let k = 0 ; k < searchTermsArray . length ; k ++ ) { const term = searchTermsArray [ k ] . replace ( /"/g , '' ) ; match = string . indexOf ( term ) !== - 1 ; if ( ! match ) { break ; } } if ( match ) { filteredRows . push ( row ) ; } } gtInfo . recordsAfterSearch = filteredRows . length ; return filteredRows ; } }