purescript-contrib/purescript-formatters

Update readme/docs to de-emphasise usage of string formats

Opened this issue · 5 comments

garyb commented

We and I'm sure others have a lot of code that looks like this sort of thing at the moment:

timeFormat = unsafePartial fromRight $ parseFormatString "HH:mm:ss"

Which is actually quite silly, when it's actually easier, safer, and generally better all round to just construct the format directly:

timeFormat = Hours24 : MinutesTwoDigits : SecondsTwoDigits : Nil
-- or
timeFormat' = L.fromFoldable [Hours24, MinutesTwoDigits, SecondsTwoDigits]

😆

As another advantage of that is Hours24 is better self-documentation than remembering "HH" vs "hh" for Hours24 vs Hours12 too, etc.

garyb commented

Not saying we shouldn't support the string formats - they do have their uses, just not for formatters that are statically defined in PureScript code.

garyb commented

@safareli pointed out that some formats are invalid (as they'd parse ambiguously)

We may do something like this: https://gist.github.com/garyb/1c4fda54c630e59e609c1f04058e5ac8 so we can validate formats at the type level.

I think we would need to split formats, like have separate DateFormat and TimeFormat and use them in DateTimeFormat:

newtype DateFormat = DateFormat (List DateCommand)
data DateCommand
  = YearFull
  | YearTwoDigits
  | YearAbsolute
  | Placeholder String
  ...

newtype TimeFormat = TimeFormat (List TimeCommand)
data TimeCommand
  = Hours24
  | Hours12
  | Meridiem
  | Placeholder String
  ...

data DateTimeCommand
  = Date DateFormat
  | Time TimeFormat
  | Placeholder String
newtype DateTimeFormat = DateTimeFormat (List DateTimeCommand)

Here are some constraints I had for DatePicker formats: (time, date, datetime)

p.s. looks like i have missed that if you use DayOfMonth there must be month component in format too

If we merge #26 then we would not be able to build datetime parser using date and time parsers because of "day shift".
but we can still make time and date modules and derive validDate and validTime formats using typeclass mechanisms.