fix(decimal_champ): only remove dots if coma also present

This commit is contained in:
Paul Chavard 2023-10-23 16:46:07 +02:00
parent 81ccc8ace2
commit d01cc76970
2 changed files with 35 additions and 18 deletions

View file

@ -52,12 +52,19 @@ export class FormatController extends ApplicationController {
} }
private formatDecimal(value: string) { private formatDecimal(value: string) {
const decimalSeparator = getDecimalSeparator(value); let formattedNumber = value;
const number = const lastDotIndex = value.lastIndexOf('.');
decimalSeparator == ',' const lastCommaIndex = value.lastIndexOf(',');
? value.replace(/\./g, '').replace(/,/g, '.') if (lastDotIndex != -1 && lastCommaIndex != -1) {
: value.replace(/,/g, ''); if (lastDotIndex < lastCommaIndex) {
return number.replace(new RegExp(`[^-?\\d.]`, 'g'), ''); formattedNumber = value.replace(/\./g, '');
} else {
formattedNumber = value.replace(/,/g, '');
}
}
return formattedNumber
.replace(/,/g, '.')
.replace(new RegExp(`[^-?\\d.]`, 'g'), '');
} }
} }
@ -71,15 +78,3 @@ function replaceValue(target: HTMLInputElement, value: string) {
target.selectionEnd = end ? end - delta : 0; target.selectionEnd = end ? end - delta : 0;
target.selectionDirection = dir; target.selectionDirection = dir;
} }
function getDecimalSeparator(value: string) {
if (value.indexOf('.') != -1 && value.indexOf(',') != -1) {
if (value.lastIndexOf('.') < value.lastIndexOf(',')) {
return ',';
}
return '.';
} else if (value.indexOf(',') != -1) {
return ',';
}
return (1.1).toLocaleString().indexOf('.') != -1 ? '.' : ',';
}

View file

@ -205,6 +205,28 @@ describe 'The user' do
wait_until { wait_until {
champ_value_for('nombre décimal') == '123456.78' champ_value_for('nombre décimal') == '123456.78'
} }
champ_past_value_for('nombre décimal', '123 456,78')
wait_until {
champ_value_for('nombre décimal') == '123456.78'
}
fill_in('nombre décimal', with: '123 456.78')
wait_until {
champ_value_for('nombre décimal') == '123456.78'
}
champ_past_value_for('nombre décimal', '123 456.78')
wait_until {
champ_value_for('nombre décimal') == '123456.78'
}
fill_in('nombre décimal', with: '123 456.002')
wait_until {
champ_value_for('nombre décimal') == '123456.002'
}
fill_in('nombre décimal', with: '123 456,002')
wait_until {
champ_value_for('nombre décimal') == '123456.002'
}
champ_past_value_for('nombre décimal', '1,234.56') champ_past_value_for('nombre décimal', '1,234.56')
wait_until { wait_until {