Skip to content

Commit

Permalink
feat(uiScientificNotationMask): allow negative numbers with ui-negati…
Browse files Browse the repository at this point in the history
…ve-number attribute
  • Loading branch information
assisrafael committed Sep 24, 2017
1 parent dd779c3 commit 1b94318
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/global/scientific-notation/scientific-notation.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ <h2>ui-scientific-notation-mask</h2>
Errors: <span ng-bind="form.scientificNotationMaskWithoutDigits.$error | json"></span>
<br>
<br>
<input type="text" name="field2" ng-model="scientificNotationMaskWithNegativeNumber" ui-scientific-notation-mask ui-negative-number> <br>
<span ng-bind="scientificNotationMaskWithNegativeNumber"></span> - (accepts negative numbers. Press '-' to test)
<br>
</form>
</body>
</html>
11 changes: 9 additions & 2 deletions src/global/scientific-notation/scientific-notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ function ScientificNotationMaskDirective($locale, $parse) {
formattedValue += 'e' + exponent;
}

return formattedValue;
var prefix = (angular.isDefined(attrs.uiNegativeNumber) && value[0] === '-') ? '-' : '';

return prefix + formattedValue;
}

function parser(value) {
Expand All @@ -96,14 +98,19 @@ function ScientificNotationMaskDirective($locale, $parse) {
}

var isExponentNegative = /e-/.test(value);
var cleanValue = value.replace(/-/g,'');
var cleanValue = value.replace('e-', 'e');
var viewValue = formatter(cleanValue);

var needsToInvertSign = (value.slice(-1) === '-');

if (needsToInvertSign ^ isExponentNegative) {
viewValue = viewValue.replace(/(e[-]?)/, 'e-');
}

if (needsToInvertSign && isExponentNegative) {
viewValue = viewValue[0] !== '-' ? ('-' + viewValue) : viewValue.replace(/^(-)/,'');
}

var modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.'));

if (ctrl.$viewValue !== viewValue) {
Expand Down
20 changes: 20 additions & 0 deletions src/global/scientific-notation/scientific-notation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ describe('uiScientificNotationMask', function() {
input.sendKeys('-');
expect(input.getAttribute('value')).toEqual('1,23e-4');
expect(value.getText()).toEqual('0.000123');
});

it('should accept negative numbers when the ui-negative attribute is present', function() {
var input = element(by.model('scientificNotationMaskWithNegativeNumber')),
value = element(by.exactBinding('scientificNotationMaskWithNegativeNumber'));

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');
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');
Expand Down
32 changes: 31 additions & 1 deletion src/global/scientific-notation/scientific-notation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('../global-masks');

describe('ui-scientific-notation-mask', function() {
describe('uiScientificNotationMask', function() {
beforeEach(angular.mock.module('ui.utils.masks.global'));

it('should throw an error if used without ng-model', function() {
Expand Down Expand Up @@ -73,6 +73,36 @@ describe('ui-scientific-notation-mask', function() {
expect(model.$modelValue).toBe(1.23e-9);
});

it('should accept negative numbers if "ui-negative-number" is defined', function() {
var input = TestUtil.compile('<input ng-model="model" ui-scientific-notation-mask ui-negative-number>', {
model: -1.3456e3
});
var model = input.controller('ngModel');

expect(model.$viewValue).toBe('-1.35e3');
expect(model.$modelValue).toBe(-1345.6);

input.val('-1.23e45').triggerHandler('input');
expect(model.$viewValue).toBe('-1.23e45');
expect(model.$modelValue).toBe(-1.23e45);

input.val('1.23e45-').triggerHandler('input');
expect(model.$viewValue).toBe('1.23e-45');
expect(model.$modelValue).toBe(1.23e-45);

input.val('1.23e-45-').triggerHandler('input');
expect(model.$viewValue).toBe('-1.23e45');
expect(model.$modelValue).toBe(-1.23e45);

input.val('-1.23e45-').triggerHandler('input');
expect(model.$viewValue).toBe('-1.23e-45');
expect(model.$modelValue).toBe(-1.23e-45);

input.val('-1.23e-45-').triggerHandler('input');
expect(model.$viewValue).toBe('1.23e45');
expect(model.$modelValue).toBe(1.23e45);
});

it('should handle corner cases', angular.mock.inject(function($rootScope) {
var input = TestUtil.compile('<input ng-model="model" ui-scientific-notation-mask>');
var model = input.controller('ngModel');
Expand Down

0 comments on commit 1b94318

Please sign in to comment.