@iamnapo/average
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

@iamnapo/average

Small utility function to calculate the average value of an array

build npm size

Install

$ npm i @iamnapo/average

Usage

import average from "@iamnapo/average";

average([0, 1, 2, 3, 4, 5]); // => 2.5
average([0, 1, 2, 3, 4, 5], { excludeZeroes: true }); // => 3
average([0, 1, 2, Number.NaN, undefined], { excludeFalsies: true }); // => 1.5
average([{ name: "iamnapo", age: 28 }, { age: 28 }, { name: "Alice", age: 25 }], { get: (v) => v.age }); // => 27
average([]); // => NaN

API

average(input, options?)

Get the average value from an array.

input

Type: unknown[]

Input array.

options

Type: object

You can specify the below options.

excludeZeroes

Type: boolean
Default: false

Exclude zero values from the calculation.

excludeFalsies

Type: boolean
Default: false

Exclude falsy values from the calculation.

get

Type: Function
Default: (v) => v

Function used to extract each value.

Dependents (0)

Package Sidebar

Install

npm i @iamnapo/average

Weekly Downloads

3

Version

1.2.2

License

MIT

Unpacked Size

4.77 kB

Total Files

5

Last publish

Collaborators

  • iamnapo