All files / seq/operator minBy.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 4/4
100% Lines 8/8

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 181x 1x   1x     10x     1x 6x 62x 52x          
import {map} from './map';
import {pipe} from 'fnxt/pipe';
import {Seq, Tuple, UnaryFunction} from 'fnxt/fnxt-types';
import {reduce} from './reduce';
 
type T<E> = Tuple<E, number>
const first = <E>(v: Tuple<E, unknown>): E => v[0];
 
 
export const minBy = <E>(mapping: UnaryFunction<E, number>): UnaryFunction<Seq<E>, E> =>
  pipe(
    map<E, T<E>>(e => [e, mapping(e)]),
    reduce<T<E>>((p: T<E>, c: T<E>) => p[1] < c[1] ? p : c),
    first
  );