Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
feat(rating): add rounding logic to rating value
Browse files Browse the repository at this point in the history
- Moved rounding logic into a formatter
- Checking for number instead of checking if undefined
- Using angular.isNumber for rounding logic
- Using bitwise instead of modulo to check for decimel

Fixes #3413
Closes #3415
Brian Lewis authored and chrisirhc committed Mar 24, 2015
1 parent c0a9c70 commit b076483
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/rating/rating.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,13 @@ angular.module('ui.bootstrap.rating', [])
ngModelCtrl = ngModelCtrl_;
ngModelCtrl.$render = this.render;

ngModelCtrl.$formatters.push(function(value) {
if (angular.isNumber(value) && value << 0 !== value) {
value = Math.round(value);
}
return value;
});

this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn;
this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff;

14 changes: 14 additions & 0 deletions src/rating/test/rating.spec.js
Original file line number Diff line number Diff line change
@@ -73,6 +73,20 @@ describe('rating directive', function () {
expect($rootScope.rate).toBe(3);
});

it('rounds off the number of stars shown with decimal values', function() {
$rootScope.rate = 2.1;
$rootScope.$digest();

expect(getState()).toEqual([true, true, false, false, false]);
expect(element.attr('aria-valuenow')).toBe('2');

$rootScope.rate = 2.5;
$rootScope.$digest();

expect(getState()).toEqual([true, true, true, false, false]);
expect(element.attr('aria-valuenow')).toBe('3');
});

it('changes the number of selected icons when value changes', function() {
$rootScope.rate = 2;
$rootScope.$digest();

0 comments on commit b076483

Please sign in to comment.