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) {
const decimalSeparator = getDecimalSeparator(value);
const number =
decimalSeparator == ','
? value.replace(/\./g, '').replace(/,/g, '.')
: value.replace(/,/g, '');
return number.replace(new RegExp(`[^-?\\d.]`, 'g'), '');
let formattedNumber = value;
const lastDotIndex = value.lastIndexOf('.');
const lastCommaIndex = value.lastIndexOf(',');
if (lastDotIndex != -1 && lastCommaIndex != -1) {
if (lastDotIndex < lastCommaIndex) {
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.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 {
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')
wait_until {