From 9dfa988a6b79a04247a061875490a7db1ac2b654 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Wed, 10 Jan 2024 09:41:13 +0000 Subject: [PATCH] Clean up numbers with extra decimal points --- app/webpack/javascripts/modules/determination.mjs | 5 ++++- .../javascripts/modules/determination_spec.mjs | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/webpack/javascripts/modules/determination.mjs b/app/webpack/javascripts/modules/determination.mjs index 85fb761134..0d66f2f2e4 100644 --- a/app/webpack/javascripts/modules/determination.mjs +++ b/app/webpack/javascripts/modules/determination.mjs @@ -94,6 +94,9 @@ export class Determination { } cleanNumber = (element) => { - element.value = element.value.replaceAll(/[^\d.]/g, '').replace(/^(\d*\.\d\d).*$/, '$1') + const split = element.value.split(/\.(.*)/).map((part) => part?.replaceAll(/[^\d]/g, '')) + + element.value = split[0] + if (split[1]) { element.value += `.${split[1].substring(0, 2)}` } } } diff --git a/app/webpack/javascripts/modules/determination_spec.mjs b/app/webpack/javascripts/modules/determination_spec.mjs index f9baaf1d10..019e18bcb8 100644 --- a/app/webpack/javascripts/modules/determination_spec.mjs +++ b/app/webpack/javascripts/modules/determination_spec.mjs @@ -203,6 +203,13 @@ describe('Determination', () => { return expect(element.value).toEqual('10') } + const itEnsuresASingleDecimalPoint = (element) => { + element.value = '10.1.2' + determination.cleanNumber(element) + + return expect(element.value).toEqual('10.12') + } + beforeEach(() => { document.body.classList.add('govuk-frontend-supported') @@ -238,6 +245,7 @@ describe('Determination', () => { itRemovesCommas(element) itRemovesExtraDigits(element) itRemovesLetters(element) + itEnsuresASingleDecimalPoint(element) }) it('expenses field', () => { @@ -247,6 +255,7 @@ describe('Determination', () => { itRemovesCommas(element) itRemovesExtraDigits(element) itRemovesLetters(element) + itEnsuresASingleDecimalPoint(element) }) it('disbursements field', () => { @@ -256,6 +265,7 @@ describe('Determination', () => { itRemovesCommas(element) itRemovesExtraDigits(element) itRemovesLetters(element) + itEnsuresASingleDecimalPoint(element) }) it('vat_amount field', () => { @@ -265,6 +275,7 @@ describe('Determination', () => { itRemovesCommas(element) itRemovesExtraDigits(element) itRemovesLetters(element) + itEnsuresASingleDecimalPoint(element) }) }) })