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

@Pipe({
  name: 'arrayFilter'
})

export class ArrayFilterPipe implements PipeTransform {

  constructor() {

  }

  transform(array: any, args: any): any {
    if (array === undefined) {
      return [];
    }

    const rArray = [];
    for (const v of array) {
      if (typeof(args) === 'object') {
        for (const s in args) {
          if (args[s] === undefined || (v[s] !== undefined && (v[s] + '').indexOf(args[s] + '') > -1)) {
            rArray.push(v);
            break;
          }
        }
      } else {
        if (args === undefined) {
          rArray.push(v);
        } else {
          for (const s in v) {
            if (v[s] !== undefined && (v[s] + '').indexOf(args + '') > -1) {
              rArray.push(v);
              break;
            }
          }
        }
      }
    }
    return rArray;
  }
}
