Skip to content

Commit

Permalink
fix: issue with number cast [ref:#791,#705]
Browse files Browse the repository at this point in the history
  • Loading branch information
igorn-mwp committed Nov 24, 2020
1 parent 32f72b0 commit 6d6d5bf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<a name="11.1.1"></a>

# 11.1.1 (2020-11-24)

### Bug Fixes

Number cast bug. Now if model value number mask should ([#791](https://github.com/JsDaddy/ngx-mask/issues/791)) ([#702](https://github.com/JsDaddy/ngx-mask/issues/702)) ([829](https://github.com/JsDaddy/ngx-mask/pull/829))

<a name="11.1.0"></a>

# 11.1.0 (2020-11-23)

### Bug Fixes

Copy paste event should work as expected ([#803](https://github.com/JsDaddy/ngx-mask/issues/765)) ([826](https://github.com/JsDaddy/ngx-mask/pull/827))
Copy paste event should work as expected ([#765](https://github.com/JsDaddy/ngx-mask/issues/765)) ([827](https://github.com/JsDaddy/ngx-mask/pull/827))

<a name="11.0.1"></a>

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ci": "npm run lint && npm run build:lib && npm run test:all && npm run build",
"copy-libdocs": "copyfiles -V -E README.md LICENSE dist/ngx-mask-lib",
"fix:prettier": "prettier --write \"./**/*.{js,json,md,ts,html,scss}\"",
"fix:lint": "npm run lint -- --fix",
"lint": "concurrently --kill-others-on-fail \"ng lint showcase\" \"ng lint ngx-mask-lib\" \"npm run lint:markdown\" \"npm run lint:prettier\"",
"lint:markdown": "markdownlint -i projects/ngx-mask-lib/coverage -i node_modules -i CHANGELOG.md ./",
"lint:prettier": "prettier --check \"./**/*.{js,json,md,ts,html,scss}\"",
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/src/lib/mask-applier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class MaskApplierService {
newPosition++;
}

let actualShift: number = justPasted ? cursor : (this._shift.has(position) ? shift : 0);
let actualShift: number = justPasted ? cursor : this._shift.has(position) ? shift : 0;
if (stepBack) {
actualShift--;
}
Expand Down
33 changes: 22 additions & 11 deletions projects/ngx-mask-lib/src/lib/mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,27 @@ export class MaskService extends MaskApplierService {
private formControlResult(inputValue: string): void {
if (!this.writingValue) {
if (Array.isArray(this.dropSpecialCharacters)) {
this.onChange(this._removeMask(this._removeSuffix(this._removePrefix(inputValue)), this.dropSpecialCharacters));
this.onChange(
this._toNumber(
this._removeMask(this._removeSuffix(this._removePrefix(inputValue)), this.dropSpecialCharacters)
)
);
} else if (this.dropSpecialCharacters) {
this.onChange(this._checkSymbols(inputValue));
this.onChange(this._toNumber(this._checkSymbols(inputValue)));
} else {
this.onChange(this._removeSuffix(this._removePrefix(inputValue)));
}
}
}

private _toNumber(value: string | number | undefined | null) {
if (!this.isNumberValue) {
return value;
}
const num = Number(value);
return Number.isNaN(num) ? value : num;
}

private _removeMask(value: string, specialCharactersForRemove: string[]): string {
return value ? value.replace(this._regExpForRemove(specialCharactersForRemove), '') : value;
}
Expand Down Expand Up @@ -360,17 +372,16 @@ export class MaskService extends MaskApplierService {
separatorValue = separatorValue.replace(this.decimalMarker, '.');
}

if (this.isNumberValue) {
if (separatorPrecision) {
if (result === this.decimalMarker) {
return null;
}
return this._checkPrecision(this.maskExpression, separatorValue);
} else {
return Number(separatorValue);
if (!this.isNumberValue) {
return separatorValue;
}
if (separatorPrecision) {
if (result === this.decimalMarker) {
return null;
}
return this._checkPrecision(this.maskExpression, separatorValue);
} else {
return separatorValue;
return Number(separatorValue);
}
}

Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-mask-lib/src/test/allow-negative-numbers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Directive: Mask (Allow negative numbers)', () => {

component.form.setValue(-123456);
equal('-123456.00', '123,456.00', fixture);
expect(component.form.value).toBe('123456.00');
expect(component.form.value).toBe(123456);
});

it('FormControl and NgModel should be filled with negative values', () => {
Expand All @@ -41,6 +41,6 @@ describe('Directive: Mask (Allow negative numbers)', () => {
component.form.setValue(-123456);

equal('-123456.00', '-123,456.00', fixture);
expect(component.form.value).toBe('-123456.00');
expect(component.form.value).toBe(-123456);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ describe('Directive: Mask (Drop special characters)', () => {
// @todo add backspace event check

equal('1234567.89', '1 234 567.89', fixture);
expect(component.form.value).toBe('1234567.89');
expect(component.form.value).toBe(1234567.89);
});
});

0 comments on commit 6d6d5bf

Please sign in to comment.