Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(#601) #1168

Merged
merged 10 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 16.1.3(2023-06-08)

### Fix

- Fix ([#601](https://github.com/JsDaddy/ngx-mask/issues/722))
- Fix ([#1042](https://github.com/JsDaddy/ngx-mask/issues/722))
- Fix ([#680](https://github.com/JsDaddy/ngx-mask/issues/722))

# 16.1.2(2023-06-05)

### Fix
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,18 @@ Masked value: 1.234
Input value: 1234.56
Masked value: 1,234
```
```html
<input type="text" mask="separator.2" [leadZero]="true" />
```
To add zeros to the model at the end

```text
Input value: 12
Masked value: 12.00

Input value: 12.1
Masked value: 12.10
```

```html
<input type="text" mask="separator.2" separatorLimit="1000" />
Expand Down
6 changes: 6 additions & 0 deletions projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class NgxMaskApplierService {

public leadZeroDateTime: IConfig['leadZeroDateTime'] = this._config.leadZeroDateTime;

public leadZero: IConfig['leadZero'] = this._config.leadZero;

private _shift: Set<number> = new Set();

public maskExpression = '';
Expand Down Expand Up @@ -120,6 +122,7 @@ export class NgxMaskApplierService {
maskExpression = '000.000.000-00';
}
}

if (maskExpression.startsWith('percent')) {
if (
inputValue.match('[a-z]|[A-Z]') ||
Expand Down Expand Up @@ -179,6 +182,7 @@ export class NgxMaskApplierService {
!backspaced
? inputValue.slice(0, inputValue.length - 1)
: inputValue;

if (backspaced) {
// eslint-disable-next-line no-param-reassign
inputValue = this._compareOrIncludes(
Expand Down Expand Up @@ -229,12 +233,14 @@ export class NgxMaskApplierService {
new RegExp(thousandSeparatorCharEscaped, 'g'),
''
);

result = this._formatWithSeparators(
strForSep,
this.thousandSeparator,
this.decimalMarker,
precision
);

const commaShift: number = result.indexOf(',') - inputValue.indexOf(',');
const shiftStep: number = result.length - inputValue.length;

Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-mask-lib/src/lib/ngx-mask.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IConfig {
separatorLimit: string;
allowNegativeNumbers: boolean;
leadZeroDateTime: boolean;
leadZero: boolean;
triggerOnMaskChange: boolean;
maskFilled: EventEmitter<void>;
patterns: {
Expand Down Expand Up @@ -53,6 +54,7 @@ export const initialConfig: IConfig = {
// eslint-disable-next-line @typescript-eslint/quotes
specialCharacters: ['-', '/', '(', ')', '.', ':', ' ', '+', ',', '@', '[', ']', '"', "'"],
leadZeroDateTime: false,
leadZero: false,
triggerOnMaskChange: false,
maskFilled: new EventEmitter<void>(),
patterns: {
Expand Down
25 changes: 23 additions & 2 deletions projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida

@Input() public leadZeroDateTime: IConfig['leadZeroDateTime'] | null = null;

@Input() public leadZero: IConfig['leadZero'] | null = null;

@Input() public triggerOnMaskChange: IConfig['triggerOnMaskChange'] | null = null;

@Output() public maskFilled: IConfig['maskFilled'] = new EventEmitter<void>();
Expand Down Expand Up @@ -129,6 +131,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
separatorLimit,
allowNegativeNumbers,
leadZeroDateTime,
leadZero,
triggerOnMaskChange,
} = changes;
if (maskExpression) {
Expand Down Expand Up @@ -211,6 +214,9 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
if (leadZeroDateTime) {
this._maskService.leadZeroDateTime = leadZeroDateTime.currentValue;
}
if (leadZero) {
this._maskService.leadZero = leadZero.currentValue;
}
if (triggerOnMaskChange) {
this._maskService.triggerOnMaskChange = triggerOnMaskChange.currentValue;
}
Expand Down Expand Up @@ -580,22 +586,37 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
if (typeof inputValue === 'number' || this._maskValue.startsWith('separator')) {
// eslint-disable-next-line no-param-reassign
inputValue = this._maskService.numberToString(inputValue);
const localeDecimalMarker = this._currentLocaleDecimalMarker();
if (!Array.isArray(this._maskService.decimalMarker)) {
const localeDecimalMarker = this._currentLocaleDecimalMarker();
// eslint-disable-next-line no-param-reassign
inputValue =
this._maskService.decimalMarker !== localeDecimalMarker
? inputValue.replace(localeDecimalMarker, this._maskService.decimalMarker)
: inputValue;
}
if (
this._maskService.leadZero &&
inputValue &&
this.maskExpression &&
this.dropSpecialCharacters !== false
) {
// eslint-disable-next-line no-param-reassign
inputValue = this._maskService._checkPrecision(
this.maskExpression.toString(),
inputValue as string
);
if (this._maskService.decimalMarker === ',') {
// eslint-disable-next-line no-param-reassign
inputValue = inputValue.toString().replace('.', ',');
}
}
this._maskService.isNumberValue = true;
}

if (typeof inputValue !== 'string') {
// eslint-disable-next-line no-param-reassign
inputValue = '';
}

this._inputValue = inputValue;
this._setMask();

Expand Down
28 changes: 22 additions & 6 deletions projects/ngx-mask-lib/src/lib/ngx-mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ export class NgxMaskService extends NgxMaskApplierService {
* 1e-7 -> '0.0000001'
*/
public numberToString(value: number | string): string {
if (!value && value !== 0) {
if (
(!value && value !== 0) ||
(this.maskExpression.startsWith('separator') &&
(this.leadZero || !this.dropSpecialCharacters))
) {
return String(value);
}
return Number(value).toLocaleString('fullwide', {
Expand Down Expand Up @@ -454,6 +458,12 @@ export class NgxMaskService extends NgxMaskApplierService {
if (!this.isNumberValue || value === '') {
return value;
}
if (
this.maskExpression.startsWith('separator') &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we create enum for this kind of strings ?

(this.leadZero || !this.dropSpecialCharacters)
) {
return value;
}
const num = Number(value);
return Number.isNaN(num) ? value : num;
}
Expand Down Expand Up @@ -503,7 +513,7 @@ export class NgxMaskService extends NgxMaskApplierService {
return value.replace(this._regExpForRemove(markers), '.');
}

private _checkSymbols(result: string): string | number | undefined | null {
public _checkSymbols(result: string): string | number | undefined | null {
if (result === '') {
return result;
}
Expand Down Expand Up @@ -536,11 +546,17 @@ export class NgxMaskService extends NgxMaskApplierService {
return matcher ? Number(matcher[1]) : null;
}

private _checkPrecision(separatorExpression: string, separatorValue: string): number | string {
if (separatorExpression.indexOf('2') > 0) {
return Number(separatorValue).toFixed(2);
public _checkPrecision(separatorExpression: string, separatorValue: string): number | string {
const separatorPrecision = separatorExpression.slice(10, 11);
if (
separatorExpression.indexOf('2') > 0 ||
(this.leadZero && Number(separatorPrecision) > 1)
) {
return this.leadZero
? Number(separatorValue).toFixed(Number(separatorPrecision))
: Number(separatorValue).toFixed(2);
}
return Number(separatorValue);
return this.numberToString(separatorValue);
}

public _repeatPatternSymbols(maskExp: string): string {
Expand Down
Loading