From 2603481cc6487b0932458ebc7eb4e53202d3f24e Mon Sep 17 00:00:00 2001 From: Yoav Schneiderman Date: Tue, 3 Dec 2019 12:55:39 +0200 Subject: Adding Template button to new service instance + cypress test + filter Issue-ID: VID-724 Change-Id: Ie69a32dd6b74de57fd747a92935c1ce5edb86351 Signed-off-by: Yoav Schneiderman Signed-off-by: Ittay Stern --- .../pipes/searchFilter/search-filter.pipe.spec.ts | 2 +- .../pipes/searchFilter/search-filter.pipe.ts | 37 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) (limited to 'vid-webpack-master/src/app/shared/pipes') diff --git a/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.spec.ts b/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.spec.ts index cbf7324ac..2567cbf27 100644 --- a/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.spec.ts +++ b/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.spec.ts @@ -5,7 +5,7 @@ describe('Search filter pipe', () => { const items= [{'id':1, 'name': 'aaa'}, {'id':12, 'name': 'bbb', 'children':{'first': 155, 'second': 2, 'third': 3}}, - {'id':3, 'name': 'ccc', 'children':{'first': 1, 'BbB': '3', 'third': 3}}, + {'id':3, 'name': 'ccc', 'children':{'first': 1, 'BbB': 'BbB', 'third': 3}}, {'id':4, 'name': 'aad', 'children':{'first': 1, 'second': 2, 'third': 3}}]; test('should return items contains substring bb', () => { diff --git a/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.ts b/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.ts index 725eacb53..6e5cfc667 100644 --- a/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.ts +++ b/vid-webpack-master/src/app/shared/pipes/searchFilter/search-filter.pipe.ts @@ -1,14 +1,43 @@ import {Pipe, PipeTransform} from '@angular/core'; +import * as _ from 'lodash'; @Pipe({ name: 'searchFilter' }) export class SearchFilterPipe implements PipeTransform { transform(items: Object[], searchText: string): any[] { - if(!items) return []; - if(!searchText) return items; - return items.filter( item => { - return JSON.stringify(item).toLowerCase().includes(searchText.toLowerCase()); + if (!items) return []; + if (!searchText) return items; + return items.filter((item: object) => { + + const deepFlatObject = this.flatten(item); + + const values = _.values(deepFlatObject).map((item: string) => { + return item.toString().toLowerCase() + }); + + return _.some(values, _.method('includes', searchText.toLowerCase())); }); } + + flatten = object => { + return Object.assign( + {}, + ...(function _flatten(objectBit, path = '') { + //spread the result into our return object + if(objectBit === null) return []; + return [].concat( + //concat everything into one level + + ...Object.keys(objectBit).map( + //iterate over object + key => + typeof objectBit[key] === 'object' //check if there is a nested object + ? _flatten(objectBit[key], `${path}/${key}`) //call itself if there is + : { [`${path}/${key}`]: objectBit[key] } //append object with it’s path as key + ) + ); + })(object) + ); + }; } -- cgit 1.2.3-korg