diff --git a/CHANGELOG.md b/CHANGELOG.md index 98aa50c3..ee4dcd6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ -# 17.1.2(2024-07-05) +# 17.1.3(2024-07-16) + +### Fix + +- Fix ([#1386](https://github.com/JsDaddy/ngx-mask/issues/1386)) +- Fix ([#1370](https://github.com/JsDaddy/ngx-mask/issues/1370)) + +# 17.1.2(2024-07-15) ### Fix diff --git a/package.json b/package.json index ece6ff22..8f0bf700 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-mask", - "version": "17.1.2", + "version": "17.1.3", "description": "Awesome ngx mask", "license": "MIT", "engines": { diff --git a/projects/ngx-mask-lib/package.json b/projects/ngx-mask-lib/package.json index 5f83f1b8..0e348689 100644 --- a/projects/ngx-mask-lib/package.json +++ b/projects/ngx-mask-lib/package.json @@ -1,6 +1,6 @@ { "name": "ngx-mask", - "version": "17.1.2", + "version": "17.1.3", "description": "awesome ngx mask", "keywords": [ "ng2-mask", diff --git a/projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts b/projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts index 8110db1e..ec382b5a 100644 --- a/projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts +++ b/projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts @@ -596,10 +596,8 @@ export class NgxMaskApplierService { result += inputSymbol; cursor++; } else if ( - (inputSymbol === MaskExpression.WHITE_SPACE && - maskExpression[cursor] === MaskExpression.WHITE_SPACE) || - (inputSymbol === MaskExpression.SLASH && - maskExpression[cursor] === MaskExpression.SLASH) + this.specialCharacters.includes(inputSymbol) && + maskExpression[cursor] === inputSymbol ) { result += inputSymbol; cursor++; @@ -642,6 +640,7 @@ export class NgxMaskApplierService { ) { cursor++; } + cursor++; i--; } else if ( diff --git a/projects/ngx-mask-lib/src/test/basic-logic.spec.ts b/projects/ngx-mask-lib/src/test/basic-logic.spec.ts index a588d932..57997473 100644 --- a/projects/ngx-mask-lib/src/test/basic-logic.spec.ts +++ b/projects/ngx-mask-lib/src/test/basic-logic.spec.ts @@ -299,7 +299,7 @@ describe('Directive: Mask', () => { equal('(', '(', fixture); equal('(1', '(1', fixture); equal('(12', '(12', fixture); - equal('(12)', '(12) ', fixture); + equal('(12)', '(12)', fixture); equal('(12) 3', '(12) 3', fixture); equal('(12) 34', '(12) 34', fixture); equal('(12) 345', '(12) 345', fixture); @@ -896,4 +896,36 @@ describe('Directive: Mask', () => { equal(']', '', fixture); equal('=', '', fixture); }); + + it('optional mask should work correct 99-99', () => { + component.mask = '99-99'; + equal('1', '1', fixture); + equal('12', '12', fixture); + equal('123', '12-3', fixture); + equal('1234', '12-34', fixture); + equal('1-2', '1-2', fixture); + equal('1-23', '1-23', fixture); + }); + + it('custom mask with optional symbol should work correct mask=999-999-999', () => { + component.mask = '999-999-999'; + equal('1', '1', fixture); + equal('12', '12', fixture); + equal('123', '123', fixture); + equal('1234', '123-4', fixture); + equal('12345', '123-45', fixture); + equal('123456', '123-456', fixture); + equal('1234567', '123-456-7', fixture); + equal('12345678', '123-456-78', fixture); + equal('123456789', '123-456-789', fixture); + equal('1-', '1-', fixture); + equal('1-2', '1-2', fixture); + equal('1-2-3', '1-2-3', fixture); + equal('1-2-345', '1-2-345', fixture); + equal('12-3-45', '12-3-45', fixture); + equal('12-34-56', '12-34-56', fixture); + equal('12-34-567', '12-34-567', fixture); + equal('123-4-5', '123-4-5', fixture); + equal('123-45-6', '123-45-6', fixture); + }); }); diff --git a/projects/ngx-mask-lib/src/test/cursor.cy-spec.ts b/projects/ngx-mask-lib/src/test/cursor.cy-spec.ts index a731ff7c..205fd2e7 100644 --- a/projects/ngx-mask-lib/src/test/cursor.cy-spec.ts +++ b/projects/ngx-mask-lib/src/test/cursor.cy-spec.ts @@ -349,7 +349,7 @@ describe('Test Date Hh:m0', () => { .should('have.value', '(123) 4') .type('{backspace}'.repeat(2)) - .should('have.prop', 'selectionStart', 4); + .should('have.prop', 'selectionStart', 5); }); it('dynamic mask after backspace should have right cursor position (00) 00000000||+00 (00) 00000000', () => { @@ -365,6 +365,6 @@ describe('Test Date Hh:m0', () => { .should('have.value', '(12) 3') .type('{backspace}'.repeat(2)) - .should('have.prop', 'selectionStart', 3); + .should('have.prop', 'selectionStart', 4); }); }); diff --git a/projects/ngx-mask-lib/src/test/custom-patterns.spec.ts b/projects/ngx-mask-lib/src/test/custom-patterns.spec.ts index adad0640..e2d2c0f6 100644 --- a/projects/ngx-mask-lib/src/test/custom-patterns.spec.ts +++ b/projects/ngx-mask-lib/src/test/custom-patterns.spec.ts @@ -178,3 +178,75 @@ describe('Directive: Mask (Provide custom patterns with symbol f and F)', () => }); }); }); + +describe('Directive: Mask (Provide custom patterns with symbol B optional)', () => { + let fixture: ComponentFixture; + let component: TestMaskComponent; + const ngxMaskCustomPatterns = { + '0': { pattern: new RegExp('\\d') }, + A: { pattern: new RegExp('[0-9,;]'), optional: false }, + B: { pattern: new RegExp('[0-9,;]'), optional: true }, + }; + + const ngxMaskConfigValue: Partial = { + patterns: { ...initialConfig.patterns, ...ngxMaskCustomPatterns }, + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [TestMaskComponent], + imports: [ReactiveFormsModule, NgxMaskDirective], + providers: [provideNgxMask(ngxMaskConfigValue)], + }); + fixture = TestBed.createComponent(TestMaskComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('custom mask with optional symbol should work correct mask=(000) 000-0000 x BBBBBBBBBB', () => { + component.mask = '(000) 000-0000 x BBBBBBBBBB'; + component.specialCharacters = ['(', ')', ' ', '-', 'x']; + equal('1', '(1', fixture); + equal('12', '(12', fixture); + equal('123', '(123', fixture); + equal('1234', '(123) 4', fixture); + equal('12345', '(123) 45', fixture); + equal('123456', '(123) 456', fixture); + equal('1234567', '(123) 456-7', fixture); + equal('12345678', '(123) 456-78', fixture); + equal('123456789', '(123) 456-789', fixture); + equal('1234567890', '(123) 456-7890', fixture); + equal('1234567890 1', '(123) 456-7890 x 1', fixture); + equal('1234567890 12', '(123) 456-7890 x 12', fixture); + equal('1234567890 123', '(123) 456-7890 x 123', fixture); + equal('1234567890 1234', '(123) 456-7890 x 1234', fixture); + equal('1234567890 12345', '(123) 456-7890 x 12345', fixture); + equal('1234567890 123456', '(123) 456-7890 x 123456', fixture); + equal('1234567890 1234567', '(123) 456-7890 x 1234567', fixture); + equal('1234567890 12345678', '(123) 456-7890 x 12345678', fixture); + equal('1234567890 123456789', '(123) 456-7890 x 123456789', fixture); + equal('1234567890 1234567890', '(123) 456-7890 x 1234567890', fixture); + }); + + it('custom mask with optional symbol should work correct mask=BBB-BBB-BBB', () => { + component.mask = 'BBB-BBB-BBB'; + equal('1', '1', fixture); + equal('12', '12', fixture); + equal('123', '123', fixture); + equal('1234', '123-4', fixture); + equal('12345', '123-45', fixture); + equal('123456', '123-456', fixture); + equal('1234567', '123-456-7', fixture); + equal('12345678', '123-456-78', fixture); + equal('123456789', '123-456-789', fixture); + equal('1-', '1-', fixture); + equal('1-2', '1-2', fixture); + equal('1-2-3', '1-2-3', fixture); + equal('1-2-345', '1-2-345', fixture); + equal('12-3-45', '12-3-45', fixture); + equal('12-34-56', '12-34-56', fixture); + equal('12-34-567', '12-34-567', fixture); + equal('123-4-5', '123-4-5', fixture); + equal('123-45-6', '123-45-6', fixture); + }); +}); diff --git a/projects/ngx-mask-lib/src/test/delete.cy-spec.ts b/projects/ngx-mask-lib/src/test/delete.cy-spec.ts index 9c287289..90ee2560 100644 --- a/projects/ngx-mask-lib/src/test/delete.cy-spec.ts +++ b/projects/ngx-mask-lib/src/test/delete.cy-spec.ts @@ -118,7 +118,7 @@ describe('Directive: Mask (Delete)', () => { .should('have.value', '(12) ') .type('{rightArrow}') .type('{backspace}') - .should('have.prop', 'selectionStart', 3); + .should('have.prop', 'selectionStart', 4); }); it('should return value from ctrl+V', () => {