Skip to content

Commit d33c20e

Browse files
committed
Derive Generic instances
1 parent eea84ec commit d33c20e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Data/Date.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ module Data.Date
2020

2121
import Prelude
2222

23+
import Control.Monad
2324
import Control.Monad.Eff
2425
import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred)
2526
import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3)
2627
import Data.Maybe (Maybe(..))
2728
import Data.Time
29+
import Data.Generic
2830

2931
-- | A native JavaScript `Date` object.
3032
foreign import data JSDate :: *
@@ -36,6 +38,12 @@ foreign import data JSDate :: *
3638
-- | modules.
3739
newtype Date = DateTime JSDate
3840

41+
instance genericDate :: Generic Date where
42+
toSpine d = SProd "Date" [const (toSpine $ toEpochMilliseconds d)]
43+
toSignature _ = SigProd [{ sigConstructor: "Date", sigValues: [const $ toSignature (anyProxy :: Proxy Milliseconds)] }]
44+
fromSpine (SProd "Date" [msf]) = fromSpine (msf unit) >>= fromEpochMilliseconds
45+
fromSpine _ = Nothing
46+
3947
instance eqDate :: Eq Date where
4048
eq = eq `on` toEpochMilliseconds
4149

src/Data/Time.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ import Prelude
1818
, compare
1919
, show )
2020

21+
import Data.Generic
22+
2123
-- | An hour component from a time value. Should fall between 0 and 23
2224
-- | inclusive.
2325
newtype HourOfDay = HourOfDay Int
2426

27+
derive instance genericHourOfDay :: Generic HourOfDay
28+
2529
instance eqHourOfDay :: Eq HourOfDay where
2630
eq (HourOfDay x) (HourOfDay y) = x == y
2731

@@ -31,6 +35,8 @@ instance ordHourOfDay :: Ord HourOfDay where
3135
-- | A quantity of hours (not necessarily a value between 0 and 23).
3236
newtype Hours = Hours Number
3337

38+
derive instance genericHours :: Generic Hours
39+
3440
instance eqHours :: Eq Hours where
3541
eq (Hours x) (Hours y) = x == y
3642

@@ -61,6 +67,8 @@ instance showHours :: Show Hours where
6167
-- | inclusive.
6268
newtype MinuteOfHour = MinuteOfHour Int
6369

70+
derive instance genericMinuteOfHour :: Generic MinuteOfHour
71+
6472
instance eqMinuteOfHour :: Eq MinuteOfHour where
6573
eq (MinuteOfHour x) (MinuteOfHour y) = x == y
6674

@@ -70,6 +78,8 @@ instance ordMinuteOfHour :: Ord MinuteOfHour where
7078
-- | A quantity of minutes (not necessarily a value between 0 and 60).
7179
newtype Minutes = Minutes Number
7280

81+
derive instance genericMinutes :: Generic Minutes
82+
7383
instance eqMinutes :: Eq Minutes where
7484
eq (Minutes x) (Minutes y) = x == y
7585

@@ -100,6 +110,8 @@ instance showMinutes :: Show Minutes where
100110
-- | inclusive.
101111
newtype SecondOfMinute = SecondOfMinute Int
102112

113+
derive instance genericSecondOfMinute :: Generic SecondOfMinute
114+
103115
instance eqSecondOfMinute :: Eq SecondOfMinute where
104116
eq (SecondOfMinute x) (SecondOfMinute y) = x == y
105117

@@ -109,6 +121,8 @@ instance ordSecondOfMinute :: Ord SecondOfMinute where
109121
-- | A quantity of seconds (not necessarily a value between 0 and 60).
110122
newtype Seconds = Seconds Number
111123

124+
derive instance genericSeconds :: Generic Seconds
125+
112126
instance eqSeconds :: Eq Seconds where
113127
eq (Seconds x) (Seconds y) = x == y
114128

@@ -139,6 +153,8 @@ instance showSeconds :: Show Seconds where
139153
-- | inclusive.
140154
newtype MillisecondOfSecond = MillisecondOfSecond Int
141155

156+
derive instance genericMillisecondOfSecond :: Generic MillisecondOfSecond
157+
142158
instance eqMillisecondOfSecond :: Eq MillisecondOfSecond where
143159
eq (MillisecondOfSecond x) (MillisecondOfSecond y) = x == y
144160

@@ -148,6 +164,8 @@ instance ordMillisecondOfSecond :: Ord MillisecondOfSecond where
148164
-- | A quantity of milliseconds (not necessarily a value between 0 and 1000).
149165
newtype Milliseconds = Milliseconds Number
150166

167+
derive instance genericMilliseconds :: Generic Milliseconds
168+
151169
instance eqMilliseconds :: Eq Milliseconds where
152170
eq (Milliseconds x) (Milliseconds y) = x == y
153171

0 commit comments

Comments
 (0)