I have the following function:
export const getFormatValue = (raw, value) => {
let maskIndex = 0;
let formattedValue = '';
raw.split('').forEach(char => {
if (value.length > maskIndex && value[maskIndex] === ' ') {
formattedValue += ' ';
maskIndex += 1;
}
formattedValue += char;
maskIndex += 1;
});
return formattedValue;
};
It fails the following test cases
Expected: "A BB CCC"
Received: "A B BC C C"
> 11 | expect(getFormatValue('ABBCCC', 'A AA AAA')).toBe('A BB CCC');
My code works for simple cases such as:
expect(getFormatValue('23', '0 0000 0000 0000')).toBe('2 3');
But soon as the "mask" pattern gets more complicated it fails.
The "mask" value is dynamic