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

ADD: Eq, Ord, and Show instances for JSDate #17

Merged
merged 9 commits into from
Mar 20, 2018
4 changes: 4 additions & 0 deletions src/Data/JSDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ exports.parse = function (dateString) {
return new Date(dateString);
};
};

exports.fromTime = function (time) {
return new Date(time);
};
12 changes: 12 additions & 0 deletions src/Data/JSDate.purs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ import Data.Time.Duration (Milliseconds(..))
-- | The type of JavaScript `Date` objects.
foreign import data JSDate :: Type

instance eqJSDate :: Eq JSDate where
eq a b = getTime a == getTime b

instance ordJSDate :: Ord JSDate where
compare a b = getTime a `compare` getTime b

instance showJSDate :: Show JSDate where
show a = "(fromTime " <> show (getTime a) <> ")"

-- | The effect type used when indicating the current machine's date/time locale
-- | is used in computing a value.
foreign import data LOCALE :: Effect
Expand Down Expand Up @@ -263,3 +272,6 @@ toTimeString dt = runFn2 dateMethod "toTimeString" dt
-- | Returns the date as a string using the UTC timezone.
toUTCString :: JSDate -> String
toUTCString dt = runFn2 dateMethod "toUTCString" dt

-- | Returns the date at a number of milliseconds since 1970-01-01 00:00:00 UTC.
foreign import fromTime :: Number -> JSDate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromTime isn't exported or used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already exists as fromInstant too, I'd prefer we use that instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromTime copes with NaN and out of bound Numbers in the native way, which is not something that makes much sense with Instant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the fromTime export? Then I'll finally get this merged in and released.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it disappeared again in the convoluted reversion process 😉

12 changes: 12 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ main = do
assert $ JSD.toDateTime (JSD.fromDateTime bottom) == Just bottom
assert $ JSD.toDateTime (JSD.fromDateTime top) == Just top

log "Check that equal dates test equal"
assert $ JSD.fromDateTime dateTime == JSD.fromDateTime dateTime
assert $ JSD.fromDateTime ancientDateTime == JSD.fromDateTime ancientDateTime

log "Check that unequal dates do not test equal"
assert $ JSD.fromDateTime dateTime /= JSD.fromDateTime ancientDateTime

log "Check that dates are chronologically ordered"
assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime dateTime == EQ
assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime ancientDateTime == GT
assert $ JSD.fromDateTime ancientDateTime `compare` JSD.fromDateTime dateTime == LT

log "All tests done"

where
Expand Down