Skip to content

Commit

Permalink
Added the ability to set a max percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Sep 26, 2016
1 parent e815540 commit 5f9c3d5
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 63 deletions.
17 changes: 3 additions & 14 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = function(grunt) {
' * @link <%= pkg.homepage %>',
' * @author <%= pkg.author %>',
' * @license MIT License, http://www.opensource.org/licenses/MIT',
' */'
' */',
''
].join('\n')
},
dirs: {
Expand All @@ -31,15 +32,6 @@ module.exports = function(grunt) {
install: {
}
},
uglify: {
options: {
banner: '<%= meta.banner %>'
},
dist: {
src: ['<%= concat.dist.dest %>'],
dest: '<%= dirs.dest %>/<%= pkg.name %>.min.js'
}
},
jshint: {
files: ['Gruntfile.js', 'src/angular-filters.js', 'src/**/*.js'],
options: {
Expand Down Expand Up @@ -92,9 +84,6 @@ module.exports = function(grunt) {
// Load the plugin that provides the "concat" task.
grunt.loadNpmTasks('grunt-contrib-concat');

// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');

grunt.loadNpmTasks('grunt-bower-task');

grunt.renameTask('bower', 'bowerInstall');
Expand All @@ -106,7 +95,7 @@ module.exports = function(grunt) {
grunt.registerTask('default', ['build']);

// Build task.
grunt.registerTask('build', ['bowerInstall', /* 'test', */ 'concat', 'uglify']);
grunt.registerTask('build', ['bowerInstall', /* 'test', */ 'concat']);

grunt.registerTask('test', ['karma:build', 'karma:buildUnderscore']);

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ Returns a number formatted as a percentage. Numbers between 0 and 1 will be roun
{{60.0 | percentage}}
{{0 | percentage}}
{{0.000001 | percentage}}
{{100.000001 | percentage:100}}

Result:
123%
0%
60%
0%
0.1%
100%
```

###replace
Expand Down
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "ng-filters",
"version": "1.3.0",
"main": "./dist/angular-filters.min.js",
"version": "1.3.1",
"main": "./dist/ng-filters.js",
"description": "Useful filters for AngularJS",
"repository": {
"type": "git",
"url": "git://github.com/exceptionless/angular-filters.git"
"url": "git://github.com/exceptionless/ng-filters.git"
},
"dependencies": {
"angular": "^1.5.3",
"angular-mocks": "^1.5.3"
"angular": "^1.5.8",
"angular-mocks": "^1.5.8"
},
"ignore": [
"node_modules"
Expand Down
6 changes: 0 additions & 6 deletions dist/angular-filters.min.js

This file was deleted.

35 changes: 23 additions & 12 deletions dist/angular-filters.js → dist/ng-filters.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* Useful filters for AngularJS
* @version v1.3.0 - 2016-04-04 * @link https://github.com/exceptionless/angular-filters
* @version v1.3.1 - 2016-09-26 * @link https://github.com/exceptionless/ng-filters
* @author Blake Niemyjski <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/(function () {
*/
(function () {
'use strict';

angular.module('angular-filters', []);
Expand Down Expand Up @@ -74,21 +75,31 @@

angular.module('angular-filters')
.filter('percentage', ['$filter', function ($filter) {
return function(input) {
if (isNaN(input) || input === null || input === '' || input === false || input === true) {
function roundUpToNextTenth(input) {
input = parseFloat(input.toFixed(5));

// Shift
input = input.toString().split('e');
input = Math.ceil(+(input[0] + 'e' + (input[1] ? (+input[1] + 1) : 1)));
// Shift back
input = input.toString().split('e');

return +(input[0] + 'e' + (input[1] ? (+input[1] - 1) : -1));
}

return function(input, max) {
input = parseFloat(input);
if (!isFinite(input)) {
return '0%';
}

if (input > 0.0 && input < 1) {
// Shift
input = input.toString().split('e');
input = Math.ceil(+(input[0] + 'e' + (input[1] ? (+input[1] + 1) : 1)));
// Shift back
input = input.toString().split('e');
return +(input[0] + 'e' + (input[1] ? (+input[1] - 1) : -1)) + '%';
input = Math.floor(input) + roundUpToNextTenth(input - Math.floor(input));
max = parseFloat(max);
if (isFinite(max) && input > max) {
return max + '%';
}

return $filter('number')(input, (input % 1 === 0) ? 0 : 1) + '%';
return parseFloat($filter('number')(input, (input % 1 === 0) ? 0 : 1)) + '%';
};
}]);
}());
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "ng-filters",
"description": "Useful filters for AngularJS",
"version": "1.3.0",
"filename": "angular-filters.min.js",
"main": "./dist/angular-filters.min.js",
"homepage": "https://github.com/exceptionless/angular-filters",
"version": "1.3.1",
"filename": "ng-filters.js",
"main": "./dist/ng-filters.js",
"homepage": "https://github.com/exceptionless/ng-filters",
"author": "Blake Niemyjski <[email protected]>",
"repository": {
"type": "git",
"url": "git://github.com/exceptionless/angular-filters.git"
"url": "git://github.com/exceptionless/ng-filters.git"
},
"keywords": [
"angular",
Expand All @@ -27,25 +27,25 @@
}
],
"dependencies": {
"angular": "^1.5.3",
"angular-mocks": "^1.5.3"
"angular": "^1.5.8",
"angular-mocks": "^1.5.8"
},
"devDependencies": {
"grunt": "0.4.5",
"grunt": "1.0.1",
"grunt-bower": "0.21",
"grunt-bower-task": "0.4",
"grunt-cli": "1.2.0",
"grunt-contrib-concat": "1.0",
"grunt-contrib-jshint": "1.0",
"grunt-contrib-uglify": "1.0",
"grunt-conventional-changelog": "6.1.0",
"grunt-karma": "0.12",
"jasmine-core": "2.4.1",
"karma": "0.13.22",
"karma-jasmine": "0.3.8",
"karma-mocha-reporter": "2.0.0",
"karma-phantomjs-launcher": "1.0.0",
"phantomjs-prebuilt": "2.1.7"
"grunt-karma": "2.0",
"jasmine-core": "2.5.2",
"karma": "1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "1.0.2",
"karma-mocha-reporter": "2.2.0",
"karma-phantomjs-launcher": "1.0.2",
"phantomjs-prebuilt": "2.1.12"
},
"scripts": {
"test": "grunt test --verbose"
Expand Down
21 changes: 21 additions & 0 deletions src/percentage/percentage-filter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,33 @@ describe('Filter: percentage', function() {

it('should return rounded %', function () {
expect(percentage(0)).toBe('0%');
expect(percentage(0.00000)).toBe('0%');
expect(percentage(1)).toBe('1%');
expect(percentage(2)).toBe('2%');
expect(percentage(2.11)).toBe('2.2%');
expect(percentage(0.99)).toBe('1%');
expect(percentage(0.000001)).toBe('0%');
expect(percentage(0.00001)).toBe('0.1%');
expect(percentage(0.0001)).toBe('0.1%');
expect(percentage(0.001)).toBe('0.1%');
expect(percentage(0.01)).toBe('0.1%');
expect(percentage(0.1)).toBe('0.1%');
expect(percentage(0.6)).toBe('0.6%');
expect(percentage(0.61)).toBe('0.7%');
expect(percentage(100.1)).toBe('100.1%');
expect(percentage(100.01)).toBe('100.1%');
});

it('should return rounded % with max', function () {
expect(percentage(0.1, 0.01)).toBe('0.01%');
expect(percentage(0.6, 0.5)).toBe('0.5%');
expect(percentage(0.6, 0.7)).toBe('0.6%');
expect(percentage(99.9, 100)).toBe('99.9%');
expect(percentage(99.99, 99.99)).toBe('99.99%');
expect(percentage(99.99, 100)).toBe('100%');
expect(percentage(100, 100)).toBe('100%');
expect(percentage(100.00001, 100)).toBe('100%');
expect(percentage(100.00001, 100.0)).toBe('100%');
expect(percentage(101, 100)).toBe('100%');
});
});
30 changes: 20 additions & 10 deletions src/percentage/percentage-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@

angular.module('angular-filters')
.filter('percentage', ['$filter', function ($filter) {
return function(input) {
if (isNaN(input) || input === null || input === '' || input === false || input === true) {
function roundUpToNextTenth(input) {
input = parseFloat(input.toFixed(5));

// Shift
input = input.toString().split('e');
input = Math.ceil(+(input[0] + 'e' + (input[1] ? (+input[1] + 1) : 1)));
// Shift back
input = input.toString().split('e');

return +(input[0] + 'e' + (input[1] ? (+input[1] - 1) : -1));
}

return function(input, max) {
input = parseFloat(input);
if (!isFinite(input)) {
return '0%';
}

if (input > 0.0 && input < 1) {
// Shift
input = input.toString().split('e');
input = Math.ceil(+(input[0] + 'e' + (input[1] ? (+input[1] + 1) : 1)));
// Shift back
input = input.toString().split('e');
return +(input[0] + 'e' + (input[1] ? (+input[1] - 1) : -1)) + '%';
input = Math.floor(input) + roundUpToNextTenth(input - Math.floor(input));
max = parseFloat(max);
if (isFinite(max) && input > max) {
return max + '%';
}

return $filter('number')(input, (input % 1 === 0) ? 0 : 1) + '%';
return parseFloat($filter('number')(input, (input % 1 === 0) ? 0 : 1)) + '%';
};
}]);
}());

0 comments on commit 5f9c3d5

Please sign in to comment.