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

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