Skip to content

Commit

Permalink
fix: allow extraction of overlapping affixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Belokopytov committed Nov 25, 2024
1 parent 19d1434 commit 9a18b5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,4 @@ describe('maskitoPostfixPostprocessorGenerator', () => {
).toEqual({value: '100.00', selection: [4, 4]});
});
});

describe('postfix starts with the same character as other part of the value ends', () => {
it('$_100_per_kg => $_|100_|per_kg (select all digits and underscore) => Delete => $_|_per_kg', () => {
const postprocessor = maskitoPostfixPostprocessorGenerator('_per_kg');

expect(
postprocessor(
{value: '$_per_kg', selection: [2, 2]}, // after
{value: '$_100_per_kg', selection: ['$_'.length, '$_100_'.length]}, // initial
),
).toEqual({value: '$__per_kg', selection: [2, 2]});
});

it('$__100__per_kg => $__|100__|per_kg (select all digits and 2 underscore) => Delete => $__|__per_kg', () => {
const postprocessor = maskitoPostfixPostprocessorGenerator('__per_kg');

expect(
postprocessor(
{value: '$__per_kg', selection: [3, 3]}, // after
{
value: '$__100__per_kg',
selection: ['$__'.length, '$__100__'.length],
}, // initial
),
).toEqual({value: '$____per_kg', selection: [3, 3]});
});
});
});
13 changes: 11 additions & 2 deletions projects/kit/src/lib/utils/extract-affixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export function extractAffixes(
const [extractedPrefix = ''] = value.match(prefixRegExp) ?? [];
const [extractedPostfix = ''] = value.match(postfixRegExp) ?? [];

const cleanValue = value.replace(prefixRegExp, '').replace(postfixRegExp, '');
if (extractedPrefix || extractedPostfix) {
return {
extractedPrefix,
extractedPostfix,
cleanValue: value.slice(
extractedPrefix.length,
extractedPostfix.length ? -extractedPostfix.length : Infinity,
),
};
}

return {extractedPrefix, extractedPostfix, cleanValue};
return {extractedPrefix, extractedPostfix, cleanValue: value};
}

0 comments on commit 9a18b5d

Please sign in to comment.