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(1009): decimalMarker not locale default #1052

Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 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 @@ -555,10 +555,11 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
// eslint-disable-next-line no-param-reassign
inputValue = this._maskService.numberToString(inputValue);
if (!Array.isArray(this.decimalMarker)) {
const localeDecimalMarker = this._currentLocaleDecimalMarker();
// eslint-disable-next-line no-param-reassign
inputValue =
this.decimalMarker !== '.'
? inputValue.replace('.', this.decimalMarker)
this.decimalMarker !== localeDecimalMarker
? inputValue.replace(localeDecimalMarker, this.decimalMarker)
: inputValue;
}
this._maskService.isNumberValue = true;
Expand Down Expand Up @@ -713,4 +714,8 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
});
}
}

private _currentLocaleDecimalMarker(): string {
return (1.1).toLocaleString().substring(1, 2);
}
}
65 changes: 65 additions & 0 deletions projects/ngx-mask-lib/src/test/separator-non-en-locale.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LOCALE_ID } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { TestMaskComponent } from './utils/test-component.component';
import { equal, typeTest } from './utils/test-functions.component';
import { provideNgxMask } from '../lib/ngx-mask.providers';
import { NgxMaskDirective } from '../lib/ngx-mask.directive';

// FR locale uses comma as decimal marker
describe('Separator: Mask with FR locale', () => {
let fixture: ComponentFixture<TestMaskComponent>;
let component: TestMaskComponent;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestMaskComponent],
imports: [ReactiveFormsModule, NgxMaskDirective],
providers: [provideNgxMask(), { provide: LOCALE_ID, useValue: 'fr' }],
});
fixture = TestBed.createComponent(TestMaskComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('Should work right when reset decimalMarker', () => {
component.mask = 'separator.2';
component.decimalMarker = '.';
equal('1000000.00', '1 000 000.00', fixture);
});

it('separator precision 2 with thousandSeparator (.) decimalMarker (,) for 12345.67', () => {
component.mask = 'separator.2';
component.thousandSeparator = ',';
component.decimalMarker = '.';
equal('12,345.67', '12,345.67', fixture);
});

it('separator precision 2 with thousandSeparator (.) decimalMarker (,) for 12345.67', () => {
component.mask = 'separator.2';
component.thousandSeparator = ',';
component.decimalMarker = '.';
equal('12345.67', '12,345.67', fixture);
});

it('check formControl value to be number when decimalMarker is dot', () => {
component.mask = 'separator.2';
component.thousandSeparator = ' ';
component.decimalMarker = '.';

typeTest('12 345.67', fixture);
expect(component.form.value).toBe('12345.67');
});

it('check formControl value to be number when decimalMarker is array', () => {
component.mask = 'separator.2';
component.thousandSeparator = ' ';
component.decimalMarker = ['.', ','];

typeTest('12 345,67', fixture);
expect(component.form.value).toBe('12345.67');

typeTest('123 456.78', fixture);
expect(component.form.value).toBe('123456.78');
});
});