1.0.1 • Published 8 years ago

get-average-if v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
8 years ago

get-average-if

Build Status

Installation

npm install get-average-if --save

Syntax

/**
 * @param {object} range - A range (e.g. Array or Immutable.List) with the methods filter and reduce.
 * @param {function} criteria -  A predicate function.
 * @param {function} getter - A getter function.
 * @returns {number}
 */
getAverageIf(range, criteria, getter)

Usage

import getAverageIf from 'get-average-if';

// Get the average of all even numbers in a number array.
getAverageIf(
    [1,2,3,4],
    (item) => item % 2 === 0
); // 3

// Get the average of all even numbers in an Immutable.List.
getAverageIf(
    Immutable.List([1,2,3,4]),
    (item) => item % 2 === 0
); // 3

// Get the average age of an object array with users.
getAverageIf(
    [{
        name: 'John',
        sex: 'male',
        age: '20'
    },{
        name: 'Mary',
        sex: 'female',
        age: '30'
    },{
        name: 'Kevin',
        sex: 'male',
        age: '30'
    }],
    (user) => user.sex === 'male',
    (user) => user.age
); // 25

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.0.0 Initial release