Skip to content

Commit dd779c3

Browse files
committed
feat(uiScientificNotationMask): allow negative exponents
1 parent ba6ba7e commit dd779c3

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/global/scientific-notation/scientific-notation.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ function ScientificNotationMaskDirective($locale, $parse) {
9595
return value;
9696
}
9797

98-
var viewValue = formatter(value),
99-
modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.'));
98+
var isExponentNegative = /e-/.test(value);
99+
var cleanValue = value.replace(/-/g,'');
100+
var viewValue = formatter(cleanValue);
101+
102+
var needsToInvertSign = (value.slice(-1) === '-');
103+
if (needsToInvertSign ^ isExponentNegative) {
104+
viewValue = viewValue.replace(/(e[-]?)/, 'e-');
105+
}
106+
107+
var modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.'));
100108

101109
if (ctrl.$viewValue !== viewValue) {
102110
ctrl.$setViewValue(viewValue);

src/global/scientific-notation/scientific-notation.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,20 @@ describe('uiScientificNotationMask', function() {
165165
expect(input.getAttribute('value')).toEqual('0');
166166
expect(value.getText()).toEqual('0');
167167
});
168+
169+
it('should format numbers with negative exponent', function() {
170+
var input = element(by.model('scientificNotationMask')),
171+
value = element(by.exactBinding('scientificNotationMask'));
172+
173+
input.clear();
174+
input.sendKeys('1234');
175+
expect(input.getAttribute('value')).toEqual('1,23e4');
176+
expect(value.getText()).toEqual('12300');
177+
input.sendKeys('-');
178+
expect(input.getAttribute('value')).toEqual('1,23e-4');
179+
expect(value.getText()).toEqual('0.000123');
180+
input.sendKeys('-');
181+
expect(input.getAttribute('value')).toEqual('1,23e4');
182+
expect(value.getText()).toEqual('12300');
183+
});
168184
});

src/global/scientific-notation/scientific-notation.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ describe('ui-scientific-notation-mask', function() {
4949
expect(model.$viewValue).toBe('1.235e4');
5050
});
5151

52+
it('should format initial model values with negative exponent', function() {
53+
var input = TestUtil.compile('<input ng-model="model" ui-scientific-notation-mask>', {
54+
model: 1.3456e-3
55+
});
56+
57+
var model = input.controller('ngModel');
58+
expect(model.$viewValue).toBe('1.35e-3');
59+
});
60+
5261
it('should format input', function() {
5362
var input = TestUtil.compile('<input ng-model="model" ui-scientific-notation-mask>');
5463
var model = input.controller('ngModel');
@@ -59,6 +68,9 @@ describe('ui-scientific-notation-mask', function() {
5968
input.val('1.2345').triggerHandler('input');
6069
expect(model.$viewValue).toBe('1.23e45');
6170
expect(model.$modelValue).toBe(1.23e45);
71+
input.val('1.2345e-9').triggerHandler('input');
72+
expect(model.$viewValue).toBe('1.23e-9');
73+
expect(model.$modelValue).toBe(1.23e-9);
6274
});
6375

6476
it('should handle corner cases', angular.mock.inject(function($rootScope) {

0 commit comments

Comments
 (0)