https://github.com/iamnapo/average
🧮 Small utility function to calculate the average value of an array.
https://github.com/iamnapo/average
array average math mean
Last synced: 12 months ago
JSON representation
🧮 Small utility function to calculate the average value of an array.
- Host: GitHub
- URL: https://github.com/iamnapo/average
- Owner: iamnapo
- License: mit
- Created: 2021-06-29T18:27:06.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-26T08:04:00.000Z (over 3 years ago)
- Last Synced: 2025-02-08T18:35:06.245Z (about 1 year ago)
- Topics: array, average, math, mean
- Language: TypeScript
- Homepage: https://npm.im/@iamnapo/average
- Size: 25.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @iamnapo/average
> Small utility function to calculate the average value of an array
[](https://github.com/iamnapo/average/actions) [](https://www.npmjs.com/package/@iamnapo/average) [](https://bundlephobia.com/result?p=@iamnapo/average)
## Install
```sh
$ npm i @iamnapo/average
```
## Usage
```js
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](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) values from the calculation.
##### get
Type: `Function`\
Default: `(v) => v`
Function used to extract each value.