Skip to content

Commit

Permalink
exclude repeated date format and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m0nggh committed Dec 24, 2024
1 parent 5c7e9bc commit 46b6603
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('common date-time formatter functions', () => {
)

it('supports parsing MyInfo Child date field', () => {
const dateTime = parseDateTime('formsgDateField', '25/03/2024')
const dateTime = parseDateTime('dd/LL/yyyy', '25/03/2024')
expect(dateTime.toUnixInteger()).toEqual(1711296000)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,54 @@ describe('convert date time', () => {
toFormat: 'dd/LL/yy',
expectedResult: '01/04/24',
},
// TODO: add tests
{
inputFormat: 'dd/LL/yy',
inputValue: '01/04/24',
toFormat: 'dd/LL/yyyy',
expectedResult: '01/04/2024',
},
{
inputFormat: 'dd/LL/yyyy',
inputValue: '01/04/2024',
toFormat: 'dd LLLL yyyy',
expectedResult: '01 April 2024',
},
{
inputFormat: 'dd LLLL yyyy',
inputValue: '01 April 2024',
toFormat: 'yyyy/LL/dd',
expectedResult: '2024/04/01',
},
{
inputFormat: 'yyyy/LL/dd',
inputValue: '2024/04/01',
toFormat: 'hh:mm a',
expectedResult: '12:00 am',
},
{
inputFormat: 'hh:mm a',
inputValue: '11:45 pm',
toFormat: 'hh:mm:ss a',
expectedResult: '11:45:00 pm',
},
{
inputFormat: 'hh:mm:ss a',
inputValue: '11:45:00 pm',
toFormat: 'hh:mm a',
expectedResult: '11:45 pm',
},
{
inputFormat: 'dd LLL yyyy hh:mm a',
inputValue: '01 Apr 2024 11:45 pm',
toFormat: 'dd LLL yyyy',
expectedResult: '01 Apr 2024',
},
{
inputFormat: 'dd LLL yyyy hh:mm:ss a',
inputValue: '01 Apr 2024 11:45:30 pm',
toFormat: 'hh:mm:ss a',
expectedResult: '11:45:30 pm',
},
])('can handle all supported input formats', (testParams) => {
const { inputFormat, inputValue, toFormat, expectedResult } = testParams
$.step.parameters = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ const formatConverters = Object.assign({
stringify: (dateTime: DateTime): string => dateTime.toFormat('dd MMM yyyy'),
},
...Object.fromEntries(
commonDateFormats.map((format) => [
format,
{
description: format,
parse: (input: string): DateTime => DateTime.fromFormat(input, format),
stringify: (dateTime: DateTime): string => dateTime.toFormat(format),
},
]),
commonDateFormats
.filter((format) => format !== 'dd LLL yyyy') // Exclude repeated option due to formsgDateField
.map((format) => [
format,
{
description: format,
parse: (input: string): DateTime =>
DateTime.fromFormat(input, format),
stringify: (dateTime: DateTime): string => dateTime.toFormat(format),
},
]),
),
}) satisfies Record<z.infer<typeof supportedFormats>, DateFormatConverter>

Expand Down Expand Up @@ -104,7 +107,10 @@ export const field = {
description: 'Select this if you are transforming a FormSG date field',
value: supportedFormats.enum.formsgDateField,
},
...commonDateFormatOptions,
// Exclude repeated option due to formsgDateField
...commonDateFormatOptions.filter(
(option) => option.value !== 'dd LLL yyyy',
),
],
} satisfies IField

Expand Down

0 comments on commit 46b6603

Please sign in to comment.