Skip to content

Commit 1b94318

Browse files
committed
feat(uiScientificNotationMask): allow negative numbers with ui-negative-number attribute
1 parent dd779c3 commit 1b94318

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/global/scientific-notation/scientific-notation.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ <h2>ui-scientific-notation-mask</h2>
4545
Errors: <span ng-bind="form.scientificNotationMaskWithoutDigits.$error | json"></span>
4646
<br>
4747
<br>
48+
<input type="text" name="field2" ng-model="scientificNotationMaskWithNegativeNumber" ui-scientific-notation-mask ui-negative-number> <br>
49+
<span ng-bind="scientificNotationMaskWithNegativeNumber"></span> - (accepts negative numbers. Press '-' to test)
50+
<br>
4851
</form>
4952
</body>
5053
</html>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ function ScientificNotationMaskDirective($locale, $parse) {
8787
formattedValue += 'e' + exponent;
8888
}
8989

90-
return formattedValue;
90+
var prefix = (angular.isDefined(attrs.uiNegativeNumber) && value[0] === '-') ? '-' : '';
91+
92+
return prefix + formattedValue;
9193
}
9294

9395
function parser(value) {
@@ -96,14 +98,19 @@ function ScientificNotationMaskDirective($locale, $parse) {
9698
}
9799

98100
var isExponentNegative = /e-/.test(value);
99-
var cleanValue = value.replace(/-/g,'');
101+
var cleanValue = value.replace('e-', 'e');
100102
var viewValue = formatter(cleanValue);
101103

102104
var needsToInvertSign = (value.slice(-1) === '-');
105+
103106
if (needsToInvertSign ^ isExponentNegative) {
104107
viewValue = viewValue.replace(/(e[-]?)/, 'e-');
105108
}
106109

110+
if (needsToInvertSign && isExponentNegative) {
111+
viewValue = viewValue[0] !== '-' ? ('-' + viewValue) : viewValue.replace(/^(-)/,'');
112+
}
113+
107114
var modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.'));
108115

109116
if (ctrl.$viewValue !== viewValue) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ describe('uiScientificNotationMask', function() {
177177
input.sendKeys('-');
178178
expect(input.getAttribute('value')).toEqual('1,23e-4');
179179
expect(value.getText()).toEqual('0.000123');
180+
});
181+
182+
it('should accept negative numbers when the ui-negative attribute is present', function() {
183+
var input = element(by.model('scientificNotationMaskWithNegativeNumber')),
184+
value = element(by.exactBinding('scientificNotationMaskWithNegativeNumber'));
185+
186+
input.clear();
187+
188+
input.sendKeys('1234');
189+
expect(input.getAttribute('value')).toEqual('1,23e4');
190+
expect(value.getText()).toEqual('12300');
191+
input.sendKeys('-');
192+
expect(input.getAttribute('value')).toEqual('1,23e-4');
193+
expect(value.getText()).toEqual('0.000123');
194+
input.sendKeys('-');
195+
expect(input.getAttribute('value')).toEqual('-1,23e4');
196+
expect(value.getText()).toEqual('-12300');
197+
input.sendKeys('-');
198+
expect(input.getAttribute('value')).toEqual('-1,23e-4');
199+
expect(value.getText()).toEqual('-0.000123');
180200
input.sendKeys('-');
181201
expect(input.getAttribute('value')).toEqual('1,23e4');
182202
expect(value.getText()).toEqual('12300');

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

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

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

76+
it('should accept negative numbers if "ui-negative-number" is defined', function() {
77+
var input = TestUtil.compile('<input ng-model="model" ui-scientific-notation-mask ui-negative-number>', {
78+
model: -1.3456e3
79+
});
80+
var model = input.controller('ngModel');
81+
82+
expect(model.$viewValue).toBe('-1.35e3');
83+
expect(model.$modelValue).toBe(-1345.6);
84+
85+
input.val('-1.23e45').triggerHandler('input');
86+
expect(model.$viewValue).toBe('-1.23e45');
87+
expect(model.$modelValue).toBe(-1.23e45);
88+
89+
input.val('1.23e45-').triggerHandler('input');
90+
expect(model.$viewValue).toBe('1.23e-45');
91+
expect(model.$modelValue).toBe(1.23e-45);
92+
93+
input.val('1.23e-45-').triggerHandler('input');
94+
expect(model.$viewValue).toBe('-1.23e45');
95+
expect(model.$modelValue).toBe(-1.23e45);
96+
97+
input.val('-1.23e45-').triggerHandler('input');
98+
expect(model.$viewValue).toBe('-1.23e-45');
99+
expect(model.$modelValue).toBe(-1.23e-45);
100+
101+
input.val('-1.23e-45-').triggerHandler('input');
102+
expect(model.$viewValue).toBe('1.23e45');
103+
expect(model.$modelValue).toBe(1.23e45);
104+
});
105+
76106
it('should handle corner cases', angular.mock.inject(function($rootScope) {
77107
var input = TestUtil.compile('<input ng-model="model" ui-scientific-notation-mask>');
78108
var model = input.controller('ngModel');

0 commit comments

Comments
 (0)