Add replaceValue metadata param (#330)

This commit is contained in:
Ildar Galeev 2024-08-21 23:10:35 +07:00 committed by GitHub
parent f09a49ae52
commit 806bcc7d5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 10 deletions

View File

@ -25,6 +25,7 @@ export type ServiceProviderMetadataField = {
formatter?: MetadataFieldFormatter; formatter?: MetadataFieldFormatter;
inputMode?: AttributeInputMode; inputMode?: AttributeInputMode;
replaceValuePattern?: string; replaceValuePattern?: string;
replaceValue?: string;
}; };
export type MetadataSelectSource = { export type MetadataSelectSource = {

View File

@ -0,0 +1,43 @@
import { applyReplacePattern } from './applyReplacePattern';
describe('applyReplacePattern', () => {
it('should replace "+" with a space', () => {
const result = applyReplacePattern('FirstName+LastName', '\\+', ' ');
expect(result).toBe('FirstName LastName');
});
it('should return the original string if pattern is not provided', () => {
const result = applyReplacePattern('FirstName+LastName');
expect(result).toBe('FirstName+LastName');
});
it('should return the original value if rawValue is not a string', () => {
const result = applyReplacePattern(12345, '\\+', ' ');
expect(result).toBe(12345);
});
it('should replace pattern with empty string when replaceValue is not provided', () => {
const result = applyReplacePattern('FirstName+LastName', '\\+');
expect(result).toBe('FirstNameLastName');
});
it('should replace pattern with empty string when replaceValue is undefined', () => {
const result = applyReplacePattern('FirstName+LastName', '\\+', undefined);
expect(result).toBe('FirstNameLastName');
});
it('should handle an empty string pattern', () => {
const result = applyReplacePattern('FirstName+LastName', '', ' ');
expect(result).toBe('FirstName+LastName');
});
it('should replace multiple occurrences of the pattern', () => {
const result = applyReplacePattern('FirstName+MiddleName+LastName', '\\+', ' ');
expect(result).toBe('FirstName MiddleName LastName');
});
it('should return the original string if pattern does not match anything', () => {
const result = applyReplacePattern('FirstName+LastName', '\\-', ' ');
expect(result).toBe('FirstName+LastName');
});
});

View File

@ -0,0 +1,9 @@
import { isNil, isString } from '../../utils';
export const applyReplacePattern = <T>(rawValue: T, pattern?: string, replaceValue?: string): string | T => {
if (!isNil(pattern) && isString(rawValue) && pattern !== '') {
const regExp = new RegExp(pattern, 'g');
return rawValue.replace(regExp, replaceValue ?? '');
}
return rawValue;
};

View File

@ -1,14 +1,7 @@
import { applyReplacePattern } from './applyReplacePattern';
import { ServiceProviderMetadataField, ServiceProviderMetadataForm } from '../../backend/payments'; import { ServiceProviderMetadataField, ServiceProviderMetadataForm } from '../../backend/payments';
import { TerminalValues } from '../../paymentMgmt'; import { TerminalValues } from '../../paymentMgmt';
import { createRegExpForMetaPattern, isNil, isString } from '../../utils'; import { createRegExpForMetaPattern, isNil } from '../../utils';
const applyReplacePattern = <T>(rawValue: T, pattern?: string, replaceValue = ''): string | T => {
if (!isNil(pattern) && isString(rawValue)) {
const regExp = new RegExp(pattern, 'g');
return rawValue.replace(regExp, replaceValue);
}
return rawValue;
};
export const prepareFormValues = ( export const prepareFormValues = (
form: ServiceProviderMetadataField[], form: ServiceProviderMetadataField[],
@ -19,7 +12,7 @@ export const prepareFormValues = (
if (isNil(value)) { if (isNil(value)) {
return acc; return acc;
} }
const appliedValue = applyReplacePattern(value, formField.replaceValuePattern); const appliedValue = applyReplacePattern(value, formField.replaceValuePattern, formField.replaceValue);
if (formField.pattern && !createRegExpForMetaPattern(formField.pattern).test(appliedValue)) { if (formField.pattern && !createRegExpForMetaPattern(formField.pattern).test(appliedValue)) {
return acc; return acc;
} }