All files / src/table/util use-middleware.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 5/5
100% Lines 6/6

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                    53x 21x     32x 32x 96x     46x        
import { TableAddon, TableMiddleware } from "../TableProps";
 
export function useMiddleware<K extends keyof TableAddon>(
  addons: TableAddon[],
  name: K
) {
  // infer value type
  type Value<T> = T extends TableMiddleware<infer V> ? V : never;
  type ValueType = Value<TableAddon[typeof name]>;
 
  if (!addons || !addons.length) {
    return (initialValue: ValueType) => initialValue;
  }
 
  return (initialValue: ValueType) =>
    addons
      .map(x => x[name])
      .filter(Boolean)
      .reduce<ValueType>(
        (previous, middleware: TableMiddleware<any>) => middleware(previous),
        initialValue
      );
}