All files with-filter.ts

83.33% Statements 15/18
50% Branches 1/2
80% Functions 8/10
82.35% Lines 14/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 421x       1x 2x 3x   3x 2x   2x       2x 2x 2x               3x   2x     1x           3x          
import { $$asyncIterator } from 'iterall';
 
export type FilterFn = (rootValue?: any, args?: any, context?: any, info?: any) => boolean;
 
export const withFilter = (asyncIteratorFn: () => AsyncIterator<any>, filterFn: FilterFn) => {
  return (rootValue: any, args: any, context: any, info: any): AsyncIterator<any> => {
    const asyncIterator = asyncIteratorFn();
 
    const getNextPromise = () => {
      return asyncIterator
        .next()
        .then(payload => Promise.all([
          payload,
          Promise.resolve(filterFn(payload.value, args, context, info)).catch(() => false),
        ]))
        .then(([payload, filterResult]) => {
          Eif (filterResult === true) {
            return payload;
          }
 
          // Skip the current value and wait for the next one
          return getNextPromise();
        });
    };
 
    return {
      next() {
        return getNextPromise();
      },
      return() {
        return asyncIterator.return();
      },
      throw(error) {
        return asyncIterator.throw(error);
      },
      [$$asyncIterator]() {
        return this;
      },
    } as any;
  };
};