Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parse safer #15

Closed
paulyoung opened this issue Aug 18, 2017 · 7 comments
Closed

Make parse safer #15

paulyoung opened this issue Aug 18, 2017 · 7 comments
Labels
good first issue First-time contributors who are looking to help should work on these issues. type: documentation Improvements or additions to documentation.

Comments

@paulyoung
Copy link

I was originally doing the following and easily ran into a runtime error because isoString was invalid:

jsDate <- parse isoString
let ms = getTime jsDate

I'm currently doing this instead:

parseJSDate
  :: forall eff
   . String
  -> Eff ( locale :: LOCALE | eff ) (Maybe JSDate)
parseJSDate date = do
  parsed <- parse date
  pure $ if isValid parsed then Just parsed else Nothing
jsDate <- parseJSDate isoString
let ms = getTime <$> jsDate

Is there any reason to not make parse do this by default?

@chexxor
Copy link

chexxor commented Oct 14, 2017

I agree with having more safe standard functions. 👍
Rename current one to unsafeParse, then introduce a safe parse.

foreign import unsafeParse :: forall eff. String -> Eff (locale :: LOCALE | eff) JSDate
parse :: String -> Eff _ (Maybe JSDate)
-- or --
foreign import parseFFI :: forall a. Maybe a -> (a -> Maybe a) -> String -> Eff _ (Maybe JSDate)
parse :: String -> Eff _ (Maybe JSDate)
parse = parseFFI Nothing Just
-- or --
parse : String -> Eff _ (Maybe JSDate)
parse = do
  parsed <- unsafeParse date
  pure $ if isValid parsed then Just parsed else Nothing

@hdgarrood
Copy link
Contributor

I think we should really be discouraging the use of parse entirely; even if it did return Maybe I don't think it could reasonably be described as "safe", since it's just a wrapper around Date.parse(str). This is not consistent across platforms and its behaviour can also vary unpredictably based on the format of the string you supply; see MDN (which has a warning along the same lines, albeit not very detailed).

However, I think it does make sense to include parse, for cases where people are porting JS code which uses Date.parse to PS (of course, just as a temporary measure).

AFAIK, this package is intended to be nothing more than a thin wrapper over the JS API, and so I think the current behaviour does actually make the most sense. I'd like to update the docs to discourage the use of parse more strongly (in favour of alternative date parsing libraries), but trying to give dates a nice API is the job of datetime.

@thomashoneyman thomashoneyman added the type: documentation Improvements or additions to documentation. label Sep 12, 2020
@thomashoneyman
Copy link
Contributor

I've labeled this as document me so that Harry's comment can make it into the docs:

AFAIK, this package is intended to be nothing more than a thin wrapper over the JS API, and so I think the current behaviour does actually make the most sense. I'd like to update the docs to discourage the use of parse more strongly (in favour of alternative date parsing libraries), but trying to give dates a nice API is the job of datetime.

@thomashoneyman thomashoneyman added good first issue First-time contributors who are looking to help should work on these issues. help wanted labels Sep 12, 2020
@maxdeviant
Copy link
Member

@thomashoneyman Would you envision this as function-level documentation on parse, or should this be a general note on the entire package?

@hdgarrood
Copy link
Contributor

I think the contents of the README adequately say what this package is for already, so I think it should just be a case of adding some clarity to the function-level docs for parse. Specifically: that it is a wrapper around Date.parse(), that an invalid date value can be returned if the date could not be parsed, and that the function should be avoided if at all possible.

@thomashoneyman
Copy link
Contributor

I agree with @hdgarrood. The README already specifies what the general package is for (and recommends alternatives), so it's the function itself that ought to have a bit more documentation.

@thomashoneyman
Copy link
Contributor

We've addressed this in #25.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue First-time contributors who are looking to help should work on these issues. type: documentation Improvements or additions to documentation.
Development

No branches or pull requests

5 participants