From 545c5c9ca721eaef978dd3bc757b26bb26f26062 Mon Sep 17 00:00:00 2001 From: aktanoff Date: Sun, 14 Jan 2024 00:55:20 +0500 Subject: [PATCH] feat(kit): unskipped tests and fixed issues --- .../number-postfix-with-point.cy.ts | 6 +- .../kit/src/lib/masks/number/number-mask.ts | 15 +- .../leading-zeroes-validation.plugin.ts | 18 +- .../masks/number/plugins/min-max.plugin.ts | 11 +- .../plugins/not-empty-integer.plugin.ts | 12 +- .../processors/affixes-filter-preprocessor.ts | 28 +++ .../decimal-zero-padding-postprocessor.ts | 11 +- .../initialization-only-preprocessor.ts | 19 +- ...leading-zeroes-validation-postprocessor.ts | 29 +++- .../processors/min-max-postprocessor.ts | 6 +- .../pseudo-character-preprocessor.ts | 5 +- ...repeated-decimal-separator-preprocessor.ts | 5 +- ...ng-zeroes-validation-postprocessor.spec.ts | 7 +- .../thousand-separator-postprocessor.ts | 17 +- .../processors/zero-precision-preprocessor.ts | 25 ++- .../masks/number/tests/number-mask.spec.ts | 162 +++++++++++++++--- ...efix-and-postfix.ts => extract-affixes.ts} | 19 +- .../lib/masks/number/utils/parse-number.ts | 2 +- 18 files changed, 290 insertions(+), 107 deletions(-) create mode 100644 projects/kit/src/lib/masks/number/processors/affixes-filter-preprocessor.ts rename projects/kit/src/lib/masks/number/utils/{extract-prefix-and-postfix.ts => extract-affixes.ts} (54%) diff --git a/projects/demo-integrations/src/tests/component-testing/number-postfix/number-postfix-with-point.cy.ts b/projects/demo-integrations/src/tests/component-testing/number-postfix/number-postfix-with-point.cy.ts index e17ddbe6a..7c5ae6541 100644 --- a/projects/demo-integrations/src/tests/component-testing/number-postfix/number-postfix-with-point.cy.ts +++ b/projects/demo-integrations/src/tests/component-testing/number-postfix/number-postfix-with-point.cy.ts @@ -35,8 +35,7 @@ describe('Number | postfix with point', () => { .should('have.prop', 'selectionEnd', 2); }); - // TODO https://github.com/taiga-family/maskito/issues/703 - it.skip('Empty => Type 0.42 => 0.42 lbs.', () => { + it('Empty => Type 0.42 => 0.42 lbs.', () => { cy.mount(TestInput, {componentProperties: {maskitoOptions}}); cy.get('input') .type('0') @@ -52,8 +51,7 @@ describe('Number | postfix with point', () => { }); }); - // TODO https://github.com/taiga-family/maskito/issues/703 - describe.skip('Complex: maskitoCaretGuard + maskitoAddOnFocusPlugin + maskitoRemoveOnBlurPlugin', () => { + describe('Complex: maskitoCaretGuard + maskitoAddOnFocusPlugin + maskitoRemoveOnBlurPlugin', () => { const postfix = ' lbs.'; const numberOptions = maskitoNumberOptionsGenerator({ postfix, diff --git a/projects/kit/src/lib/masks/number/number-mask.ts b/projects/kit/src/lib/masks/number/number-mask.ts index 927ce7e08..3be3bab7b 100644 --- a/projects/kit/src/lib/masks/number/number-mask.ts +++ b/projects/kit/src/lib/masks/number/number-mask.ts @@ -29,6 +29,7 @@ import { createThousandSeparatorPostprocessor, createZeroPrecisionPreprocessor, } from './processors'; +import {createAffixesFilterPreprocessor} from './processors/affixes-filter-preprocessor'; import {generateMaskExpression, validateDecimalPseudoSeparators} from './utils'; export function maskitoNumberOptionsGenerator({ @@ -79,7 +80,10 @@ export function maskitoNumberOptionsGenerator({ decimalSeparator, decimalPseudoSeparators: validatedDecimalPseudoSeparators, pseudoMinuses, + prefix, + postfix, }), + createAffixesFilterPreprocessor({prefix, postfix}), createFullWidthToHalfWidthPreprocessor(), createPseudoCharactersPreprocessor({ validCharacter: CHAR_MINUS, @@ -99,7 +103,12 @@ export function maskitoNumberOptionsGenerator({ decimalZeroPadding, thousandSeparator, }), - createZeroPrecisionPreprocessor(precision, decimalSeparator), + createZeroPrecisionPreprocessor({ + precision, + decimalSeparator, + prefix, + postfix, + }), createRepeatedDecimalSeparatorPreprocessor({ decimalSeparator, prefix, @@ -107,7 +116,7 @@ export function maskitoNumberOptionsGenerator({ }), ], postprocessors: [ - createMinMaxPostprocessor({decimalSeparator, min, max, prefix, postfix}), + createMinMaxPostprocessor({decimalSeparator, min, max}), maskitoPrefixPostprocessorGenerator(prefix), maskitoPostfixPostprocessorGenerator(postfix), createThousandSeparatorPostprocessor({ @@ -136,7 +145,7 @@ export function maskitoNumberOptionsGenerator({ prefix, postfix, }), - createMinMaxPlugin({min, max, decimalSeparator, prefix, postfix}), + createMinMaxPlugin({min, max, decimalSeparator}), ], overwriteMode: decimalZeroPadding ? ({value, selection: [from]}) => diff --git a/projects/kit/src/lib/masks/number/plugins/leading-zeroes-validation.plugin.ts b/projects/kit/src/lib/masks/number/plugins/leading-zeroes-validation.plugin.ts index 6a894cc1f..572072379 100644 --- a/projects/kit/src/lib/masks/number/plugins/leading-zeroes-validation.plugin.ts +++ b/projects/kit/src/lib/masks/number/plugins/leading-zeroes-validation.plugin.ts @@ -2,7 +2,7 @@ import {MaskitoPlugin, maskitoUpdateElement} from '@maskito/core'; import {maskitoEventHandler} from '../../../plugins'; import {createLeadingZeroesValidationPostprocessor} from '../processors'; -import {extractPrefixAndPostfix} from '../utils/extract-prefix-and-postfix'; +import {extractAffixes} from '../utils/extract-affixes'; const DUMMY_SELECTION = [0, 0] as const; @@ -22,20 +22,20 @@ export function createLeadingZeroesValidationPlugin({ prefix: string; postfix: string; }): MaskitoPlugin { - const dropRepeatedLeadingZeroes = createLeadingZeroesValidationPostprocessor( + const dropRepeatedLeadingZeroes = createLeadingZeroesValidationPostprocessor({ decimalSeparator, thousandSeparator, - ); + prefix, + postfix, + }); return maskitoEventHandler( 'blur', element => { - const {cleanValue, extractedPostfix, extractedPrefix} = - extractPrefixAndPostfix({ - value: element.value, - prefix, - postfix, - }); + const {cleanValue, extractedPostfix, extractedPrefix} = extractAffixes( + element.value, + {prefix, postfix}, + ); const newValue = extractedPrefix + diff --git a/projects/kit/src/lib/masks/number/plugins/min-max.plugin.ts b/projects/kit/src/lib/masks/number/plugins/min-max.plugin.ts index 3dd1afca8..410991cbd 100644 --- a/projects/kit/src/lib/masks/number/plugins/min-max.plugin.ts +++ b/projects/kit/src/lib/masks/number/plugins/min-max.plugin.ts @@ -12,24 +12,15 @@ export function createMinMaxPlugin({ min, max, decimalSeparator, - prefix, - postfix, }: { min: number; max: number; decimalSeparator: string; - prefix: string; - postfix: string; }): MaskitoPlugin { return maskitoEventHandler( 'blur', (element, options) => { - const parsedNumber = maskitoParseNumber( - element.value, - decimalSeparator, - prefix, - postfix, - ); + const parsedNumber = maskitoParseNumber(element.value, decimalSeparator); const clampedNumber = clamp(parsedNumber, min, max); if (!Number.isNaN(parsedNumber) && parsedNumber !== clampedNumber) { diff --git a/projects/kit/src/lib/masks/number/plugins/not-empty-integer.plugin.ts b/projects/kit/src/lib/masks/number/plugins/not-empty-integer.plugin.ts index 4c44bd063..7f1222d59 100644 --- a/projects/kit/src/lib/masks/number/plugins/not-empty-integer.plugin.ts +++ b/projects/kit/src/lib/masks/number/plugins/not-empty-integer.plugin.ts @@ -2,7 +2,7 @@ import {MaskitoPlugin, maskitoUpdateElement} from '@maskito/core'; import {maskitoEventHandler} from '../../../plugins'; import {escapeRegExp} from '../../../utils'; -import {extractPrefixAndPostfix} from '../utils/extract-prefix-and-postfix'; +import {extractAffixes} from '../utils/extract-affixes'; /** * It pads EMPTY integer part with zero if decimal parts exists. @@ -21,12 +21,10 @@ export function createNotEmptyIntegerPlugin({ return maskitoEventHandler( 'blur', element => { - const {cleanValue, extractedPostfix, extractedPrefix} = - extractPrefixAndPostfix({ - value: element.value, - prefix, - postfix, - }); + const {cleanValue, extractedPostfix, extractedPrefix} = extractAffixes( + element.value, + {prefix, postfix}, + ); const newValue = extractedPrefix + cleanValue.replace( diff --git a/projects/kit/src/lib/masks/number/processors/affixes-filter-preprocessor.ts b/projects/kit/src/lib/masks/number/processors/affixes-filter-preprocessor.ts new file mode 100644 index 000000000..9dba6d6e6 --- /dev/null +++ b/projects/kit/src/lib/masks/number/processors/affixes-filter-preprocessor.ts @@ -0,0 +1,28 @@ +import {MaskitoPreprocessor} from '@maskito/core'; + +import {extractAffixes} from '../utils/extract-affixes'; + +/** + * It drops prefix and postfix from data + * Needed for case, when prefix or postfix contain decimalSeparator, to ignore it in resulting number + * @example User pastes '{prefix}123.45{postfix}' => 123.45 + */ +export function createAffixesFilterPreprocessor({ + prefix, + postfix, +}: { + prefix: string; + postfix: string; +}): MaskitoPreprocessor { + return ({elementState, data}) => { + const {cleanValue: cleanData} = extractAffixes(data, { + prefix, + postfix, + }); + + return { + elementState, + data: cleanData, + }; + }; +} diff --git a/projects/kit/src/lib/masks/number/processors/decimal-zero-padding-postprocessor.ts b/projects/kit/src/lib/masks/number/processors/decimal-zero-padding-postprocessor.ts index 38563d5ed..cafb0eb41 100644 --- a/projects/kit/src/lib/masks/number/processors/decimal-zero-padding-postprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/decimal-zero-padding-postprocessor.ts @@ -2,7 +2,7 @@ import {MaskitoPostprocessor} from '@maskito/core'; import {identity} from '../../../utils'; import {maskitoParseNumber} from '../utils'; -import {extractPrefixAndPostfix} from '../utils/extract-prefix-and-postfix'; +import {extractAffixes} from '../utils/extract-affixes'; /** * If `decimalZeroPadding` is `true`, it pads decimal part with zeroes @@ -27,17 +27,12 @@ export function createDecimalZeroPaddingPostprocessor({ } return ({value, selection}) => { - const {cleanValue, extractedPrefix, extractedPostfix} = extractPrefixAndPostfix({ - value, + const {cleanValue, extractedPrefix, extractedPostfix} = extractAffixes(value, { prefix, postfix, }); - if ( - Number.isNaN( - maskitoParseNumber(cleanValue, decimalSeparator, prefix, postfix), - ) - ) { + if (Number.isNaN(maskitoParseNumber(cleanValue, decimalSeparator))) { return {value, selection}; } diff --git a/projects/kit/src/lib/masks/number/processors/initialization-only-preprocessor.ts b/projects/kit/src/lib/masks/number/processors/initialization-only-preprocessor.ts index 955a818e9..58aafdd31 100644 --- a/projects/kit/src/lib/masks/number/processors/initialization-only-preprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/initialization-only-preprocessor.ts @@ -1,6 +1,7 @@ import {MaskitoPreprocessor, maskitoTransform} from '@maskito/core'; import {generateMaskExpression} from '../utils'; +import {extractAffixes} from '../utils/extract-affixes'; /** * This preprocessor works only once at initialization phase (when `new Maskito(...)` is executed). @@ -16,10 +17,14 @@ export function createInitializationOnlyPreprocessor({ decimalSeparator, decimalPseudoSeparators, pseudoMinuses, + prefix, + postfix, }: { decimalSeparator: string; decimalPseudoSeparators: readonly string[]; pseudoMinuses: readonly string[]; + prefix: string; + postfix: string; }): MaskitoPreprocessor { let isInitializationPhase = true; const cleanNumberMask = generateMaskExpression({ @@ -40,10 +45,18 @@ export function createInitializationOnlyPreprocessor({ isInitializationPhase = false; + const {cleanValue} = extractAffixes(elementState.value, {prefix, postfix}); + return { - elementState: maskitoTransform(elementState, { - mask: cleanNumberMask, - }), + elementState: maskitoTransform( + { + ...elementState, + value: cleanValue, + }, + { + mask: cleanNumberMask, + }, + ), data, }; }; diff --git a/projects/kit/src/lib/masks/number/processors/leading-zeroes-validation-postprocessor.ts b/projects/kit/src/lib/masks/number/processors/leading-zeroes-validation-postprocessor.ts index ad926ec3b..1d0082322 100644 --- a/projects/kit/src/lib/masks/number/processors/leading-zeroes-validation-postprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/leading-zeroes-validation-postprocessor.ts @@ -1,6 +1,7 @@ import {MaskitoPostprocessor} from '@maskito/core'; import {escapeRegExp} from '../../../utils'; +import {extractAffixes} from '../utils/extract-affixes'; /** * It removes repeated leading zeroes for integer part. @@ -9,10 +10,17 @@ import {escapeRegExp} from '../../../utils'; * @example User types "000000" => 0| * @example 0| => User types "5" => 5| */ -export function createLeadingZeroesValidationPostprocessor( - decimalSeparator: string, - thousandSeparator: string, -): MaskitoPostprocessor { +export function createLeadingZeroesValidationPostprocessor({ + decimalSeparator, + thousandSeparator, + prefix, + postfix, +}: { + decimalSeparator: string; + thousandSeparator: string; + prefix: string; + postfix: string; +}): MaskitoPostprocessor { const trimLeadingZeroes = (value: string): string => { const escapedThousandSeparator = escapeRegExp(thousandSeparator); @@ -42,8 +50,13 @@ export function createLeadingZeroesValidationPostprocessor( return ({value, selection}) => { const [from, to] = selection; - const hasDecimalSeparator = value.includes(decimalSeparator); - const [integerPart, decimalPart = ''] = value.split(decimalSeparator); + const {cleanValue, extractedPrefix, extractedPostfix} = extractAffixes(value, { + prefix, + postfix, + }); + + const hasDecimalSeparator = cleanValue.includes(decimalSeparator); + const [integerPart, decimalPart = ''] = cleanValue.split(decimalSeparator); const zeroTrimmedIntegerPart = trimLeadingZeroes(integerPart); if (integerPart === zeroTrimmedIntegerPart) { @@ -55,9 +68,11 @@ export function createLeadingZeroesValidationPostprocessor( return { value: + extractedPrefix + zeroTrimmedIntegerPart + (hasDecimalSeparator ? decimalSeparator : '') + - decimalPart, + decimalPart + + extractedPostfix, selection: [Math.max(newFrom, 0), Math.max(newTo, 0)], }; }; diff --git a/projects/kit/src/lib/masks/number/processors/min-max-postprocessor.ts b/projects/kit/src/lib/masks/number/processors/min-max-postprocessor.ts index 5c61b125f..3ceaf474f 100644 --- a/projects/kit/src/lib/masks/number/processors/min-max-postprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/min-max-postprocessor.ts @@ -11,17 +11,13 @@ export function createMinMaxPostprocessor({ min, max, decimalSeparator, - prefix, - postfix, }: { min: number; max: number; decimalSeparator: string; - prefix: string; - postfix: string; }): MaskitoPostprocessor { return ({value, selection}) => { - const parsedNumber = maskitoParseNumber(value, decimalSeparator, prefix, postfix); + const parsedNumber = maskitoParseNumber(value, decimalSeparator); const limitedValue = /** * We cannot limit lower bound if user enters positive number. diff --git a/projects/kit/src/lib/masks/number/processors/pseudo-character-preprocessor.ts b/projects/kit/src/lib/masks/number/processors/pseudo-character-preprocessor.ts index 4e9ced1fe..0ae37f1fa 100644 --- a/projects/kit/src/lib/masks/number/processors/pseudo-character-preprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/pseudo-character-preprocessor.ts @@ -1,6 +1,6 @@ import {MaskitoPreprocessor} from '@maskito/core'; -import {extractPrefixAndPostfix} from '../utils/extract-prefix-and-postfix'; +import {extractAffixes} from '../utils/extract-affixes'; /** * It replaces pseudo characters with valid one. @@ -23,8 +23,7 @@ export function createPseudoCharactersPreprocessor({ return ({elementState, data}) => { const {value, selection} = elementState; - const {cleanValue, extractedPostfix, extractedPrefix} = extractPrefixAndPostfix({ - value, + const {cleanValue, extractedPostfix, extractedPrefix} = extractAffixes(value, { prefix, postfix, }); diff --git a/projects/kit/src/lib/masks/number/processors/repeated-decimal-separator-preprocessor.ts b/projects/kit/src/lib/masks/number/processors/repeated-decimal-separator-preprocessor.ts index c192eb2c8..6cf4d08b5 100644 --- a/projects/kit/src/lib/masks/number/processors/repeated-decimal-separator-preprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/repeated-decimal-separator-preprocessor.ts @@ -1,7 +1,7 @@ import {MaskitoPreprocessor} from '@maskito/core'; import {escapeRegExp} from '../../../utils'; -import {extractPrefixAndPostfix} from '../utils/extract-prefix-and-postfix'; +import {extractAffixes} from '../utils/extract-affixes'; /** * It rejects new typed decimal separator if it already exists in text field. @@ -19,9 +19,10 @@ export function createRepeatedDecimalSeparatorPreprocessor({ }): MaskitoPreprocessor { return ({elementState, data}) => { const {value, selection} = elementState; + const [from, to] = selection; - const {cleanValue} = extractPrefixAndPostfix({value, prefix, postfix}); + const {cleanValue} = extractAffixes(value, {prefix, postfix}); return { elementState, diff --git a/projects/kit/src/lib/masks/number/processors/tests/leading-zeroes-validation-postprocessor.spec.ts b/projects/kit/src/lib/masks/number/processors/tests/leading-zeroes-validation-postprocessor.spec.ts index 5acfa233a..ce6882321 100644 --- a/projects/kit/src/lib/masks/number/processors/tests/leading-zeroes-validation-postprocessor.spec.ts +++ b/projects/kit/src/lib/masks/number/processors/tests/leading-zeroes-validation-postprocessor.spec.ts @@ -1,7 +1,12 @@ import {createLeadingZeroesValidationPostprocessor} from '../leading-zeroes-validation-postprocessor'; describe('createLeadingZeroesValidationPostprocessor', () => { - const processor = createLeadingZeroesValidationPostprocessor(',', ''); + const processor = createLeadingZeroesValidationPostprocessor({ + decimalSeparator: ',', + thousandSeparator: '', + prefix: '', + postfix: '', + }); const DUMMY_INITIAL_STATE = {value: '', selection: [0, 0]} as const; const process = ( 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 62b105ff4..4e217d7bd 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 @@ -1,7 +1,8 @@ import {MaskitoPostprocessor} from '@maskito/core'; +import {CHAR_MINUS} from '../../../constants'; import {identity} from '../../../utils'; -import {extractPrefixAndPostfix} from '../utils/extract-prefix-and-postfix'; +import {extractAffixes} from '../utils/extract-affixes'; /** * It adds symbol for separating thousands. @@ -22,16 +23,23 @@ export function createThousandSeparatorPostprocessor({ return identity; } + const minusReg = new RegExp(`^${CHAR_MINUS}?`); const isAllSpaces = (...chars: string[]): boolean => chars.every(x => /\s/.test(x)); return ({value, selection}) => { - const {cleanValue, extractedPostfix, extractedPrefix} = extractPrefixAndPostfix({ - value, + const { + cleanValue: cleanValueWithPossibleMinus, + extractedPostfix, + extractedPrefix, + } = extractAffixes(value, { prefix, postfix, - shouldExtractMinus: true, }); + const [extractedMinus = ''] = cleanValueWithPossibleMinus.match(minusReg) || []; + + const cleanValue = cleanValueWithPossibleMinus.replace(minusReg, ''); + const [integerPart, decimalPart = ''] = cleanValue.split(decimalSeparator); const [initialFrom, initialTo] = selection; let [from, to] = selection; @@ -83,6 +91,7 @@ export function createThousandSeparatorPostprocessor({ return { value: extractedPrefix + + extractedMinus + processedIntegerPart + (cleanValue.includes(decimalSeparator) ? decimalSeparator : '') + decimalPart + diff --git a/projects/kit/src/lib/masks/number/processors/zero-precision-preprocessor.ts b/projects/kit/src/lib/masks/number/processors/zero-precision-preprocessor.ts index 11787ef5b..02ca110c0 100644 --- a/projects/kit/src/lib/masks/number/processors/zero-precision-preprocessor.ts +++ b/projects/kit/src/lib/masks/number/processors/zero-precision-preprocessor.ts @@ -1,15 +1,23 @@ import {MaskitoPreprocessor} from '@maskito/core'; import {escapeRegExp, identity} from '../../../utils'; +import {extractAffixes} from '../utils/extract-affixes'; /** * It drops decimal part if precision is zero. * @example User pastes '123.45' (but precision is zero) => 123 */ -export function createZeroPrecisionPreprocessor( - precision: number, - decimalSeparator: string, -): MaskitoPreprocessor { +export function createZeroPrecisionPreprocessor({ + precision, + decimalSeparator, + prefix, + postfix, +}: { + precision: number; + decimalSeparator: string; + prefix: string; + postfix: string; +}): MaskitoPreprocessor { if (precision > 0) { return identity; } @@ -18,8 +26,15 @@ export function createZeroPrecisionPreprocessor( return ({elementState, data}) => { const {value, selection} = elementState; + const {cleanValue, extractedPrefix, extractedPostfix} = extractAffixes(value, { + prefix, + postfix, + }); const [from, to] = selection; - const newValue = value.replace(decimalPartRegExp, ''); + const newValue = + extractedPrefix + + cleanValue.replace(decimalPartRegExp, '') + + extractedPostfix; return { elementState: { 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 691e41a12..babf8ef68 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 @@ -84,39 +84,159 @@ describe('Number (maskitoTransform)', () => { }); }); - // TODO https://github.com/taiga-family/maskito/issues/703 - xdescribe('`postfix` contains point and space (` lbs.`)', () => { + describe('`postfix` contains point and space (` lbs.`)', () => { let options: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; - beforeEach(() => { - options = maskitoNumberOptionsGenerator({ - postfix: ' lbs.', - precision: 2, + describe('precision: 2', () => { + beforeEach(() => { + options = maskitoNumberOptionsGenerator({ + postfix: ' lbs.', + precision: 2, + }); }); - }); - it('Empty textfield => empty textfield', () => { - expect(maskitoTransform('', options)).toBe(''); - }); + it('Empty textfield => empty textfield', () => { + expect(maskitoTransform('', options)).toBe(''); + }); - it('Only postfix => Only postfix', () => { - expect(maskitoTransform(' lbs.', options)).toBe(' lbs.'); - }); + it('Only postfix => Only postfix', () => { + expect(maskitoTransform(' lbs.', options)).toBe(' lbs.'); + }); - it('5 => 5 lbs.', () => { - expect(maskitoTransform('5', options)).toBe('5 lbs.'); + it('5 => 5 lbs.', () => { + expect(maskitoTransform('5', options)).toBe('5 lbs.'); + }); + + it('0.42 => 0.42 lbs.', () => { + expect(maskitoTransform('0.42', options)).toBe('0.42 lbs.'); + }); + + it('1 000 => 1 000 lbs.', () => { + expect(maskitoTransform('1 000', options)).toBe('1 000 lbs.'); + }); + + it('1 000. => 1 000. lbs.', () => { + expect(maskitoTransform('1 000.', options)).toBe('1 000. lbs.'); + }); + + it('1 000 lbs. => 1 000 lbs.', () => { + expect(maskitoTransform('1 000 lbs.', options)).toBe('1 000 lbs.'); + }); }); - it('0.42 => 5 lbs.', () => { - expect(maskitoTransform('0.42', options)).toBe('0.42 lbs.'); + describe('precision: 0', () => { + beforeEach(() => { + options = maskitoNumberOptionsGenerator({ + postfix: ' lbs.', + precision: 0, + }); + }); + + it('Empty textfield => empty textfield', () => { + expect(maskitoTransform('', options)).toBe(''); + }); + + it('Only postfix => Only postfix', () => { + expect(maskitoTransform(' lbs.', options)).toBe(' lbs.'); + }); + + it('5 => 5 lbs.', () => { + expect(maskitoTransform('5', options)).toBe('5 lbs.'); + }); + + it('0.42 => 0 lbs.', () => { + expect(maskitoTransform('0.42', options)).toBe('0 lbs.'); + }); + + it('1 000 => 1 000 lbs.', () => { + expect(maskitoTransform('1 000', options)).toBe('1 000 lbs.'); + }); + + it('1 000. => 1 000 lbs.', () => { + expect(maskitoTransform('1 000.', options)).toBe('1 000 lbs.'); + }); + + it('1 000 lbs. => 1 000 lbs.', () => { + expect(maskitoTransform('1 000 lbs.', options)).toBe('1 000 lbs.'); + }); }); + }); - it('1 000 => 1 000 lbs.', () => { - expect(maskitoTransform('1 000', options)).toBe('1 000 lbs.'); + describe('`prefix` contains point and space (`lbs. `)', () => { + let options: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; + + describe('precision: 2', () => { + beforeEach(() => { + options = maskitoNumberOptionsGenerator({ + prefix: 'lbs. ', + precision: 2, + }); + }); + + it('Empty textfield => empty textfield', () => { + expect(maskitoTransform('', options)).toBe(''); + }); + + it('Only prefix => Only prefix', () => { + expect(maskitoTransform('lbs. ', options)).toBe('lbs. '); + }); + + it('5 => lbs. 5', () => { + expect(maskitoTransform('5', options)).toBe('lbs. 5'); + }); + + it('0.42 => lbs. 0.42', () => { + expect(maskitoTransform('0.42', options)).toBe('lbs. 0.42'); + }); + + it('1 000 => lbs. 1 000', () => { + expect(maskitoTransform('1 000', options)).toBe('lbs. 1 000'); + }); + + it('1 000. => lbs. 1 000', () => { + expect(maskitoTransform('1 000.', options)).toBe('lbs. 1 000.'); + }); + + it('lbs. 1 000 => lbs. 1 000', () => { + expect(maskitoTransform('lbs. 1 000', options)).toBe('lbs. 1 000'); + }); }); - it('1 000 lbs. => 1 000 lbs.', () => { - expect(maskitoTransform('1 000 lbs.', options)).toBe('1 000 lbs.'); + describe('precision: 0', () => { + beforeEach(() => { + options = maskitoNumberOptionsGenerator({ + prefix: 'lbs. ', + precision: 0, + }); + }); + + it('Empty textfield => empty textfield', () => { + expect(maskitoTransform('', options)).toBe(''); + }); + + it('Only prefix => Only prefix', () => { + expect(maskitoTransform('lbs. ', options)).toBe('lbs. '); + }); + + it('5 => lbs. 5', () => { + expect(maskitoTransform('5', options)).toBe('lbs. 5'); + }); + + it('0.42 => lbs. 0', () => { + expect(maskitoTransform('0.42', options)).toBe('lbs. 0'); + }); + + it('1 000 => lbs. 1 000', () => { + expect(maskitoTransform('1 000', options)).toBe('lbs. 1 000'); + }); + + it('1 000. => lbs. 1 000', () => { + expect(maskitoTransform('1 000.', options)).toBe('lbs. 1 000'); + }); + + it('lbs. 1 000 => lbs. 1 000', () => { + expect(maskitoTransform('lbs. 1 000', options)).toBe('lbs. 1 000'); + }); }); }); }); diff --git a/projects/kit/src/lib/masks/number/utils/extract-prefix-and-postfix.ts b/projects/kit/src/lib/masks/number/utils/extract-affixes.ts similarity index 54% rename from projects/kit/src/lib/masks/number/utils/extract-prefix-and-postfix.ts rename to projects/kit/src/lib/masks/number/utils/extract-affixes.ts index 871f0f5e3..ca22b11c4 100644 --- a/projects/kit/src/lib/masks/number/utils/extract-prefix-and-postfix.ts +++ b/projects/kit/src/lib/masks/number/utils/extract-affixes.ts @@ -1,23 +1,14 @@ -import {CHAR_MINUS} from '../../../constants'; import {escapeRegExp} from '../../../utils'; -export function extractPrefixAndPostfix({ - value, - prefix, - postfix, - shouldExtractMinus = false, -}: { - value: string; - prefix: string; - postfix: string; - shouldExtractMinus?: boolean; -}): { +export function extractAffixes( + value: string, + {prefix, postfix}: {prefix: string; postfix: string}, +): { extractedPrefix: string; extractedPostfix: string; cleanValue: string; } { - const minusPart = shouldExtractMinus ? `${CHAR_MINUS}?` : ''; - const prefixRegExp = new RegExp(`^${escapeRegExp(prefix)}${minusPart}`); + const prefixRegExp = new RegExp(`^${escapeRegExp(prefix)}`); const postfixRegExp = new RegExp(`${escapeRegExp(postfix)}$`); const [extractedPrefix = ''] = value.match(prefixRegExp) ?? []; diff --git a/projects/kit/src/lib/masks/number/utils/parse-number.ts b/projects/kit/src/lib/masks/number/utils/parse-number.ts index 0a2ddb52a..c6a4feb08 100644 --- a/projects/kit/src/lib/masks/number/utils/parse-number.ts +++ b/projects/kit/src/lib/masks/number/utils/parse-number.ts @@ -3,7 +3,7 @@ import {escapeRegExp} from '../../../utils'; export function maskitoParseNumber( maskedNumber: string, - decimalSeparator: string = '.' + decimalSeparator: string = '.', ): number { const hasNegativeSign = !!maskedNumber.match( new RegExp(`^\\D*[${CHAR_MINUS}\\${CHAR_HYPHEN}${CHAR_EN_DASH}${CHAR_EM_DASH}]`),