All files pipe.ts

100% Statements 77/77
100% Branches 30/30
100% Functions 3/3
100% Lines 73/73

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                                                                                                                            44x   192x   99x 99x 192x 192x 119x 119x 119x 119x 119x 119x   73x   99x 99x 130x 130x 130x 73x 73x 73x   57x 57x 124x 119x 119x 15x     5x       57x   57x 159x 159x 28x     57x 57x 15x   42x   57x   99x                           169x 169x 169x           169x 5x 5x   164x 164x 164x 315x 315x 315x 315x 315x 315x 315x 315x 276x 6x 10x 10x         10x 1x     5x   270x     309x 39x       270x 28x     158x 119x   158x 28x   130x    
import { LazyResult } from './_reduceLazy';
 
/**
 * Perform left-to-right function composition.
 * @param value The initial value.
 * @param operations the list of operations to apply.
 * @signature P.pipe(data, op1, op2, op3)
 * @example
 *    P.pipe(
 *      [1, 2, 3, 4],
 *      P.map(x => x * 2),
 *      arr => [arr[0] + arr[1], arr[2] + arr[3]],
 *    ) // => [6, 14]
 *
 *
 * @data_first
 * @category Function
 */
export function pipe<A, B>(value: A, op1: (input: A) => B): B;
export function pipe<A, B, C>(
  value: A,
  op1: (input: A) => B,
  op2: (input: B) => C
): C;
 
export function pipe<A, B, C, D>(
  value: A,
  op1: (input: A) => B,
  op2: (input: B) => C,
  op3: (input: C) => D
): D;
 
export function pipe<A, B, C, D, E>(
  value: A,
  op1: (input: A) => B,
  op2: (input: B) => C,
  op3: (input: C) => D,
  op4: (input: D) => E
): E;
 
export function pipe<A, B, C, D, E, F>(
  value: A,
  op1: (input: A) => B,
  op2: (input: B) => C,
  op3: (input: C) => D,
  op4: (input: D) => E,
  op5: (input: E) => F
): F;
 
export function pipe<A, B, C, D, E, F, G>(
  value: A,
  op1: (input: A) => B,
  op2: (input: B) => C,
  op3: (input: C) => D,
  op4: (input: D) => E,
  op5: (input: E) => F,
  op6: (input: F) => G
): G;
 
export function pipe<A, B, C, D, E, F, G, H>(
  value: A,
  op1: (input: A) => B,
  op2: (input: B) => C,
  op3: (input: C) => D,
  op4: (input: D) => E,
  op5: (input: E) => F,
  op6: (input: F) => G,
  op7: (input: G) => H
): H;
 
export function pipe(
  value: any,
  ...operations: Array<(value: any) => any>
): any {
  let ret = value;
  const lazyOps = operations.map(op => {
    const { lazy, lazyArgs } = op as LazyOp;
    if (lazy) {
      const fn: any = lazy(...lazyArgs);
      fn.indexed = lazy.indexed;
      fn.single = lazy.single;
      fn.index = 0;
      fn.items = [];
      return fn;
    }
    return null;
  });
  let opIdx = 0;
  while (opIdx < operations.length) {
    const op = operations[opIdx];
    const lazyOp = lazyOps[opIdx];
    if (!lazyOp) {
      ret = op(ret);
      opIdx++;
      continue;
    }
    const lazySeq: LazyFn[] = [];
    for (let j = opIdx; j < operations.length; j++) {
      if (lazyOps[j]) {
        lazySeq.push(lazyOps[j]);
        if (lazyOps[j].single) {
          break;
        }
      } else {
        break;
      }
    }
 
    let acc: any[] = [];
 
    for (let j = 0; j < ret.length; j++) {
      let item = ret[j];
      if (_processItem({ item, acc, lazySeq })) {
        break;
      }
    }
    const lastLazySeq = lazySeq[lazySeq.length - 1];
    if ((lastLazySeq as any).single) {
      ret = acc[0];
    } else {
      ret = acc;
    }
    opIdx += lazySeq.length;
  }
  return ret;
}
 
type LazyFn = (value: any, index?: number, items?: any) => LazyResult<any>;
 
type LazyOp = ((input: any) => any) & {
  lazy: ((...args: any[]) => LazyFn) & {
    indexed: boolean;
    single: boolean;
  };
  lazyArgs: any[];
};
 
function _processItem({
  item,
  lazySeq,
  acc,
}: {
  item: any;
  lazySeq: any[];
  acc: any[];
}): boolean {
  if (lazySeq.length === 0) {
    acc.push(item);
    return false;
  }
  let lazyResult: LazyResult<any> = { done: false, hasNext: false };
  let isDone = false;
  for (let i = 0; i < lazySeq.length; i++) {
    const lazyFn = lazySeq[i];
    const indexed = lazyFn.indexed;
    const index = lazyFn.index;
    const items = lazyFn.items;
    items.push(item);
    lazyResult = indexed ? lazyFn(item, index, items) : lazyFn(item);
    lazyFn.index++;
    if (lazyResult.hasNext) {
      if (lazyResult.hasMany) {
        const nextValues: any[] = lazyResult.next;
        for (const subItem of nextValues) {
          const subResult = _processItem({
            item: subItem,
            acc,
            lazySeq: lazySeq.slice(i + 1),
          });
          if (subResult) {
            return true;
          }
        }
        return false;
      } else {
        item = lazyResult.next;
      }
    }
    if (!lazyResult.hasNext) {
      break;
    }
    // process remaining functions in the pipe
    // but don't process remaining elements in the input array
    if (lazyResult.done) {
      isDone = true;
    }
  }
  if (lazyResult.hasNext) {
    acc.push(item);
  }
  if (isDone) {
    return true;
  }
  return false;
}