Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new format money with commas to presenters #485

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/presenters/base.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ function formatNumberAsMoney (value, includeSymbol = false) {
return `${symbol}${value.toFixed(2)}`
}

/**
* Formats a number which represents a value in pounds as a money string with commas, for example, 2889 as '2,889.00'
*
* > Credit to https://stackoverflow.com/a/32154217/6117745
*
* @param {Number} value The value to display as currency. Assumed to be in pounds
* @param {Boolean} includeSymbol Whether to add the £ symbol to the start of the returned string
*
* @returns {string} The value formatted as a money string with commas with optional currency symbol
*/
function formatNumberAsMoneyWithCommas (value, includeSymbol = false) {
const symbol = includeSymbol ? '£' : ''

return `${symbol}${value.toLocaleString('en-GB', { minimumFractionDigits: 2 })}`
}

/**
* Pads a number to a given length with leading zeroes and returns the result as a string
*
Expand All @@ -152,5 +168,6 @@ module.exports = {
formatLongDate,
formatLongDateTime,
formatNumberAsMoney,
formatNumberAsMoneyWithCommas,
leftPadZeroes
}
20 changes: 20 additions & 0 deletions test/presenters/base.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ describe('Base presenter', () => {
})
})

describe('#formatNumberAsMoneyWithCommas()', () => {
const valueInPence = 1149.5
Cruikshanks marked this conversation as resolved.
Show resolved Hide resolved

describe('when no £ symbol is requested', () => {
it('correctly returns the value as a money string with commas and no symbol, for example, 1,149.50', async () => {
const result = BasePresenter.formatNumberAsMoneyWithCommas(valueInPence)

expect(result).to.equal('1,149.50')
})
})

describe('when the £ symbol is requested', () => {
it('correctly returns the value as a money string with commas and a symbol, for example, £1,149.50', async () => {
const result = BasePresenter.formatNumberAsMoneyWithCommas(valueInPence, true)

expect(result).to.equal('£1,149.50')
})
})
})

describe('#leftPadZeroes()', () => {
it('correctly pads numbers', async () => {
const number = 123
Expand Down
Loading