diff --git a/app/presenters/base.presenter.js b/app/presenters/base.presenter.js index 68abe6d601..92e89edbe4 100644 --- a/app/presenters/base.presenter.js +++ b/app/presenters/base.presenter.js @@ -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 * @@ -123,6 +144,7 @@ function leftPadZeroes (number, length) { } module.exports = { + capitalize, convertPenceToPounds, formatAbstractionDate, formatAbstractionPeriod, diff --git a/test/presenters/base.presenter.test.js b/test/presenters/base.presenter.test.js index c9e53d51e5..a665d4b459 100644 --- a/test/presenters/base.presenter.test.js +++ b/test/presenters/base.presenter.test.js @@ -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