Skip to content

Commit

Permalink
Fix/issue (#1334)
Browse files Browse the repository at this point in the history
* feat(ref: #1298, #1302, #1301): fix issues

* fix(ref: #1292): fix percent mask with zero at start

* fix(ref: no-ref): remove unusable code

* fix(ref: no-ref): remove unusable code

* fix(ref: no-ref): remove unusable code

* fix(ref: no-ref): remove unusable code

* fix(ref: no-ref): replace + into template string
  • Loading branch information
andriikamaldinov1 authored Mar 27, 2024
1 parent 1750b39 commit a440a24
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 94 deletions.
24 changes: 17 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# 17.0.6(2024-03-27)

### Fix

- Fix ([#1301](https://github.com/JsDaddy/ngx-mask/issues/1301))
- Fix ([#1302](https://github.com/JsDaddy/ngx-mask/issues/1302))
- Fix ([#1298](https://github.com/JsDaddy/ngx-mask/issues/1298))
- Fix ([#1292](https://github.com/JsDaddy/ngx-mask/issues/1292))

# 17.0.5(2024-03-25)

### Fix

https://github.com/JsDaddy/ngx-mask/issues/1295
https://github.com/JsDaddy/ngx-mask/issues/1292
https://github.com/JsDaddy/ngx-mask/issues/1314
https://github.com/JsDaddy/ngx-mask/issues/1310
https://github.com/JsDaddy/ngx-mask/issues/1304
https://github.com/JsDaddy/ngx-mask/issues/1308
https://github.com/JsDaddy/ngx-mask/issues/1299
- Fix ([#1295](https://github.com/JsDaddy/ngx-mask/issues/1295))
- Fix ([#1292](https://github.com/JsDaddy/ngx-mask/issues/1292))
- Fix ([#1314](https://github.com/JsDaddy/ngx-mask/issues/1314))
- Fix ([#1310](https://github.com/JsDaddy/ngx-mask/issues/1310))
- Fix ([#1304](https://github.com/JsDaddy/ngx-mask/issues/1304))
- Fix ([#1308](https://github.com/JsDaddy/ngx-mask/issues/1308))
- Fix ([#1299](https://github.com/JsDaddy/ngx-mask/issues/1299))


# 17.0.4(2023-12-01)

Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-mask",
"version": "17.0.5",
"version": "17.0.6",
"description": "awesome ngx mask",
"keywords": [
"ng2-mask",
Expand Down
21 changes: 13 additions & 8 deletions projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ export class NgxMaskApplierService {
let value = '';
this.allowNegativeNumbers &&
inputValue.slice(cursor, cursor + 1) === MaskExpression.MINUS
? (value =
MaskExpression.MINUS +
inputValue.slice(cursor + 1, cursor + inputValue.length))
? (value = `${MaskExpression.MINUS}${inputValue.slice(cursor + 1, cursor + inputValue.length)}`)
: (value = inputValue);
if (this.percentage(value)) {
result = this._splitPercentZero(inputValue);
Expand Down Expand Up @@ -576,7 +574,8 @@ export class NgxMaskApplierService {
!this.specialCharacters.includes(
inputValueSliceMinusTwoCursor as string
) &&
Number(inputValueSliceMinusTwoCursor) > monthsCount;
Number(inputValueSliceMinusTwoCursor) > monthsCount &&
maskExpression.includes('d0');
// 10<day<31 && month<12 for paste whole data
const day2monthPaste: boolean =
Number(inputValueSliceMinusThreeMinusOne) <= daysCount &&
Expand Down Expand Up @@ -941,18 +940,24 @@ export class NgxMaskApplierService {
typeof this.decimalMarker === 'string'
? value.indexOf(this.decimalMarker)
: value.indexOf(MaskExpression.DOT);
const emptyOrMinus =
this.allowNegativeNumbers && value.includes(MaskExpression.MINUS) ? '-' : '';
if (decimalIndex === -1) {
const parsedValue = parseInt(value, 10);
return isNaN(parsedValue) ? MaskExpression.EMPTY_STRING : parsedValue.toString();
const parsedValue = parseInt(emptyOrMinus ? value.slice(1, value.length) : value, 10);
return isNaN(parsedValue)
? MaskExpression.EMPTY_STRING
: `${emptyOrMinus}${parsedValue}`;
} else {
const integerPart = parseInt(value.substring(0, decimalIndex), 10);
const integerPart = parseInt(value.replace('-', '').substring(0, decimalIndex), 10);
const decimalPart = value.substring(decimalIndex + 1);
const integerString = isNaN(integerPart) ? '' : integerPart.toString();

const decimal =
typeof this.decimalMarker === 'string' ? this.decimalMarker : MaskExpression.DOT;

return integerString === MaskExpression.EMPTY_STRING
? MaskExpression.EMPTY_STRING
: integerString + decimal + decimalPart;
: `${emptyOrMinus}${integerString}${decimal}${decimalPart}`;
}
}
}
91 changes: 32 additions & 59 deletions projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,30 +469,23 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
if (this._code === MaskExpression.BACKSPACE) {
if (!selectRangeBackspace) {
if (this._maskService.selStart === prefixLength) {
this._maskService.actualValue =
this.prefix +
this._maskService.maskIsShown.slice(0, selEnd) +
this._inputValue.split(this.prefix).join('');
this._maskService.actualValue = `${this.prefix}${this._maskService.maskIsShown.slice(0, selEnd)}${this._inputValue.split(this.prefix).join('')}`;
} else if (
this._maskService.selStart ===
this._maskService.maskIsShown.length + prefixLength
) {
this._maskService.actualValue =
this._inputValue +
this._maskService.maskIsShown.slice(selStart, selEnd);
this._maskService.actualValue = `${this._inputValue}${this._maskService.maskIsShown.slice(selStart, selEnd)}`;
} else {
this._maskService.actualValue =
this.prefix +
this._inputValue
.split(this.prefix)
.join('')
.slice(0, selStart) +
this._maskService.maskIsShown.slice(selStart, selEnd) +
this._maskService.actualValue.slice(
selEnd + prefixLength,
this._maskService.maskIsShown.length + prefixLength
) +
this.suffix;
this._maskService.actualValue = `${this.prefix}${this._inputValue
.split(this.prefix)
.join('')
.slice(
0,
selStart
)}${this._maskService.maskIsShown.slice(selStart, selEnd)}${this._maskService.actualValue.slice(
selEnd + prefixLength,
this._maskService.maskIsShown.length + prefixLength
)}${this.suffix}`;
}
} else if (
!this._maskService.specialCharacters.includes(
Expand All @@ -504,21 +497,17 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
selectRangeBackspace
) {
if (selStart === 1 && this.prefix) {
this._maskService.actualValue =
this.prefix +
this._maskService.placeHolderCharacter +
el.value
.split(this.prefix)
.join('')
.split(this.suffix)
.join('') +
this.suffix;
this._maskService.actualValue = `${this.prefix}${this._maskService.placeHolderCharacter}${el.value
.split(this.prefix)
.join('')
.split(this.suffix)
.join('')}${this.suffix}`;

position = position - 1;
} else {
const part1 = el.value.substring(0, position);
const part2 = el.value.substring(position);
this._maskService.actualValue =
part1 + this._maskService.placeHolderCharacter + part2;
this._maskService.actualValue = `${part1}${this._maskService.placeHolderCharacter}${part2}`;
}
}
}
Expand All @@ -534,31 +523,19 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
el.value.slice(position + 1, position + 2)
)
) {
this._maskService.actualValue =
el.value.slice(0, position - 1) +
el.value.slice(position, position + 1) +
inputSymbol +
el.value.slice(position + 2);
this._maskService.actualValue = `${el.value.slice(0, position - 1)}${el.value.slice(position, position + 1)}${inputSymbol}${el.value.slice(position + 2)}`;
position = position + 1;
} else if (checkSymbols) {
if (el.value.length === 1 && position === 1) {
this._maskService.actualValue =
this.prefix +
inputSymbol +
this._maskService.maskIsShown.slice(
1,
this._maskService.maskIsShown.length
) +
this.suffix;
this._maskService.actualValue = `${this.prefix}${inputSymbol}${this._maskService.maskIsShown.slice(
1,
this._maskService.maskIsShown.length
)}${this.suffix}`;
} else {
this._maskService.actualValue =
el.value.slice(0, position - 1) +
inputSymbol +
el.value
.slice(position + 1)
.split(this.suffix)
.join('') +
this.suffix;
this._maskService.actualValue = `${el.value.slice(0, position - 1)}${inputSymbol}${el.value
.slice(position + 1)
.split(this.suffix)
.join('')}${this.suffix}`;
}
} else if (
this.prefix &&
Expand All @@ -570,14 +547,10 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
MaskExpression.EMPTY_STRING
)
) {
this._maskService.actualValue =
this.prefix +
el.value +
this._maskService.maskIsShown.slice(
1,
this._maskService.maskIsShown.length
) +
this.suffix;
this._maskService.actualValue = `${this.prefix}${el.value}${this._maskService.maskIsShown.slice(
1,
this._maskService.maskIsShown.length
)}${this.suffix}`;
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions projects/ngx-mask-lib/src/lib/ngx-mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class NgxMaskService extends NgxMaskApplierService {
}
if (!inputValue && this.showMaskTyped) {
this.formControlResult(this.prefix);
return this.prefix + this.maskIsShown + this.suffix;
return `${this.prefix}${this.maskIsShown}${this.suffix}`;
}

const getSymbol: string =
Expand Down Expand Up @@ -163,7 +163,7 @@ export class NgxMaskService extends NgxMaskApplierService {
this.formControlResult(value);
return this.actualValue
? this.actualValue
: this.prefix + this.maskIsShown + this.suffix;
: `${this.prefix}${this.maskIsShown}${this.suffix}`;
}

const result: string = super.applyMask(
Expand Down Expand Up @@ -204,33 +204,33 @@ export class NgxMaskService extends NgxMaskApplierService {
this.maskChanged ||
(this._previousValue === this._currentValue && justPasted);
}

this._emitValue ? this.formControlResult(result) : '';
this._emitValue
? this.writingValue
? requestAnimationFrame(() => this.formControlResult(result))
: this.formControlResult(result)
: '';
if (!this.showMaskTyped || (this.showMaskTyped && this.hiddenInput)) {
if (this.hiddenInput) {
if (backspaced) {
return this.hideInput(result, this.maskExpression);
}
return (
this.hideInput(result, this.maskExpression) +
this.maskIsShown.slice(result.length)
);
return `${this.hideInput(result, this.maskExpression)}${this.maskIsShown.slice(result.length)}`;
}
return result;
}
const resLen: number = result.length;
const prefNmask: string = this.prefix + this.maskIsShown + this.suffix;
const prefNmask: string = `${this.prefix}${this.maskIsShown}${this.suffix}`;

if (this.maskExpression.includes(MaskExpression.HOURS)) {
const countSkipedSymbol = this._numberSkipedSymbols(result);
return result + prefNmask.slice(resLen + countSkipedSymbol);
return `${result}${prefNmask.slice(resLen + countSkipedSymbol)}`;
} else if (
this.maskExpression === MaskExpression.IP ||
this.maskExpression === MaskExpression.CPF_CNPJ
) {
return result + prefNmask;
return `${result}${prefNmask}`;
}
return result + prefNmask.slice(resLen);
return `${result}${prefNmask.slice(resLen)}`;
}

// get the number of characters that were shifted
Expand Down
9 changes: 9 additions & 0 deletions projects/ngx-mask-lib/src/test/add-prefix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,13 @@ describe('Directive: Mask (Add prefix)', () => {
equal('KZ123123', 'KZ123 123', fixture);
expect(component.form.value).toBe('123123');
});

it('should remove prefix when setValue', () => {
component.mask = '000 000';
component.prefix = 'KZ';
component.dropSpecialCharacters = false;
component.form.setValue('KZ123123');
equal('KZ123123', 'KZ123 123', fixture);
expect(component.form.value).toBe('123123');
});
});
26 changes: 26 additions & 0 deletions projects/ngx-mask-lib/src/test/drop-special-charaters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,30 @@ describe('Directive: Mask (Drop special characters)', () => {
equal('1234567890', '(123) 456-7890', fixture);
expect(component.form.valid).toBe(true);
});

it('dropSpecialCharacter = false test for valid with setValue', () => {
component.mask = '(000) 000-0000';
component.dropSpecialCharacters = false;
component.validation = true;
equal('1', '(1', fixture);
expect(component.form.valid).toBeFalsy();
equal('12', '(12', fixture);
expect(component.form.valid).toBeFalsy();
equal('123', '(123', fixture);
expect(component.form.valid).toBeFalsy();
equal('1234', '(123) 4', fixture);
expect(component.form.valid).toBeFalsy();
equal('12345', '(123) 45', fixture);
expect(component.form.valid).toBeFalsy();
equal('123456', '(123) 456', fixture);
expect(component.form.valid).toBeFalsy();
equal('1234567', '(123) 456-7', fixture);
expect(component.form.valid).toBeFalsy();
equal('12345678', '(123) 456-78', fixture);
expect(component.form.valid).toBeFalsy();
equal('123456789', '(123) 456-789', fixture);
expect(component.form.valid).toBeFalsy();
equal('1234567890', '(123) 456-7890', fixture);
expect(component.form.valid).toBeTrue();
});
});
Loading

0 comments on commit a440a24

Please sign in to comment.