Home:ALL Converter>Create a pipe to filter exact search in Angular

Create a pipe to filter exact search in Angular

Ask Time:2019-07-29T21:59:25         Author:Ryan Coolwebs

Json Formatter

I have an Angular app that runs queries over a twitter api. I want to be able to run search queries over the api and pull back tweets that contain certain hashtags. I am using an input search field which has data binding.

So I want it to be an exact search and be case sensitive. For this I believe I need to setup a custom pipe. I have done so by following what another SO user has done in a related question here Custom filter case sensitive but it is still not working for me. Any recommendations as to what I am doing wrong?

To be honest, the custom pipe I am using borrows alot of code patterns that I am unfamiliar with and maybe incorrect or not necessary but I can't find anything else on the web (besides AngularJS tutorials) on the topic.

HTML template

  <!--Search input field to filter table data-->
  <div class="search-container" style="direction: rtl;">
    <mat-form-field>
      <mat-icon matPrefix aria-hidden="false" aria-label="Search">search</mat-icon>
      <input matInput #hashtagsSearchInput placeholder="Search by hashtag">
    </mat-form-field>
  </div>

ExactSearch Pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'exactSearch'
})
export class ExactSearchPipe implements PipeTransform {

    transform(aliases: any, hashtagsSearchInput: any): any {
        // check if search term is undefined
        if (hashtagsSearchInput === undefined) return aliases;
        // return updated array
        return aliases.filter(function(alias){
            return alias.local_part.includes(hashtagsSearchInput) || alias.domain.includes(hashtagsSearchInput);
        });
    }
}

Author:Ryan Coolwebs,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57255453/create-a-pipe-to-filter-exact-search-in-angular
yy