Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1x 1x 1x 32x 20x 20x 8x 8x 8x 8x 8x 8x 12x 12x 12x 12x 12x 12x 12x 20x 1x 1x 1x 1x 1x 1x 20x | import { validationMessage } from '@/constants/validationMessages';
import type { ResultWithValue } from '@/contracts/result';
import type { ValidationResult } from '@/contracts/validationResult';
interface IBaseConverter<TI, TO> {
input: TI;
converter: (input: TI) => TO;
inputValidator: (input: TI) => ValidationResult;
}
export const baseConverter =
<TI, TO extends {}>(props: IBaseConverter<TI, TO>) =>
(): ResultWithValue<TO> => {
const validationResult = props.inputValidator(props.input);
if (validationResult.isValid === false) {
return {
isSuccess: false,
value: {} as TO,
errorMessage: validationResult.errorMessage ?? validationMessage.generic,
};
}
try {
const conversionResult = props.converter(props.input);
return {
isSuccess: true,
errorMessage: '',
value: conversionResult,
};
} catch (ex) {
return {
isSuccess: false,
value: {} as TO,
errorMessage: ex?.toString?.() ?? 'Unknown exception occurred during conversion',
};
}
};
|