Skip to content

Commit

Permalink
Add the period to string util
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-s-ccs committed Feb 22, 2022
1 parent 2752b27 commit 1f24fa4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/utils/periodToString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pluralise from './pluralise'

const periodToString = (years: number, months: number): string => {
let text = ''

if (years > 0) text += `${years} ${pluralise('year', years)}`
if (years > 0 && months > 0) text += ' and '
if (months > 0) text += `${months} ${pluralise('month', months)}`

return text
}

export default periodToString
2 changes: 2 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import cast from './cast'
import dateHelpers from './dateHelpers'
import formatDate from './formatDate'
import numberToCurrency from './numberToCurrency'
import periodToString from './periodToString'
import pluralise from './pluralise'

export default {
addLeadingZeros: addLeadingZeros,
cast: cast,
formatDate: formatDate,
dateHelpers: dateHelpers,
periodToString: periodToString,
pluralise: pluralise,
numberToCurrency: numberToCurrency
}
28 changes: 28 additions & 0 deletions test/utils/periodToString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import periodToString from '../../src/utils/periodToString'
import { expect } from 'chai'

describe('periodToString', () => {
it('returns \'3 years\' when the years are 3 and the months are 0', () => {
const result = periodToString(3, 0)

expect(result).to.equal('3 years')
})

it('returns \'3 months\' when the years are 0 and the months are 3', () => {
const result = periodToString(0, 3)

expect(result).to.equal('3 months')
})

it('returns \'1 year and 1 month\' when the years are 1 and the months are 1', () => {
const result = periodToString(1, 1)

expect(result).to.equal('1 year and 1 month')
})

it('returns \'3 years and 3 months\' when the years are 3 and the months are 3', () => {
const result = periodToString(3, 3)

expect(result).to.equal('3 years and 3 months')
})
})

0 comments on commit 1f24fa4

Please sign in to comment.