Skip to content

Commit

Permalink
feat(uiScientificNotationMask): allow negative exponents
Browse files Browse the repository at this point in the history
  • Loading branch information
assisrafael committed Sep 24, 2017
1 parent ba6ba7e commit dd779c3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/global/scientific-notation/scientific-notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/global/scientific-notation/scientific-notation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
12 changes: 12 additions & 0 deletions src/global/scientific-notation/scientific-notation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input ng-model="model" ui-scientific-notation-mask>', {
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('<input ng-model="model" ui-scientific-notation-mask>');
var model = input.controller('ngModel');
Expand All @@ -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) {
Expand Down

0 comments on commit dd779c3

Please sign in to comment.