Skip to content

Commit

Permalink
fix(kit): Number should accept all types of spaces as interchangeab…
Browse files Browse the repository at this point in the history
…le characters for `thousandSeparator`
  • Loading branch information
nsbarsukov committed Sep 14, 2023
1 parent 4bbf758 commit 18788d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function createThousandSeparatorPostprocessor({

const prefixReg = new RegExp(`^${escapeRegExp(prefix)}${CHAR_MINUS}?`);
const postfixReg = new RegExp(`${escapeRegExp(postfix)}$`);
const isAllSpaces = (...chars: string[]): boolean => chars.every(x => /\s/.test(x));

return ({value, selection}) => {
const [integerPart, decimalPart = ''] = value.split(decimalSeparator);
Expand All @@ -43,8 +44,11 @@ export function createThousandSeparatorPostprocessor({
formattedValuePart.length &&
(formattedValuePart.length + 1) % 4 === 0;

if (char === thousandSeparator && isPositionForSeparator) {
return char + formattedValuePart;
if (
isPositionForSeparator &&
(char === thousandSeparator || isAllSpaces(char, thousandSeparator))
) {
return thousandSeparator + formattedValuePart;
}

if (char === thousandSeparator && !isPositionForSeparator) {
Expand Down
10 changes: 10 additions & 0 deletions projects/kit/src/lib/masks/number/tests/number-mask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ describe('Number (maskitoTransform)', () => {
expect(maskitoTransform('−120 343', options)).toBe('−120.343');
});
});

it('should accept simple and non-breaking spaces as interchangeable characters for [thousandSeparator]', () => {
const options = maskitoNumberOptionsGenerator({
postfix: ' $',
thousandSeparator: ' ',
});

expect(maskitoTransform('45 001 $', options)).toBe('45 001 $'); // initialization phase
expect(maskitoTransform('45 001 $', options)).toBe('45 001 $'); // next user interaction
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function generateMaskExpression({
? `[${CHAR_MINUS}${pseudoMinuses.map(x => `\\${x}`).join('')}]?`
: '';
const integerPart = thousandSeparator
? `[${digit}${escapeRegExp(thousandSeparator)}]*`
? `[${digit}${escapeRegExp(thousandSeparator).replace(/\s/g, '\\s')}]*`
: `[${digit}]*`;
const decimalPart =
precision > 0
Expand Down

0 comments on commit 18788d9

Please sign in to comment.