Skip to content

Commit 2851dd5

Browse files
committed
Added support for percentages
Got the code for rounding to the nearest 10th decimal spot from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global _Objects/Math/round
1 parent 3d4201a commit 2851dd5

7 files changed

+109
-60
lines changed

bower.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "angular-filters",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"main": "./dist/angular-filters.min.js",
55
"description": "Useful filters for AngularJS",
66
"repository": {
77
"type": "git",
8-
"url": "git://github.com/niemyjski/angular-filters.git"
8+
"url": "git://github.com/exceptionless/angular-filters.git"
99
},
1010
"dependencies": {
11-
"angular": ">=1.3.x",
12-
"angular-mocks": ">=1.3.x"
11+
"angular": "^1.5.3",
12+
"angular-mocks": "^1.5.3"
1313
},
1414
"ignore": [
1515
"node_modules"

dist/angular-filters.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Useful filters for AngularJS
3-
* @version v1.2.0 - 2015-01-30 * @link https://github.com/exceptionless/angular-filters
3+
* @version v1.3.0 - 2016-04-04 * @link https://github.com/exceptionless/angular-filters
44
* @author Blake Niemyjski <[email protected]>
55
* @license MIT License, http://www.opensource.org/licenses/MIT
66
*/(function () {
@@ -69,6 +69,30 @@
6969
}]);
7070
}());
7171

