buildo / metarpheus-io-ts

Generate domain models and client interpreting metarpheus output
MIT License
7 stars 1 forks source link

Add DateFromString or use DateFromISOString #86

Open ecamellini opened 5 years ago

ecamellini commented 5 years ago

requirements

At the moment, to convert Instant, LocalDateTime, etc into JS Dates, we need to manually map them to Date in the prelude in metarpheus config and to manually define and include also a DateFromString io-ts type: we could include all this in metarpehus:

specs

At the moment, this is what we add to the prelude in projects:

const dateFromString = `
export const DateFromString = new t.Type<Date, string>(
  'Date',
  (v): v is Date => v instanceof Date,
  (v, c) =>
    t.string.validate(v, c).chain(s => {
      const d = new Date(s)
      return isNaN(d.getTime()) ? t.failure(s, c) : t.success(d)
    }),
  a => a.toISOString()
)
`;

const modelPrelude = `
${dateFromString}
export type Instant = Date;
export const Instant = DateFromString;
export type LocalDateTime = Date;
export const LocalDateTime = DateFromString;
export type LocalDate = Date;
export const LocalDate = DateFromString;
export type LocalTime = Date;
export const LocalTime = DateFromString;

misc

{optional: other useful info}

gabro commented 5 years ago

DateFromISOString implementation looks identical, so I would just pull it in and use it.

https://github.com/gcanti/io-ts-types/blob/master/src/Date/DateFromISOString.ts

giogonzo commented 5 years ago

@ecamellini LocalDate and LocalDateTime should use https://github.com/buildo/local-date though