diff --git a/bower.json b/bower.json index 499acf0..959fd27 100644 --- a/bower.json +++ b/bower.json @@ -26,6 +26,7 @@ "dependencies": { "purescript-enums": "^0.7.0", "purescript-functions": "^0.1.0", - "purescript-globals": "^0.2.0" + "purescript-globals": "^0.2.0", + "purescript-generics": "^0.7.0" } } diff --git a/src/Data/Date.purs b/src/Data/Date.purs index 10daa89..3211b30 100644 --- a/src/Data/Date.purs +++ b/src/Data/Date.purs @@ -20,11 +20,14 @@ module Data.Date import Prelude +import Type.Proxy + import Control.Monad.Eff import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred) import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3) import Data.Maybe (Maybe(..)) import Data.Time +import Data.Generic -- | A native JavaScript `Date` object. foreign import data JSDate :: * @@ -36,6 +39,12 @@ foreign import data JSDate :: * -- | modules. newtype Date = DateTime JSDate +instance genericDate :: Generic Date where + toSpine d = SProd "Date" [const (toSpine $ toEpochMilliseconds d)] + toSignature _ = SigProd "Date" [{ sigConstructor: "Date", sigValues: [const $ toSignature (Proxy :: Proxy Milliseconds)] }] + fromSpine (SProd "Date" [msf]) = fromSpine (msf unit) >>= fromEpochMilliseconds + fromSpine _ = Nothing + instance eqDate :: Eq Date where eq = eq `on` toEpochMilliseconds diff --git a/src/Data/Time.purs b/src/Data/Time.purs index dbc6754..db64d2d 100644 --- a/src/Data/Time.purs +++ b/src/Data/Time.purs @@ -18,10 +18,14 @@ import Prelude , compare , show ) +import Data.Generic + -- | An hour component from a time value. Should fall between 0 and 23 -- | inclusive. newtype HourOfDay = HourOfDay Int +derive instance genericHourOfDay :: Generic HourOfDay + instance eqHourOfDay :: Eq HourOfDay where eq (HourOfDay x) (HourOfDay y) = x == y @@ -31,6 +35,8 @@ instance ordHourOfDay :: Ord HourOfDay where -- | A quantity of hours (not necessarily a value between 0 and 23). newtype Hours = Hours Number +derive instance genericHours :: Generic Hours + instance eqHours :: Eq Hours where eq (Hours x) (Hours y) = x == y @@ -61,6 +67,8 @@ instance showHours :: Show Hours where -- | inclusive. newtype MinuteOfHour = MinuteOfHour Int +derive instance genericMinuteOfHour :: Generic MinuteOfHour + instance eqMinuteOfHour :: Eq MinuteOfHour where eq (MinuteOfHour x) (MinuteOfHour y) = x == y @@ -70,6 +78,8 @@ instance ordMinuteOfHour :: Ord MinuteOfHour where -- | A quantity of minutes (not necessarily a value between 0 and 60). newtype Minutes = Minutes Number +derive instance genericMinutes :: Generic Minutes + instance eqMinutes :: Eq Minutes where eq (Minutes x) (Minutes y) = x == y @@ -100,6 +110,8 @@ instance showMinutes :: Show Minutes where -- | inclusive. newtype SecondOfMinute = SecondOfMinute Int +derive instance genericSecondOfMinute :: Generic SecondOfMinute + instance eqSecondOfMinute :: Eq SecondOfMinute where eq (SecondOfMinute x) (SecondOfMinute y) = x == y @@ -109,6 +121,8 @@ instance ordSecondOfMinute :: Ord SecondOfMinute where -- | A quantity of seconds (not necessarily a value between 0 and 60). newtype Seconds = Seconds Number +derive instance genericSeconds :: Generic Seconds + instance eqSeconds :: Eq Seconds where eq (Seconds x) (Seconds y) = x == y @@ -139,6 +153,8 @@ instance showSeconds :: Show Seconds where -- | inclusive. newtype MillisecondOfSecond = MillisecondOfSecond Int +derive instance genericMillisecondOfSecond :: Generic MillisecondOfSecond + instance eqMillisecondOfSecond :: Eq MillisecondOfSecond where eq (MillisecondOfSecond x) (MillisecondOfSecond y) = x == y @@ -148,6 +164,8 @@ instance ordMillisecondOfSecond :: Ord MillisecondOfSecond where -- | A quantity of milliseconds (not necessarily a value between 0 and 1000). newtype Milliseconds = Milliseconds Number +derive instance genericMilliseconds :: Generic Milliseconds + instance eqMilliseconds :: Eq Milliseconds where eq (Milliseconds x) (Milliseconds y) = x == y