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

Commit b076483

Browse files
Brian Lewischrisirhc
Brian Lewis
authored andcommittedMar 24, 2015
feat(rating): add rounding logic to rating value
- 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
1 parent c0a9c70 commit b076483

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

Diff for: ‎src/rating/rating.js

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ angular.module('ui.bootstrap.rating', [])
1313
ngModelCtrl = ngModelCtrl_;
1414
ngModelCtrl.$render = this.render;
1515

16+
ngModelCtrl.$formatters.push(function(value) {
17+
if (angular.isNumber(value) && value << 0 !== value) {
18+
value = Math.round(value);
19+
}
20+
return value;
21+
});
22+
1623
this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn;
1724
this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff;
1825

Diff for: ‎src/rating/test/rating.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ describe('rating directive', function () {
7373
expect($rootScope.rate).toBe(3);
7474
});
7575

76+
it('rounds off the number of stars shown with decimal values', function() {
77+
$rootScope.rate = 2.1;
78+
$rootScope.$digest();
79+
80+
expect(getState()).toEqual([true, true, false, false, false]);
81+
expect(element.attr('aria-valuenow')).toBe('2');
82+
83+
$rootScope.rate = 2.5;
84+
$rootScope.$digest();
85+
86+
expect(getState()).toEqual([true, true, true, false, false]);
87+
expect(element.attr('aria-valuenow')).toBe('3');
88+
});
89+
7690
it('changes the number of selected icons when value changes', function() {
7791
$rootScope.rate = 2;
7892
$rootScope.$digest();

0 commit comments

Comments
 (0)
This repository has been archived.