From b076483caa9b672068ae8d0cbdfeaed68f530861 Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Fri, 20 Mar 2015 22:03:15 -0500 Subject: [PATCH] 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 --- src/rating/rating.js | 7 +++++++ src/rating/test/rating.spec.js | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/rating/rating.js b/src/rating/rating.js index 55ed9e0d44..1ff9ebd6f4 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -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; diff --git a/src/rating/test/rating.spec.js b/src/rating/test/rating.spec.js index c841783944..0a32707ffa 100644 --- a/src/rating/test/rating.spec.js +++ b/src/rating/test/rating.spec.js @@ -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();