From 73975bbc676487330359056c367f73e32ea6eaf4 Mon Sep 17 00:00:00 2001 From: Nikita Barsukov Date: Fri, 15 Sep 2023 10:41:11 +0300 Subject: [PATCH] fix(kit): `Number` should accept all types of spaces as interchangeable characters for `thousandSeparator` (#505) --- .../processors/thousand-separator-postprocessor.ts | 8 ++++++-- .../kit/src/lib/masks/number/tests/number-mask.spec.ts | 10 ++++++++++ .../lib/masks/number/utils/generate-mask-expression.ts | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/projects/kit/src/lib/masks/number/processors/thousand-separator-postprocessor.ts b/projects/kit/src/lib/masks/number/processors/thousand-separator-postprocessor.ts index b6e08669c..b10eaedbf 100644 --- a/projects/kit/src/lib/masks/number/processors/thousand-separator-postprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/thousand-separator-postprocessor.ts @@ -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); @@ -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) { diff --git a/projects/kit/src/lib/masks/number/tests/number-mask.spec.ts b/projects/kit/src/lib/masks/number/tests/number-mask.spec.ts index 01233d89e..a7f61e77f 100644 --- a/projects/kit/src/lib/masks/number/tests/number-mask.spec.ts +++ b/projects/kit/src/lib/masks/number/tests/number-mask.spec.ts @@ -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 + }); }); diff --git a/projects/kit/src/lib/masks/number/utils/generate-mask-expression.ts b/projects/kit/src/lib/masks/number/utils/generate-mask-expression.ts index e75e3a543..fb2f1373b 100644 --- a/projects/kit/src/lib/masks/number/utils/generate-mask-expression.ts +++ b/projects/kit/src/lib/masks/number/utils/generate-mask-expression.ts @@ -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