Skip to content

Commit

Permalink
feat(kit): moved extractAffixes and fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aktanoff committed Jan 17, 2024
1 parent 545c5c9 commit e54d368
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {MaskitoPlugin, maskitoUpdateElement} from '@maskito/core';

import {maskitoEventHandler} from '../../../plugins';
import {createLeadingZeroesValidationPostprocessor} from '../processors';
import {extractAffixes} from '../utils/extract-affixes';

const DUMMY_SELECTION = [0, 0] as const;

Expand Down Expand Up @@ -32,21 +31,13 @@ export function createLeadingZeroesValidationPlugin({
return maskitoEventHandler(
'blur',
element => {
const {cleanValue, extractedPostfix, extractedPrefix} = extractAffixes(
element.value,
{prefix, postfix},
);

const newValue =
extractedPrefix +
dropRepeatedLeadingZeroes(
{
value: cleanValue,
selection: DUMMY_SELECTION,
},
{value: '', selection: DUMMY_SELECTION},
).value +
extractedPostfix;
const newValue = dropRepeatedLeadingZeroes(
{
value: element.value,
selection: DUMMY_SELECTION,
},
{value: '', selection: DUMMY_SELECTION},
).value;

if (element.value !== newValue) {
maskitoUpdateElement(element, newValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {MaskitoPlugin, maskitoUpdateElement} from '@maskito/core';

import {maskitoEventHandler} from '../../../plugins';
import {escapeRegExp} from '../../../utils';
import {extractAffixes} from '../utils/extract-affixes';
import {escapeRegExp, extractAffixes} from '../../../utils';

/**
* It pads EMPTY integer part with zero if decimal parts exists.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MaskitoPreprocessor} from '@maskito/core';

import {extractAffixes} from '../utils/extract-affixes';
import {extractAffixes} from '../../../utils';

/**
* It drops prefix and postfix from data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {MaskitoPostprocessor} from '@maskito/core';

import {identity} from '../../../utils';
import {extractAffixes, identity} from '../../../utils';
import {maskitoParseNumber} from '../utils';
import {extractAffixes} from '../utils/extract-affixes';

/**
* If `decimalZeroPadding` is `true`, it pads decimal part with zeroes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {MaskitoPreprocessor, maskitoTransform} from '@maskito/core';

import {extractAffixes} from '../../../utils';
import {generateMaskExpression} from '../utils';
import {extractAffixes} from '../utils/extract-affixes';

/**
* This preprocessor works only once at initialization phase (when `new Maskito(...)` is executed).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {MaskitoPostprocessor} from '@maskito/core';

import {escapeRegExp} from '../../../utils';
import {extractAffixes} from '../utils/extract-affixes';
import {escapeRegExp, extractAffixes} from '../../../utils';

/**
* It removes repeated leading zeroes for integer part.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MaskitoPreprocessor} from '@maskito/core';

import {extractAffixes} from '../utils/extract-affixes';
import {extractAffixes} from '../../../utils';

/**
* It replaces pseudo characters with valid one.
Expand All @@ -22,21 +22,18 @@ export function createPseudoCharactersPreprocessor({

return ({elementState, data}) => {
const {value, selection} = elementState;

const {cleanValue, extractedPostfix, extractedPrefix} = extractAffixes(value, {
prefix,
postfix,
});

const newValue =
extractedPrefix +
cleanValue.replace(pseudoCharactersRegExp, validCharacter) +
extractedPostfix;

return {
elementState: {
selection,
value: newValue,
value:
extractedPrefix +
cleanValue.replace(pseudoCharactersRegExp, validCharacter) +
extractedPostfix,
},
data: data.replace(pseudoCharactersRegExp, validCharacter),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {MaskitoPreprocessor} from '@maskito/core';

import {escapeRegExp} from '../../../utils';
import {extractAffixes} from '../utils/extract-affixes';
import {escapeRegExp, extractAffixes} from '../../../utils';

/**
* It rejects new typed decimal separator if it already exists in text field.
Expand All @@ -19,7 +18,6 @@ export function createRepeatedDecimalSeparatorPreprocessor({
}): MaskitoPreprocessor {
return ({elementState, data}) => {
const {value, selection} = elementState;

const [from, to] = selection;

const {cleanValue} = extractAffixes(value, {prefix, postfix});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {MaskitoPostprocessor} from '@maskito/core';

import {CHAR_MINUS} from '../../../constants';
import {identity} from '../../../utils';
import {extractAffixes} from '../utils/extract-affixes';
import {extractAffixes, identity} from '../../../utils';

/**
* It adds symbol for separating thousands.
Expand All @@ -23,24 +22,17 @@ 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: cleanValueWithPossibleMinus,
extractedPostfix,
extractedPrefix,
} = extractAffixes(value, {
const {cleanValue, extractedPostfix, extractedPrefix} = extractAffixes(value, {
prefix,
postfix,
});

const [extractedMinus = ''] = cleanValueWithPossibleMinus.match(minusReg) || [];

const cleanValue = cleanValueWithPossibleMinus.replace(minusReg, '');

const [integerPart, decimalPart = ''] = cleanValue.split(decimalSeparator);
const [integerPart, decimalPart = ''] = cleanValue
.replace(CHAR_MINUS, '')
.split(decimalSeparator);
const [initialFrom, initialTo] = selection;
let [from, to] = selection;

Expand Down Expand Up @@ -91,7 +83,7 @@ export function createThousandSeparatorPostprocessor({
return {
value:
extractedPrefix +
extractedMinus +
(cleanValue.includes(CHAR_MINUS) ? CHAR_MINUS : '') +
processedIntegerPart +
(cleanValue.includes(decimalSeparator) ? decimalSeparator : '') +
decimalPart +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {MaskitoPreprocessor} from '@maskito/core';

import {escapeRegExp, identity} from '../../../utils';
import {extractAffixes} from '../utils/extract-affixes';
import {escapeRegExp, extractAffixes, identity} from '../../../utils';

/**
* It drops decimal part if precision is zero.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {escapeRegExp} from '../../../utils';
import {escapeRegExp} from '.';

export function extractAffixes(
value: string,
Expand Down
1 change: 1 addition & 0 deletions projects/kit/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './date/segments-to-date';
export * from './date/to-date-string';
export * from './date/validate-date-string';
export * from './escape-reg-exp';
export * from './extract-affixes';
export * from './find-common-beginning-substr';
export * from './get-focused';
export * from './identity';
Expand Down

0 comments on commit e54d368

Please sign in to comment.