From 510584e04b35785226ab11fda4209a6fee99566b Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 17 Nov 2015 18:27:42 +0100 Subject: [PATCH] fix: return null for empty number and percent fields Return null if the input of an ui-number-mask or ui-percentage-mask field is empty instead of returning undefined or an empty string. This has the advantage, that no additional check is required for null values inside of calculations (e.g. 5 + null === 5). null is also a valid number, where as an empty string is not. This is the same behavior as angular uses for input[number]. --- src/global/number/number.js | 2 +- src/global/number/number.test.js | 16 +++++++++++++++- src/global/percentage/percentage.js | 2 +- src/global/percentage/percentage.test.js | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/global/number/number.js b/src/global/number/number.js index e0d8fbba..3af222f4 100644 --- a/src/global/number/number.js +++ b/src/global/number/number.js @@ -22,7 +22,7 @@ function NumberMaskDirective($locale, $parse, PreFormatters, NumberMasks) { function parser(value) { if(ctrl.$isEmpty(value)) { - return value; + return null; } var valueToFormat = PreFormatters.clearDelimitersAndLeadingZeros(value) || '0'; diff --git a/src/global/number/number.test.js b/src/global/number/number.test.js index 5803da01..60f8a178 100644 --- a/src/global/number/number.test.js +++ b/src/global/number/number.test.js @@ -49,6 +49,20 @@ describe('ui-number-mask', function() { expect(model.$viewValue).toBe('3456.79'); }); + it('should return null if field is empty', function () { + var input = TestUtil.compile('', { + model: 1000 + }); + + var model = input.controller('ngModel'); + input.val('').triggerHandler('input'); + + expect(model.$viewValue).toBe(''); + expect(model.$modelValue).toBeNull(); + expect(model.$valid).toBe(true); + + }); + it('should validate minimum value', function() { var input = TestUtil.compile('', { model: '3456.79' @@ -101,7 +115,7 @@ describe('ui-number-mask', function() { {modelValue: '', viewValue: ''}, {modelValue: '0', viewValue: '0.00'}, {modelValue: '0.0', viewValue: '0.00'}, - {modelValue: 0, viewValue: '0.00'}, + {modelValue: 0, viewValue: '0.00'} ]; tests.forEach(function(test) { diff --git a/src/global/percentage/percentage.js b/src/global/percentage/percentage.js index 34888c6f..fe0c29a7 100644 --- a/src/global/percentage/percentage.js +++ b/src/global/percentage/percentage.js @@ -46,7 +46,7 @@ function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks) { function parse(value) { if (ctrl.$isEmpty(value)) { - return value; + return null; } var valueToFormat = PreFormatters.clearDelimitersAndLeadingZeros(value) || '0'; diff --git a/src/global/percentage/percentage.test.js b/src/global/percentage/percentage.test.js index 59eea632..31fb98bc 100644 --- a/src/global/percentage/percentage.test.js +++ b/src/global/percentage/percentage.test.js @@ -29,6 +29,20 @@ describe('ui-percentage-mask', function() { expect(model.$viewValue).toBe('1,234.50 %'); }); + it('should return null if field is empty', function () { + var input = TestUtil.compile('', { + model: 12.3 + }); + + var model = input.controller('ngModel'); + input.val('').triggerHandler('input'); + + expect(model.$viewValue).toBe(''); + expect(model.$modelValue).toBeNull(); + expect(model.$valid).toBe(true); + + }); + it('should hide thousands delimiter when ui-hide-group-sep is present', function() { var input = TestUtil.compile('', { model: '12.345'