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

feat(rating): Added rounding logic to rating value #3415

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions src/rating/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ angular.module('ui.bootstrap.rating', [])
this.init = function(ngModelCtrl_) {
ngModelCtrl = ngModelCtrl_;
ngModelCtrl.$render = this.render;

ngModelCtrl.$formatters.push(function(value) {
if (angular.isNumber(value) && value % 1 !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 % 1 === -0. Though value should never be negative..

How about doing integer truncation so it'll be value << 0 === value instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be value << 0 !== value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, sorry for that typo.

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;
Expand Down Expand Up @@ -58,6 +65,7 @@ angular.module('ui.bootstrap.rating', [])
this.render = function() {
$scope.value = ngModelCtrl.$viewValue;
};

}])

.directive('rating', function() {
Expand Down
14 changes: 14 additions & 0 deletions src/rating/test/rating.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ describe('rating directive', function () {
});

it('changes the number of selected icons when value changes', 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('rounds off the number of stars shown with decimal values', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant for this to be the title of the new test ^ instead of changing the name of an old test.

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

Expand Down