Skip to content

Commit

Permalink
feat(kit): unskipped tests and fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aktanoff committed Jan 17, 2024
1 parent 60bb758 commit 545c5c9
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions projects/kit/src/lib/masks/number/number-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
createThousandSeparatorPostprocessor,
createZeroPrecisionPreprocessor,
} from './processors';
import {createAffixesFilterPreprocessor} from './processors/affixes-filter-preprocessor';
import {generateMaskExpression, validateDecimalPseudoSeparators} from './utils';

export function maskitoNumberOptionsGenerator({
Expand Down Expand Up @@ -79,7 +80,10 @@ export function maskitoNumberOptionsGenerator({
decimalSeparator,
decimalPseudoSeparators: validatedDecimalPseudoSeparators,
pseudoMinuses,
prefix,
postfix,
}),
createAffixesFilterPreprocessor({prefix, postfix}),
createFullWidthToHalfWidthPreprocessor(),
createPseudoCharactersPreprocessor({
validCharacter: CHAR_MINUS,
Expand All @@ -99,15 +103,20 @@ export function maskitoNumberOptionsGenerator({
decimalZeroPadding,
thousandSeparator,
}),
createZeroPrecisionPreprocessor(precision, decimalSeparator),
createZeroPrecisionPreprocessor({
precision,
decimalSeparator,
prefix,
postfix,
}),
createRepeatedDecimalSeparatorPreprocessor({
decimalSeparator,
prefix,
postfix,
}),
],
postprocessors: [
createMinMaxPostprocessor({decimalSeparator, min, max, prefix, postfix}),
createMinMaxPostprocessor({decimalSeparator, min, max}),
maskitoPrefixPostprocessorGenerator(prefix),
maskitoPostfixPostprocessorGenerator(postfix),
createThousandSeparatorPostprocessor({
Expand Down Expand Up @@ -136,7 +145,7 @@ export function maskitoNumberOptionsGenerator({
prefix,
postfix,
}),
createMinMaxPlugin({min, max, decimalSeparator, prefix, postfix}),
createMinMaxPlugin({min, max, decimalSeparator}),
],
overwriteMode: decimalZeroPadding
? ({value, selection: [from]}) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 +
Expand Down
11 changes: 1 addition & 10 deletions projects/kit/src/lib/masks/number/plugins/min-max.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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({
Expand All @@ -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,
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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)],
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down
Loading

0 comments on commit 545c5c9

Please sign in to comment.