Skip to content

Commit

Permalink
Clean up numbers with extra decimal points
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Jan 10, 2024
1 parent 3ef1141 commit 9dfa988
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/webpack/javascripts/modules/determination.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)}` }
}
}
11 changes: 11 additions & 0 deletions app/webpack/javascripts/modules/determination_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -238,6 +245,7 @@ describe('Determination', () => {
itRemovesCommas(element)
itRemovesExtraDigits(element)
itRemovesLetters(element)
itEnsuresASingleDecimalPoint(element)
})

it('expenses field', () => {
Expand All @@ -247,6 +255,7 @@ describe('Determination', () => {
itRemovesCommas(element)
itRemovesExtraDigits(element)
itRemovesLetters(element)
itEnsuresASingleDecimalPoint(element)
})

it('disbursements field', () => {
Expand All @@ -256,6 +265,7 @@ describe('Determination', () => {
itRemovesCommas(element)
itRemovesExtraDigits(element)
itRemovesLetters(element)
itEnsuresASingleDecimalPoint(element)
})

it('vat_amount field', () => {
Expand All @@ -265,6 +275,7 @@ describe('Determination', () => {
itRemovesCommas(element)
itRemovesExtraDigits(element)
itRemovesLetters(element)
itEnsuresASingleDecimalPoint(element)
})
})
})

0 comments on commit 9dfa988

Please sign in to comment.