sd4-github / mycart_project

angular project
https://mycart-project.vercel.app
0 stars 0 forks source link

ng2searchfilter ommit custom pipe added and old serverUrl changed to local #42

Open sd4-github opened 1 year ago

sd4-github commented 1 year ago

ng2searchfilter omit

https://github.com/solodynamo/ng2-search-filter/issues/55#issuecomment-607651243

ng generate pipe CustomFilterPipe

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

@Pipe({
  name: "filter",
  pure: false
})
@Injectable()
export class CustomFilterPipe implements PipeTransform {

  transform(items: any, term: string, excludes: any = []): any {
    if (!term || !items) return items;
    return CustomFilterPipe.filter(items, term, excludes);
  }

  static filter(
    items: Array<{ [key: string]: any }>,
    term: string,
    excludes: any
  ): Array<{ [key: string]: any }> {
    const toCompare = term.toLowerCase();

    function checkInside(item: any, term: string) {
      if (
        typeof item === "string" &&
        item
          .toString()
          .toLowerCase()
          .includes(toCompare)
      ) {
        return true;
      }

      for (let property in item) {
        if (
          item[property] === null ||
          item[property] == undefined ||
          excludes.includes(property)
        ) {
          continue;
        }

        if (typeof item[property] === "object") {
          if (checkInside(item[property], term)) {
            return true;
          }
        } else if (
          item[property]
            .toString()
            .toLowerCase()
            .includes(toCompare)
        ) {
          return true;
        }
      }
      return false;
    }

    return items.filter(function (item) {
      return checkInside(item, term);
    });
  }
}
sd4-github commented 1 year ago

serverUrl change

old heroku prod url "https://backend-api1.herokuapp.com" replaced by "http://localhost:3200/"

arcsector commented 9 months ago

If anyone is looking for a solution to not use type any in https://github.com/solodynamo/ng2-search-filter/issues/55#issuecomment-607651243, try using template types instead of any - in this I've removed the excludes arg, however you can add it back as templated if you'd like:

// Created from https://github.com/solodynamo/ng2-search-filter/issues/55#issuecomment-607651243
import { Pipe, PipeTransform, Injectable } from '@angular/core';

@Pipe({
    name: 'filter',
    pure: false
})
@Injectable()
export class FilterPipe implements PipeTransform {
    transform<T>(items: Array<T>, term: string): Array<T> {
        if (!term || !items) return items;
        return FilterPipe.filter(items, term);
    }

    static filter<T>(items: Array<T>, term: string): Array<T> {
        const toCompare = term.toLowerCase();

        function checkInside(item: T, term: string) {
            if (
                typeof item === 'string' &&
                item.toString().toLowerCase().includes(toCompare)
            ) {
                return true;
            }

            for (const property in item) {
                if (item[property] === null || item[property] == undefined) {
                    continue;
                }

                if (typeof item[property] === 'object') {
                    if (checkInside(item[property] as T, term)) {
                        return true;
                    }
                } else if (
                    item[property].toString().toLowerCase().includes(toCompare)
                ) {
                    return true;
                }
            }
            return false;
        }

        return items.filter((item) => {
            return checkInside(item, term);
        });
    }
}