date icon indicating copy to clipboard operation
date copied to clipboard

Expose useful helpers

Open cmditch opened this issue 4 years ago • 4 comments

Hey @justinmimbs, once again thanks greatly for the fantastic libraries.

I've found myself copying these helpers in a few codebases at this point. Curious if we can just expose them?

Thanks

cmditch avatar Jun 13 '20 01:06 cmditch

Hi Coury,

Despite a handful of requests to expose these functions, I've been reluctant to, because so far we've always found simpler solutions using the existing higher-level functions. Would you mind sharing your use case for the functions?

Thanks!

Justin

justinmimbs avatar Jun 15 '20 11:06 justinmimbs

In the first instance, I'm creating a library called elm-rrule for working with recurring events as defined in the iCalendar spec (RFC 5545). I make heavy use of your Time.Extra and Date libraries. The amount of work that's saved me is incredible, much thanks.

I've actually had to copy a good number of low-level function out of Date. The primary reason being that not all calendars start the week with Monday (see http://chartsbin.com/view/41671), and in the iCalendar spec one is able to define WKST (weekstart).

This above situation is not a simple matter of exposing isLeapYear or daysInMonth, as it would require a significant API change to support Saturday, Sunday, and Monday weekstarts. I think this is worth considering in order to make the library more internationally friendly, but I understand that's a non-trivial undertaking.

The other codebase where we've brought daysInMonth in is to URL validation. We track the calendar view similar to Google Calendar https://calendar.google.com/calendar/r/week/2020/6/24, and we check to make sure they haven't entered something like week/2020/6/123.

cmditch avatar Jun 17 '20 16:06 cmditch

You can see here how I've adapted weekdayNumber to work with different weekstarts https://github.com/RealKinetic/elm-rrule/blob/master/src/Util.elm#L363

cmditch avatar Jun 17 '20 16:06 cmditch

Unless I've misunderstood, you should be able to define weekdayNumber for custom week-starts like this:

weekdayNumber : Weekday -> Date -> Int
weekdayNumber weekStart date =
    (Date.weekdayNumber date - Date.weekdayToNumber weekStart |> modBy 7) + 1

Here's an Ellie example. Is that what you're looking for?

I can see why you'd want daysInMonth for validating user input. As an alternative, you could put the numeric date into an ISO formatted string, run it through fromIsoString, and check for an Ok result; that function returns an Err on invalid date values (unlike fromCalendarDate, which clamps out-of-range values).

justinmimbs avatar Jun 17 '20 17:06 justinmimbs