[IMP] web: number parsers: fallback on english localization

With this commit, if the user types something in a numeric field
input that doesn't match its localization, we try to parse it with
the english localization. If it works, we considerer that value.
Otherwise, we throw an error.

al request

Part-of: odoo/odoo#92475
This commit is contained in:
Simon Genin (ges)
2022-06-30 09:29:45 +02:00
committed by Aaron Bohy
parent 3c782440ab
commit 53595b9326
3 changed files with 31 additions and 2 deletions
@@ -72,6 +72,15 @@ export function parseFloat(value) {
thousandsSep: thousandsSepRegex,
decimalPoint: decimalPointRegex,
});
if (isNaN(parsed)) {
parsed = parseNumber(value, {
thousandsSep: ",",
decimalPoint: ".",
});
if (isNaN(parsed)) {
throw new InvalidNumberError(`"${value}" is not a correct number`);
}
}
return parsed;
}
@@ -114,6 +123,16 @@ export function parseInteger(value) {
decimalPoint: decimalPointRegex,
truncate: true,
});
if (!Number.isInteger(parsed)) {
parsed = parseNumber(value, {
thousandsSep: ",",
decimalPoint: ".",
truncate: true,
});
if (!Number.isInteger(parsed)) {
throw new InvalidNumberError(`"${value}" is not a correct number`);
}
}
return parsed;
}
@@ -622,12 +622,12 @@ QUnit.module("Search", (hooks) => {
assert.strictEqual(floatInput.value, "4,2");
await editConditionValue(target, 0, "4,2,");
assert.strictEqual(floatInput.value, "4,2");
assert.strictEqual(floatInput.value, "42,0"); // because of the en localization fallback in parsers.
await editConditionValue(target, 0, "DefinitelyValidFloat");
// The input here is a string, resulting in a parsing error instead of 0
assert.strictEqual(floatInput.value, "4,2");
assert.strictEqual(floatInput.value, "42,0");
});
QUnit.test("add custom filter with multiple values", async function (assert) {
@@ -127,4 +127,14 @@ QUnit.module("Fields", (hooks) => {
assert.throws(() => parseMonetary("1$\u00a01", { currencyId: 1 }));
assert.throws(() => parseMonetary("$\u00a012.00\u00a034", { currencyId: 3 }));
});
QUnit.test("parsers fallback on english localisation", function (assert) {
patchWithCleanup(localization, {
decimalPoint: ",",
thousandsSep: ".",
});
assert.strictEqual(parseInteger("1,000,000"), 1000000);
assert.strictEqual(parseFloat("1,000,000.50"), 1000000.5);
});
});