diff --git a/package-lock.json b/package-lock.json index 63d74a88..d361ba85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2265,6 +2265,15 @@ "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==", "dev": true }, + "@types/moment": { + "version": "2.13.0", + "resolved": "https://registry.npmjs.org/@types/moment/-/moment-2.13.0.tgz", + "integrity": "sha1-YE69GJvDvDShVIaJQE5hoqSqyJY=", + "dev": true, + "requires": { + "moment": "*" + } + }, "@types/node": { "version": "12.7.11", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.11.tgz", diff --git a/package.json b/package.json index 99762489..2859341e 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@svgr/webpack": "^4.3.3", "@types/enzyme": "^3.10.5", "@types/jest": "^26.0.10", + "@types/moment": "^2.13.0", "@types/ramda": "^0.27.14", "@types/react": "^16.9.46", "@types/react-dom": "^16.9.8", diff --git a/src/utils/helpers/date.js b/src/utils/helpers/date.js deleted file mode 100644 index db4f5fda..00000000 --- a/src/utils/helpers/date.js +++ /dev/null @@ -1,3 +0,0 @@ -export const formatDate = (format = 'YYYY-MM-DD') => (date) => date && date.format ? date.format(format) : date; - -export const formatIsoDate = (date) => date && date.format ? date.format() : date; diff --git a/src/utils/helpers/date.ts b/src/utils/helpers/date.ts new file mode 100644 index 00000000..f8da9a77 --- /dev/null +++ b/src/utils/helpers/date.ts @@ -0,0 +1,13 @@ +import * as moment from 'moment'; + +type MomentOrString = moment.Moment | string; +type NullableDate = MomentOrString | null; + +const isMomentObject = (date: moment.Moment | string): date is moment.Moment => typeof (date as moment.Moment).format === 'function'; + +const formatDateFromFormat = (date?: NullableDate, format?: string): NullableDate | undefined => + !date || !isMomentObject(date) ? date : date.format(format); + +export const formatDate = (format = 'YYYY-MM-DD') => (date?: NullableDate) => formatDateFromFormat(date, format); + +export const formatIsoDate = (date: NullableDate) => formatDateFromFormat(date, undefined); diff --git a/test/utils/helpers/date.test.js b/test/utils/helpers/date.test.js new file mode 100644 index 00000000..950de3ef --- /dev/null +++ b/test/utils/helpers/date.test.js @@ -0,0 +1,30 @@ +import moment from 'moment'; +import { formatDate, formatIsoDate } from '../../../src/utils/helpers/date'; + +describe('date', () => { + describe('formatDate', () => { + it.each([ + [ moment('2020-03-05 10:00:10'), 'DD/MM/YYYY', '05/03/2020' ], + [ moment('2020-03-05 10:00:10'), 'YYYY-MM', '2020-03' ], + [ moment('2020-03-05 10:00:10'), undefined, '2020-03-05' ], + [ '2020-03-05 10:00:10', 'DD-MM-YYYY', '2020-03-05 10:00:10' ], + [ '2020-03-05 10:00:10', undefined, '2020-03-05 10:00:10' ], + [ undefined, undefined, undefined ], + [ null, undefined, null ], + ])('formats date as expected', (date, format, expected) => { + expect(formatDate(format)(date)).toEqual(expected); + }); + }); + + describe('formatIsoDate', () => { + it.each([ + [ moment('2020-03-05 10:00:10'), moment('2020-03-05 10:00:10').format() ], + [ '2020-03-05 10:00:10', '2020-03-05 10:00:10' ], + [ 'foo', 'foo' ], + [ undefined, undefined ], + [ null, null ], + ])('formats date as expected', (date, expected) => { + expect(formatIsoDate(date)).toEqual(expected); + }); + }); +});