@defra/lis-infra-ui-services 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -3,8 +3,8 @@ import { intervalToDuration, isDate, isValid, parseISO } from 'date-fns'
|
|
|
3
3
|
/**
|
|
4
4
|
* @param {Date | string} dateOfBirth
|
|
5
5
|
* @param {Date} [now]
|
|
6
|
-
* @returns {string} e.g. "3 years, 6 months", "3 years
|
|
7
|
-
*
|
|
6
|
+
* @returns {string} e.g. "3 years, 6 months", "3 years, 0 months" or
|
|
7
|
+
* "0 years, 6 months", or "Less than 1 month" under a month. Empty when there
|
|
8
8
|
* is no usable date of birth - missing, unparseable or in the future - so
|
|
9
9
|
* a page never shows a made-up age.
|
|
10
10
|
*/
|
|
@@ -26,12 +26,8 @@ export function formatAge(dateOfBirth, now = new Date()) {
|
|
|
26
26
|
const yearsText = `${years} year${years === 1 ? '' : 's'}`
|
|
27
27
|
const monthsText = `${months} month${months === 1 ? '' : 's'}`
|
|
28
28
|
|
|
29
|
-
if (years === 0) {
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (months === 0) {
|
|
34
|
-
return yearsText
|
|
29
|
+
if (years === 0 && months === 0) {
|
|
30
|
+
return 'Less than 1 month'
|
|
35
31
|
}
|
|
36
32
|
|
|
37
33
|
return `${yearsText}, ${monthsText}`
|
|
@@ -27,7 +27,7 @@ describe('formatAge()', () => {
|
|
|
27
27
|
expect(age).toBe('1 year, 1 month')
|
|
28
28
|
})
|
|
29
29
|
|
|
30
|
-
test('it
|
|
30
|
+
test('it shows 0 months when the age is a whole number of years', () => {
|
|
31
31
|
// Arrange
|
|
32
32
|
const dateOfBirth = '2023-08-19'
|
|
33
33
|
const now = new Date('2026-09-11')
|
|
@@ -36,10 +36,10 @@ describe('formatAge()', () => {
|
|
|
36
36
|
const age = formatAge(dateOfBirth, now)
|
|
37
37
|
|
|
38
38
|
// Assert
|
|
39
|
-
expect(age).toBe('3 years')
|
|
39
|
+
expect(age).toBe('3 years, 0 months')
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
-
test('it uses the singular "year" for
|
|
42
|
+
test('it uses the singular "year" for exactly 1 year and 0 months', () => {
|
|
43
43
|
// Arrange
|
|
44
44
|
const dateOfBirth = '2025-08-19'
|
|
45
45
|
const now = new Date('2026-09-11')
|
|
@@ -48,10 +48,10 @@ describe('formatAge()', () => {
|
|
|
48
48
|
const age = formatAge(dateOfBirth, now)
|
|
49
49
|
|
|
50
50
|
// Assert
|
|
51
|
-
expect(age).toBe('1 year')
|
|
51
|
+
expect(age).toBe('1 year, 0 months')
|
|
52
52
|
})
|
|
53
53
|
|
|
54
|
-
test('it shows
|
|
54
|
+
test('it shows 0 years for an animal under a year old', () => {
|
|
55
55
|
// Arrange
|
|
56
56
|
const dateOfBirth = '2026-03-10'
|
|
57
57
|
const now = new Date('2026-09-10')
|
|
@@ -60,7 +60,7 @@ describe('formatAge()', () => {
|
|
|
60
60
|
const age = formatAge(dateOfBirth, now)
|
|
61
61
|
|
|
62
62
|
// Assert
|
|
63
|
-
expect(age).toBe('6 months')
|
|
63
|
+
expect(age).toBe('0 years, 6 months')
|
|
64
64
|
})
|
|
65
65
|
|
|
66
66
|
test('it uses the singular "month" for an animal exactly 1 month old', () => {
|
|
@@ -72,7 +72,7 @@ describe('formatAge()', () => {
|
|
|
72
72
|
const age = formatAge(dateOfBirth, now)
|
|
73
73
|
|
|
74
74
|
// Assert
|
|
75
|
-
expect(age).toBe('1 month')
|
|
75
|
+
expect(age).toBe('0 years, 1 month')
|
|
76
76
|
})
|
|
77
77
|
|
|
78
78
|
test('it shows "Less than 1 month" for an animal under a month old', () => {
|