Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mean filter. #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- [shortFmt](#shortfmt)
- [byteFmt](#bytefmt)
- [kbFmt](#kbfmt)
- [mean](#mean)
- [Boolean](#boolean)
- [isNull](#isnull)
- [isDefined](#isdefined)
Expand Down Expand Up @@ -1190,6 +1191,14 @@ Converts kilobytes into formatted display<br/>
1 MB
1.00126 GB

```
###mean
Calculates the mean value from all values within an array<br/>
**Usage:** ```array | mean```
```html
{{ [12,7,5] | mean }}
<!--result
8
```
#Boolean
>Used for boolean expression in chaining filters
Expand Down
40 changes: 40 additions & 0 deletions src/_filter/math/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @ngdoc filter
* @name mean
* @kind function
*
* @description
* Math.mean will get an array and return the mean value. if an expression
* is provided, will return mean value by expression.
*/

angular.module('a8m.math.mean', ['a8m.math', 'a8m.math.sum'])

.filter('mean', ['$math', 'sumFilter', function ($math, sumFilter) {
return function (input) {

if(!isArray(input) || !isNumericArray(input)){
return input;
}

return sumFilter(input) / input.length;
};

/**
* Check if the given array is numeric.
* @private
* @param array An array given by the input.
* @returns {boolean}
*/
function isNumericArray(array){
var result = true;
forEach(array, function(element){
if(!isNumber(element)){
result = false;
return;
}
});
return result;
}

}]);
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ angular.module('angular.filter', [
'a8m.math.byteFmt',
'a8m.math.kbFmt',
'a8m.math.shortFmt',
'a8m.math.mean',

'a8m.angular',
'a8m.conditions',
Expand Down
28 changes: 28 additions & 0 deletions test/spec/filter/math/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

describe('meanFilter', function () {

var filter;

beforeEach(module('a8m.math.mean'));

beforeEach(inject(function ($filter) {
filter = $filter('mean');
}));

it('should return the mean of all members in array', function() {
expect(filter([1,2,3,4,5,6])).toEqual(3.5);
expect(filter([0,0,0,0,0,3])).toEqual(0.5);
});

it('should return an object if the members type != number', function() {
expect(typeof filter([{}, 'string', 'foo'])).toEqual('object')
});

it('should return the input as-is if is not an array', function() {
expect(filter('string')).toEqual('string');
expect(filter(1)).toEqual(1);
expect(filter(!1)).toBeFalsy();
})

});