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 capitalize() method to base presenter #451

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions app/presenters/base.presenter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
'use strict'

/**
* Capitalize the first letter of each word in a string
*
* Will work for strings containing multiple words or only one.
*
* @param {string} value The string to capitalize
*
* @returns {string} The capitalized string
*/
function capitalize (value) {
const words = value.split(' ')
const capitalizedWords = []

words.forEach((word) => {
const capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1)
capitalizedWords.push(capitalizedWord)
})

return capitalizedWords.join(' ')
}

/**
* Converts a number which represents pence into pounds by dividing it by 100
*
Expand Down Expand Up @@ -123,6 +144,7 @@ function leftPadZeroes (number, length) {
}

module.exports = {
capitalize,
convertPenceToPounds,
formatAbstractionDate,
formatAbstractionPeriod,
Expand Down
52 changes: 52 additions & 0 deletions test/presenters/base.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,58 @@ const { expect } = Code
const BasePresenter = require('../../app/presenters/base.presenter.js')

describe('Base presenter', () => {
describe('#capitalize()', () => {
let valueToCapitalize

describe('when the value is a single word', () => {
beforeEach(() => {
valueToCapitalize = 'high'
})

it('correctly returns the value capitalized, for example, High', async () => {
const result = BasePresenter.capitalize(valueToCapitalize)

expect(result).to.equal('High')
})
})

describe('when the value is multiple words', () => {
beforeEach(() => {
valueToCapitalize = 'spray irrigation'
})

it('correctly returns the value capitalized, for example, Spray Irrigation', async () => {
const result = BasePresenter.capitalize(valueToCapitalize)

expect(result).to.equal('Spray Irrigation')
})
})

describe('when the value contains a symbol', () => {
beforeEach(() => {
valueToCapitalize = 'spray irrigation - direct'
})

it('correctly returns the value capitalized, for example, Spray Irrigation - Direct', async () => {
const result = BasePresenter.capitalize(valueToCapitalize)

expect(result).to.equal('Spray Irrigation - Direct')
})
})

describe('when the value is all capitals', () => {
beforeEach(() => {
valueToCapitalize = 'SPRAY IRRIGATION'
})

it('correctly returns the value unchanged, for example, SPRAY IRRIGATION', async () => {
const result = BasePresenter.capitalize(valueToCapitalize)

expect(result).to.equal('SPRAY IRRIGATION')
})
})
})

describe('#convertPenceToPounds()', () => {
let valueInPence

Expand Down