From 6d6d5bfdf4cdaefe6e69dabbf14f9786ad49be78 Mon Sep 17 00:00:00 2001 From: Igor Nepipenko Date: Tue, 24 Nov 2020 13:15:10 +0200 Subject: [PATCH] fix: issue with number cast [ref:#791,#705] --- CHANGELOG.md | 10 +++++- package.json | 1 + .../src/lib/mask-applier.service.ts | 2 +- projects/ngx-mask-lib/src/lib/mask.service.ts | 33 ++++++++++++------- .../src/test/allow-negative-numbers.spec.ts | 4 +-- .../src/test/drop-special-charaters.spec.ts | 2 +- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20e1ee94..69112c4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,18 @@ + + +# 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)) + # 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)) diff --git a/package.json b/package.json index 03feaa84..636e0b3e 100644 --- a/package.json +++ b/package.json @@ -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}\"", diff --git a/projects/ngx-mask-lib/src/lib/mask-applier.service.ts b/projects/ngx-mask-lib/src/lib/mask-applier.service.ts index c35ee22b..2c5e8012 100644 --- a/projects/ngx-mask-lib/src/lib/mask-applier.service.ts +++ b/projects/ngx-mask-lib/src/lib/mask-applier.service.ts @@ -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--; } diff --git a/projects/ngx-mask-lib/src/lib/mask.service.ts b/projects/ngx-mask-lib/src/lib/mask.service.ts index 6198ea79..52dd720f 100644 --- a/projects/ngx-mask-lib/src/lib/mask.service.ts +++ b/projects/ngx-mask-lib/src/lib/mask.service.ts @@ -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; } @@ -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); } } diff --git a/projects/ngx-mask-lib/src/test/allow-negative-numbers.spec.ts b/projects/ngx-mask-lib/src/test/allow-negative-numbers.spec.ts index b747e18f..6c31f47e 100644 --- a/projects/ngx-mask-lib/src/test/allow-negative-numbers.spec.ts +++ b/projects/ngx-mask-lib/src/test/allow-negative-numbers.spec.ts @@ -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', () => { @@ -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); }); }); diff --git a/projects/ngx-mask-lib/src/test/drop-special-charaters.spec.ts b/projects/ngx-mask-lib/src/test/drop-special-charaters.spec.ts index 3d462037..b80095fc 100644 --- a/projects/ngx-mask-lib/src/test/drop-special-charaters.spec.ts +++ b/projects/ngx-mask-lib/src/test/drop-special-charaters.spec.ts @@ -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); }); });