Skip to content

Instantly share code, notes, and snippets.

@nathandaly
Created November 11, 2017 11:35
Show Gist options
  • Save nathandaly/e27bef6707938fd185362afa38999c35 to your computer and use it in GitHub Desktop.
Save nathandaly/e27bef6707938fd185362afa38999c35 to your computer and use it in GitHub Desktop.
import { Component, NgModule, OnInit } from '@angular/core';
import { Pipe, PipeTransform } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import _ from 'lodash';
@Pipe({name: 'Search'})
export class SearchService implements PipeTransform {
transform(obj: any, term) {
if (obj != null && term) {
return obj.filter(el => {
let testString = JSON.stringify(el);
Object.keys(el).forEach(k => {
testString = testString.replace(k, '');
});
const terms = term.replace(/[\s]+/gm, '').replace(/^[\s]|[\s]$/gm, '').split(' ');
let containCount = 0;
terms.forEach(item => {
if (testString.toLowerCase().indexOf(item.toLowerCase()) > -1) {
++containCount;
}
});
return (containCount === terms.length);
});
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment