Migrated to Typescript a file which is imported in JS files

This commit is contained in:
Alejandro Celaya 2020-08-22 09:48:55 +02:00
parent 524b0a74c6
commit d65a6ba970
5 changed files with 53 additions and 3 deletions

9
package-lock.json generated
View file

@ -2265,6 +2265,15 @@
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==", "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==",
"dev": true "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": { "@types/node": {
"version": "12.7.11", "version": "12.7.11",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.11.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.11.tgz",

View file

@ -75,6 +75,7 @@
"@svgr/webpack": "^4.3.3", "@svgr/webpack": "^4.3.3",
"@types/enzyme": "^3.10.5", "@types/enzyme": "^3.10.5",
"@types/jest": "^26.0.10", "@types/jest": "^26.0.10",
"@types/moment": "^2.13.0",
"@types/ramda": "^0.27.14", "@types/ramda": "^0.27.14",
"@types/react": "^16.9.46", "@types/react": "^16.9.46",
"@types/react-dom": "^16.9.8", "@types/react-dom": "^16.9.8",

View file

@ -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;

13
src/utils/helpers/date.ts Normal file
View file

@ -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);

View file

@ -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);
});
});
});