mirror of
https://github.com/valitydev/checkout.git
synced 2024-11-06 02:25:18 +00:00
Add replaceValue metadata param (#330)
This commit is contained in:
parent
f09a49ae52
commit
806bcc7d5d
@ -25,6 +25,7 @@ export type ServiceProviderMetadataField = {
|
||||
formatter?: MetadataFieldFormatter;
|
||||
inputMode?: AttributeInputMode;
|
||||
replaceValuePattern?: string;
|
||||
replaceValue?: string;
|
||||
};
|
||||
|
||||
export type MetadataSelectSource = {
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
9
src/common/paymentCondition/utils/applyReplacePattern.ts
Normal file
9
src/common/paymentCondition/utils/applyReplacePattern.ts
Normal 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;
|
||||
};
|
@ -1,14 +1,7 @@
|
||||
import { applyReplacePattern } from './applyReplacePattern';
|
||||
import { ServiceProviderMetadataField, ServiceProviderMetadataForm } from '../../backend/payments';
|
||||
import { TerminalValues } from '../../paymentMgmt';
|
||||
import { createRegExpForMetaPattern, isNil, isString } 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;
|
||||
};
|
||||
import { createRegExpForMetaPattern, isNil } from '../../utils';
|
||||
|
||||
export const prepareFormValues = (
|
||||
form: ServiceProviderMetadataField[],
|
||||
@ -19,7 +12,7 @@ export const prepareFormValues = (
|
||||
if (isNil(value)) {
|
||||
return acc;
|
||||
}
|
||||
const appliedValue = applyReplacePattern(value, formField.replaceValuePattern);
|
||||
const appliedValue = applyReplacePattern(value, formField.replaceValuePattern, formField.replaceValue);
|
||||
if (formField.pattern && !createRegExpForMetaPattern(formField.pattern).test(appliedValue)) {
|
||||
return acc;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user