Codibre/fluent-iterable

View on GitHub
src/recipes/avg-recipe.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { AnyMapper } from '../types-internal';
import { prepare } from '../types-internal/prepare';
import { identity, getAverageStepper } from '../utils';
import { BasicIngredients } from './ingredients';

export function avgRecipe({ forEach, resolver }: BasicIngredients) {
  return function <T>(this: Iterable<T>, baseMapper: AnyMapper<T> = identity) {
    const stepper = getAverageStepper();
    const mapper = prepare(baseMapper);
    return resolver(
      forEach.call(this, (x: T) => resolver(mapper(x), stepper.step)),
      () => stepper.avg,
    );
  };
}