Skip to content
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 @@ -71,6 +71,7 @@
- [shortFmt](#shortfmt)
- [byteFmt](#bytefmt)
- [kbFmt](#kbfmt)
- [mode] (#mode)
- [Boolean](#boolean)
- [isNull](#isnull)
- [isDefined](#isdefined)
Expand Down Expand Up @@ -1222,6 +1223,14 @@ Converts kilobytes into formatted display<br/>
1 MB
1.00126 GB

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

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

var modeMap = {};
var mode = input[0];
var maxCount = 1;
for (var i = 0; i < input.length; i++) {
var val = input[i];
if(isDefined(modeMap[val])){
modeMap[val]++;
} else {
modeMap[val] = 1;
}

if(modeMap[val] > maxCount){
mode = val;
maxCount = modeMap[val];
}
}

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

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

describe('modeFilter', function () {

var filter;

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

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

it('should get an array of numbers and return the mode', function() {
expect(filter([1,2,3,4,5])).toEqual(1);
expect(filter([2,0,2,2,2])).toEqual(2);
expect(filter([1,2.32,2.32,7,5])).toEqual(2.32);
expect(filter([4])).toEqual(4);
});


it('should get an !array and return it as-is', function() {
expect(filter('string')).toEqual('string');
expect(filter({})).toEqual({});
expect(filter(!0)).toBeTruthy();
});

});