Skip to content

Commit 7f53ac8

Browse files
test: add unit tests to negative numbers in separator feature
1 parent ffe5528 commit 7f53ac8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

projects/ngx-mask-lib/src/lib/mask-applier.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,10 @@ export class MaskApplierService {
358358
let res: string = x[0];
359359
const separatorLimit: string = this.separatorLimit.replace(/\s/g, '');
360360
if (separatorLimit && +separatorLimit) {
361-
res = res.slice(0, separatorLimit.length);
361+
if(res[0] === '-')
362+
res = `-${res.slice(1, res.length).slice(0, separatorLimit.length)}`;
363+
else
364+
res = res.slice(0, separatorLimit.length);
362365
}
363366
const rgx: RegExp = /(\d+)(\d{3})/;
364367
while (rgx.test(res)) {

projects/ngx-mask-lib/src/test/separator.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,36 @@ describe('Separator: Mask', () => {
3131
equal('100', '100', fixture);
3232
});
3333

34+
it('separator for -100', () => {
35+
component.mask = 'separator';
36+
equal('-100', '-100', fixture);
37+
});
38+
3439
it('separator for 1000', () => {
3540
component.mask = 'separator';
3641
equal('1000', '1 000', fixture);
3742
});
3843

44+
it('separator for -1000', () => {
45+
component.mask = 'separator';
46+
equal('-1000', '-1 000', fixture);
47+
});
48+
3949
it('separator for 10000', () => {
4050
component.mask = 'separator';
4151
equal('10000', '10 000', fixture);
4252
});
4353

54+
it('separator for -10000', () => {
55+
component.mask = 'separator';
56+
equal('-10000', '-10 000', fixture);
57+
});
58+
59+
it('separator for -100000', () => {
60+
component.mask = 'separator';
61+
equal('-100000', '-100 000', fixture);
62+
});
63+
4464
it('separator for 100000', () => {
4565
component.mask = 'separator';
4666
equal('100000', '100 000', fixture);
@@ -51,6 +71,11 @@ describe('Separator: Mask', () => {
5171
equal('1000000', '1 000 000', fixture);
5272
});
5373

74+
it('separator for -1000000', () => {
75+
component.mask = 'separator';
76+
equal('-1000000', '-1 000 000', fixture);
77+
});
78+
5479
it('should limit separator to 1000', () => {
5580
component.mask = 'separator';
5681
component.separatorLimit = '1000';
@@ -62,6 +87,11 @@ describe('Separator: Mask', () => {
6287
equal('1000000.00', '1 000 000.00', fixture);
6388
});
6489

90+
it('separator precision 2 for -1000000.00', () => {
91+
component.mask = 'separator.2';
92+
equal('-1000000.00', '-1 000 000.00', fixture);
93+
});
94+
6595
it('should limit separator with precision 2 to 10000', () => {
6696
component.mask = 'separator.2';
6797
component.separatorLimit = '10000';
@@ -112,12 +142,25 @@ describe('Separator: Mask', () => {
112142
equal('1000000', '100.000', fixture);
113143
});
114144

145+
it('should limit separator thousandSeparator . to -100000', () => {
146+
component.mask = 'separator';
147+
component.thousandSeparator = '.';
148+
component.separatorLimit = '100000';
149+
equal('-1000000', '-100.000', fixture);
150+
});
151+
115152
it('separator thousandSeparator . precision 2 for 1000000.00', () => {
116153
component.mask = 'separator.2';
117154
component.thousandSeparator = '.';
118155
equal('1000000,00', '1.000.000,00', fixture);
119156
});
120157

158+
it('separator thousandSeparator . precision 2 for -1000000.00', () => {
159+
component.mask = 'separator.2';
160+
component.thousandSeparator = '.';
161+
equal('-1000000,00', '-1.000.000,00', fixture);
162+
});
163+
121164
it('separator thousandSeparator . precision 2 with 0 after point for 1000000.00', () => {
122165
component.mask = 'separator.2';
123166
component.thousandSeparator = '.';

0 commit comments

Comments
 (0)