Skip to content

Commit

Permalink
fix(kit): Number fails to trim leading zeroes after deleting of lea…
Browse files Browse the repository at this point in the history
…ding digit (#268)
  • Loading branch information
nsbarsukov authored Apr 25, 2023
1 parent 100b793 commit 4ae0010
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ describe('Number | Zero integer part', () => {
.should('have.prop', 'selectionEnd', '0'.length);
});

// TODO: BUG!
// https://github.com/Tinkoff/maskito/issues/266
it.skip('1|-000-000 => Backspace => 0', () => {
it('1|-000-000 => Backspace => 0', () => {
openNumberPage('thousandSeparator=_&precision=2');

cy.get('@input')
Expand Down
5 changes: 4 additions & 1 deletion projects/kit/src/lib/masks/number/number-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export function maskitoNumberOptionsGenerator({
createZeroPrecisionPreprocessor(precision, decimalSeparator),
),
postprocessor: maskitoPipe(
createLeadingZeroesValidationPostprocessor(decimalSeparator),
createLeadingZeroesValidationPostprocessor(
decimalSeparator,
thousandSeparator,
),
createMaxValidationPostprocessor({decimalSeparator, max}),
maskitoPrefixPostprocessorGenerator(prefix),
maskitoPostfixPostprocessorGenerator(postfix),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {MaskitoOptions} from '@maskito/core';

import {escapeRegExp} from '../../../utils';

/**
* It removes repeated leading zeroes for integer part.
* @example 0,|00005 => Backspace => |5
Expand All @@ -9,7 +11,35 @@ import {MaskitoOptions} from '@maskito/core';
*/
export function createLeadingZeroesValidationPostprocessor(
decimalSeparator: string,
thousandSeparator: string,
): NonNullable<MaskitoOptions['postprocessor']> {
const trimLeadingZeroes = (value: string): string => {
const escapedThousandSeparator = escapeRegExp(thousandSeparator);

return value
.replace(
// all leading zeroes followed by another zero
new RegExp(`^(\\D+)?[0${escapedThousandSeparator}]+(?=0)`),
'$1',
)
.replace(
// zero followed by not-zero digit
new RegExp(`^(\\D+)?[0${escapedThousandSeparator}]+(?=[1-9])`),
'$1',
);
};

const countTrimmedZeroesBefore = (value: string, index: number): number => {
const valueBefore = value.slice(0, index);
const followedByZero = value.slice(index).startsWith('0');

return (
valueBefore.length -
trimLeadingZeroes(valueBefore).length +
(followedByZero ? 1 : 0)
);
};

return ({value, selection}) => {
const [from, to] = selection;
const hasDecimalSeparator = value.includes(decimalSeparator);
Expand All @@ -32,20 +62,3 @@ export function createLeadingZeroesValidationPostprocessor(
};
};
}

function trimLeadingZeroes(value: string): string {
return value
.replace(/^(\D+)?0+(?=0)/, '$1') // all leading zeroes followed by another zero
.replace(/^(\D+)?0+(?=[1-9])/, '$1'); // zero followed by not-zero digit
}

function countTrimmedZeroesBefore(value: string, index: number): number {
const valueBefore = value.slice(0, index);
const followedByZero = value.slice(index).startsWith('0');

return (
valueBefore.length -
trimLeadingZeroes(valueBefore).length +
(followedByZero ? 1 : 0)
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {createLeadingZeroesValidationPostprocessor} from '../leading-zeroes-validation-postprocessor';

describe('createLeadingZeroesValidationPostprocessor', () => {
const processor = createLeadingZeroesValidationPostprocessor(',');
const processor = createLeadingZeroesValidationPostprocessor(',', '');
const DYMMY_INITIAL_STATE = {value: '', selection: [0, 0]} as const;

const process = (
Expand Down

0 comments on commit 4ae0010

Please sign in to comment.