72+
(function () {
73+
'use strict';
74+
75+
angular.module('angular-filters')
76+
.filter('percentage', ['$filter', function ($filter) {
77+
return function(input) {
78+
if (isNaN(input) || input === null || input === '' || input === false || input === true) {
79+
return '0%';
80+
}
81+
82+
if (input > 0.0 && input < 1) {
83+
// Shift
84+
input = input.toString().split('e');
85+
input = Math.ceil(+(input[0] + 'e' + (input[1] ? (+input[1] + 1) : 1)));
86+
// Shift back
87+
input = input.toString().split('e');
88+
return +(input[0] + 'e' + (input[1] ? (+input[1] - 1) : -1)) + '%';
89+
}
90+
91+
return $filter('number')(input, (input % 1 === 0) ? 0 : 1) + '%';
92+
};
93+
}]);
94+
}());
95+
7296
(function () {
7397
'use strict';
7498

dist/angular-filters.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

karma.conf.js

+3-34
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,28 @@
1-
// Karma configuration
2-
// Generated on Fri Aug 09 2013 14:14:35 GMT-0500 (CDT)
3-
41
module.exports = function(config) {
52
config.set({
6-
// base path, that will be used to resolve files and exclude
73
basePath: '',
84
frameworks: ["jasmine"],
95

10-
// list of files / patterns to load in the browser
116
files: [
12-
'bower_components/angular/angular.js',
13-
'bower_components/angular-mocks/angular-mocks.js',
7+
'node_modules/angular/angular.js',
8+
'node_modules/angular-mocks/angular-mocks.js',
149
'src/angular-filters.js',
1510
'src/**/*.js'
1611
],
1712

18-
// list of files to exclude
19-
exclude: [
20-
],
21-
22-
// test results reporter to use
23-
// possible values: 'dots', 'progress', 'junit'
13+
exclude: [],
2414
reporters: ['mocha'],
25-
26-
// web server port
2715
port: 9876,
28-
29-
// cli runner port
3016
runnerPort: 9100,
31-
32-
// enable / disable colors in the output (reporters and logs)
3317
colors: true,
3418

35-
// level of logging
3619
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
3720
logLevel: config.LOG_WARN,
38-
39-
// enable / disable watching file and executing tests whenever any file changes
4021
autoWatch: true,
4122

42-
// Start these browsers, currently available:
43-
// - Chrome
44-
// - ChromeCanary
45-
// - Firefox
46-
// - Opera
47-
// - Safari (only Mac)
48-
// - PhantomJS
49-
// - IE (only Windows)
5023
browsers: ['PhantomJS'],
5124

52-
// If browser does not capture in given timeout [ms], kill it
5325
captureTimeout: 60000,
54-
55-
// Continuous Integration mode
56-
// if true, it capture browsers, run tests and exit
5726
singleRun: true
5827
});
5928
};

package.json

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-filters",
33
"description": "Useful filters for AngularJS",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"filename": "angular-filters.min.js",
66
"main": "./dist/angular-filters.min.js",
77
"homepage": "https://github.com/exceptionless/angular-filters",
@@ -10,9 +10,6 @@
1010
"type": "git",
1111
"url": "git://github.com/exceptionless/angular-filters.git"
1212
},
13-
"engines": {
14-
"node": ">= 0.9"
15-
},
1613
"keywords": [
1714
"angular",
1815
"client",
@@ -29,22 +26,26 @@
2926
"website": "http://niemyjski.com"
3027
}
3128
],
29+
"dependencies": {
30+
"angular": "^1.5.3",
31+
"angular-mocks": "^1.5.3"
32+
},
3233
"devDependencies": {
33-
"grunt-cli": ">= 0.1.7",
34-
"grunt-contrib-concat": "*",
35-
"grunt-contrib-jshint": "*",
36-
"grunt-contrib-uglify": "*",
37-
"grunt-bower": "*",
38-
"grunt-bower-task": "*",
39-
40-
"grunt-karma": "latest",
41-
"grunt-conventional-changelog": "0.0.12",
42-
"karma": "~0.12.1",
43-
"karma-mocha-reporter": "0.2.8",
44-
"karma-jasmine": "~0.1.5",
45-
"karma-chrome-launcher": "~0.1.2",
46-
"karma-phantomjs-launcher": "~0.1.2",
47-
"karma-firefox-launcher": "~0.1.3"
34+
"grunt": "0.4.5",
35+
"grunt-bower": "0.21",
36+
"grunt-bower-task": "0.4",
37+
"grunt-cli": "1.2.0",
38+
"grunt-contrib-concat": "1.0",
39+
"grunt-contrib-jshint": "1.0",
40+
"grunt-contrib-uglify": "1.0",
41+
"grunt-conventional-changelog": "6.1.0",
42+
"grunt-karma": "0.12",
43+
"jasmine-core": "2.4.1",
44+
"karma": "0.13.22",
45+
"karma-jasmine": "0.3.8",
46+
"karma-mocha-reporter": "2.0.0",
47+
"karma-phantomjs-launcher": "1.0.0",
48+
"phantomjs-prebuilt": "2.1.7"
4849
},
4950
"scripts": {
5051
"test": "grunt test --verbose"
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
describe('Filter: percentage', function() {
2+
beforeEach(module('angular-filters'));
3+
4+
// initialize a new instance of the filter before each test
5+
var percentage;
6+
beforeEach(inject(function ($filter) {
7+
percentage = $filter('percentage');
8+
}));
9+
10+
it('should return 0% when invalid value is passed in', function () {
11+
expect(percentage()).toBe('0%');
12+
expect(percentage(null)).toBe('0%');
13+
expect(percentage(true)).toBe('0%');
14+
expect(percentage(false)).toBe('0%');
15+
expect(percentage('')).toBe('0%');
16+
});
17+
18+
it('should return number % when number is passed in', function () {
19+
expect(percentage(123)).toBe('123%');
20+
});
21+
22+
it('should return rounded %', function () {
23+
expect(percentage(0)).toBe('0%');
24+
expect(percentage(0.00001)).toBe('0.1%');
25+
expect(percentage(0.0001)).toBe('0.1%');
26+
expect(percentage(0.001)).toBe('0.1%');
27+
expect(percentage(0.01)).toBe('0.1%');
28+
expect(percentage(0.1)).toBe('0.1%');
29+
expect(percentage(0.6)).toBe('0.6%');
30+
expect(percentage(0.61)).toBe('0.7%');
31+
});
32+
});

src/percentage/percentage-filter.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(function () {
2+
'use strict';
3+
4+
angular.module('angular-filters')
5+
.filter('percentage', ['$filter', function ($filter) {
6+
return function(input) {
7+
if (isNaN(input) || input === null || input === '' || input === false || input === true) {
8+
return '0%';
9+
}
10+
11+
if (input > 0.0 && input < 1) {
12+
// Shift
13+
input = input.toString().split('e');
14+
input = Math.ceil(+(input[0] + 'e' + (input[1] ? (+input[1] + 1) : 1)));
15+
// Shift back
16+
input = input.toString().split('e');
17+
return +(input[0] + 'e' + (input[1] ? (+input[1] - 1) : -1)) + '%';
18+
}
19+
20+
return $filter('number')(input, (input % 1 === 0) ? 0 : 1) + '%';
21+
};
22+
}]);
23+
}());

0 commit comments

Comments
 (0)