Skip to content

Commit b9f0ffe

Browse files
committed
Derive Generic instances
1 parent 14639e9 commit b9f0ffe

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Data/Date.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred)
2525
import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3)
2626
import Data.Maybe (Maybe(..))
2727
import Data.Time
28+
import Data.Generic
2829

2930
-- | A native JavaScript `Date` object.
3031
foreign import data JSDate :: *
@@ -36,6 +37,14 @@ foreign import data JSDate :: *
3637
-- | modules.
3738
newtype Date = DateTime JSDate
3839

40+
instance genericDate :: Generic Date where
41+
toSpine d = SProd "Date" [const (toSpine $ toEpochMilliseconds d)]
42+
toSignature _ = SigProd [{ sigConstructor: "Date", sigValues: [const SigNumber] }]
43+
fromSpine (SProd "Date" [msf]) = case msf unit of
44+
(SNumber ms) -> fromEpochMilliseconds $ Milliseconds ms
45+
_ -> Nothing
46+
fromSpine _ = Nothing
47+
3948
instance eqDate :: Eq Date where
4049
eq = eq `on` toEpochMilliseconds
4150

src/Data/Time.purs

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

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

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

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

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

@@ -62,6 +68,8 @@ instance showHours :: Show Hours where
6268
-- | inclusive.
6369
newtype MinuteOfHour = MinuteOfHour Int
6470

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

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

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

@@ -101,6 +111,8 @@ instance showMinutes :: Show Minutes where
101111
-- | inclusive.
102112
newtype SecondOfMinute = SecondOfMinute Int
103113

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

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

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

@@ -140,6 +154,8 @@ instance showSeconds :: Show Seconds where
140154
-- | inclusive.
141155
newtype MillisecondOfSecond = MillisecondOfSecond Int
142156

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

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

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

0 commit comments

Comments
 (0)