From dd779c375fb14fecf70d2d230024cad5187a54f6 Mon Sep 17 00:00:00 2001 From: Igor Rafael Date: Sun, 24 Sep 2017 12:56:26 -0300 Subject: [PATCH] feat(uiScientificNotationMask): allow negative exponents --- .../scientific-notation/scientific-notation.js | 12 ++++++++++-- .../scientific-notation.spec.js | 16 ++++++++++++++++ .../scientific-notation.test.js | 12 ++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/global/scientific-notation/scientific-notation.js b/src/global/scientific-notation/scientific-notation.js index 9953b0da..3817d5b6 100644 --- a/src/global/scientific-notation/scientific-notation.js +++ b/src/global/scientific-notation/scientific-notation.js @@ -95,8 +95,16 @@ function ScientificNotationMaskDirective($locale, $parse) { return value; } - var viewValue = formatter(value), - modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.')); + var isExponentNegative = /e-/.test(value); + var cleanValue = value.replace(/-/g,''); + var viewValue = formatter(cleanValue); + + var needsToInvertSign = (value.slice(-1) === '-'); + if (needsToInvertSign ^ isExponentNegative) { + viewValue = viewValue.replace(/(e[-]?)/, 'e-'); + } + + var modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.')); if (ctrl.$viewValue !== viewValue) { ctrl.$setViewValue(viewValue); diff --git a/src/global/scientific-notation/scientific-notation.spec.js b/src/global/scientific-notation/scientific-notation.spec.js index cebff16f..495ccd46 100644 --- a/src/global/scientific-notation/scientific-notation.spec.js +++ b/src/global/scientific-notation/scientific-notation.spec.js @@ -165,4 +165,20 @@ describe('uiScientificNotationMask', function() { expect(input.getAttribute('value')).toEqual('0'); expect(value.getText()).toEqual('0'); }); + + it('should format numbers with negative exponent', function() { + var input = element(by.model('scientificNotationMask')), + value = element(by.exactBinding('scientificNotationMask')); + + input.clear(); + input.sendKeys('1234'); + expect(input.getAttribute('value')).toEqual('1,23e4'); + expect(value.getText()).toEqual('12300'); + input.sendKeys('-'); + expect(input.getAttribute('value')).toEqual('1,23e-4'); + expect(value.getText()).toEqual('0.000123'); + input.sendKeys('-'); + expect(input.getAttribute('value')).toEqual('1,23e4'); + expect(value.getText()).toEqual('12300'); + }); }); diff --git a/src/global/scientific-notation/scientific-notation.test.js b/src/global/scientific-notation/scientific-notation.test.js index 021352f0..73ca73b4 100644 --- a/src/global/scientific-notation/scientific-notation.test.js +++ b/src/global/scientific-notation/scientific-notation.test.js @@ -49,6 +49,15 @@ describe('ui-scientific-notation-mask', function() { expect(model.$viewValue).toBe('1.235e4'); }); + it('should format initial model values with negative exponent', function() { + var input = TestUtil.compile('', { + model: 1.3456e-3 + }); + + var model = input.controller('ngModel'); + expect(model.$viewValue).toBe('1.35e-3'); + }); + it('should format input', function() { var input = TestUtil.compile(''); var model = input.controller('ngModel'); @@ -59,6 +68,9 @@ describe('ui-scientific-notation-mask', function() { input.val('1.2345').triggerHandler('input'); expect(model.$viewValue).toBe('1.23e45'); expect(model.$modelValue).toBe(1.23e45); + input.val('1.2345e-9').triggerHandler('input'); + expect(model.$viewValue).toBe('1.23e-9'); + expect(model.$modelValue).toBe(1.23e-9); }); it('should handle corner cases', angular.mock.inject(function($rootScope) {