Skip to content

Prepare for 2.0 release #40

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

Merged
merged 1 commit into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"package.json"
],
"dependencies": {
"purescript-enums": "^1.0.0",
"purescript-functions": "^1.0.0",
"purescript-generics": "^1.0.0",
"purescript-integers": "^1.0.0",
"purescript-enums": "^2.0.0",
"purescript-functions": "^2.0.0",
"purescript-generics": "^3.0.0",
"purescript-integers": "^2.0.0",
"purescript-math": "^2.0.0"
},
"devDependencies": {
"purescript-assert": "^1.0.0",
"purescript-console": "^1.0.0"
"purescript-assert": "^2.0.0",
"purescript-console": "^2.0.0"
}
}
8 changes: 4 additions & 4 deletions src/Data/Date/Component.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import Data.Maybe (Maybe(..))
-- | acquire a year value from an integer.
newtype Year = Year Int

derive instance eqYear :: Eq Year
derive instance ordYear :: Ord Year
derive newtype instance eqYear :: Eq Year
derive newtype instance ordYear :: Ord Year
derive instance genericYear :: Generic Year

-- Note: these seemingly arbitrary bounds come from relying on JS for date
Expand Down Expand Up @@ -122,8 +122,8 @@ instance showMonth :: Show Month where
-- | acquire a day value from an integer.
newtype Day = Day Int

derive instance eqDay :: Eq Day
derive instance ordDay :: Ord Day
derive newtype instance eqDay :: Eq Day
derive newtype instance ordDay :: Ord Day
derive instance genericDay :: Generic Day

instance boundedDay :: Bounded Day where
Expand Down
4 changes: 2 additions & 2 deletions src/Data/DateTime/Instant.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import Partial.Unsafe (unsafePartial)
-- | `DateTime` type.
newtype Instant = Instant Milliseconds

derive instance eqDateTime :: Eq Instant
derive instance ordDateTime :: Ord Instant
derive newtype instance eqDateTime :: Eq Instant
derive newtype instance ordDateTime :: Ord Instant
derive instance genericDateTime :: Generic Instant

instance boundedInstant :: Bounded Instant where
Expand Down
6 changes: 4 additions & 2 deletions src/Data/DateTime/Locale.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Control.Comonad (class Comonad, class Extend)
import Data.DateTime (Date, Time, DateTime)
import Data.Generic (class Generic)
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype)
import Data.Time.Duration (Minutes)

-- | A date/time locale specifying an offset in minutes and an optional name for
Expand All @@ -23,8 +24,9 @@ instance showLocale :: Show Locale where
-- | The name of a date/time locale. For example: "GMT", "MDT", "CET", etc.
newtype LocaleName = LocaleName String

derive instance eqLocaleName :: Eq LocaleName
derive instance ordLocaleName :: Ord LocaleName
derive instance newtypeLocaleName :: Newtype LocaleName _
derive newtype instance eqLocaleName :: Eq LocaleName
derive newtype instance ordLocaleName :: Ord LocaleName
derive instance genericLocaleName :: Generic LocaleName

instance showLocaleName :: Show LocaleName where
Expand Down
8 changes: 4 additions & 4 deletions src/Data/Time.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import Data.Enum (fromEnum, toEnum)
import Data.Generic (class Generic)
import Data.Int as Int
import Data.Maybe (fromJust)
import Data.Newtype (unwrap)
import Data.Time.Component (Hour, Millisecond, Minute, Second)
import Data.Time.Duration (class Duration, Days(..), Milliseconds(..), unMilliseconds, fromDuration, toDuration)
import Data.Time.Duration (class Duration, Days(..), Milliseconds(..), fromDuration, toDuration)
import Data.Tuple (Tuple(..))

import Math as Math
Expand Down Expand Up @@ -80,7 +81,7 @@ adjust d t =
d' = fromDuration d
tLength = timeToMillis t
dayLength = 86400000.0
wholeDays = Days $ Math.floor (unMilliseconds d' / dayLength)
wholeDays = Days $ Math.floor (unwrap d' / dayLength)
msAdjust = d' - fromDuration wholeDays
msAdjusted = tLength + msAdjust
wrap = if msAdjusted > maxTime then 1.0 else if msAdjusted < -maxTime then -1.0 else 0.0
Expand All @@ -100,9 +101,8 @@ timeToMillis t = Milliseconds
+ Int.toNumber (fromEnum (millisecond t))

millisToTime :: Milliseconds -> Time
millisToTime ms =
millisToTime ms@(Milliseconds ms') =
let
ms' = unMilliseconds ms
hourLength = 3600000.0
minuteLength = 60000.0
secondLength = 1000.0
Expand Down
16 changes: 8 additions & 8 deletions src/Data/Time/Component.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Data.Maybe (Maybe(..))
-- | lowered to a plain integer with the `fromEnum` function.
newtype Hour = Hour Int

derive instance eqHour :: Eq Hour
derive instance ordHour :: Ord Hour
derive newtype instance eqHour :: Eq Hour
derive newtype instance ordHour :: Ord Hour
derive instance genericHour :: Generic Hour

instance boundedHour :: Bounded Hour where
Expand Down Expand Up @@ -49,8 +49,8 @@ instance showHour :: Show Hour where
-- | be lowered to a plain integer with the `fromEnum` function.
newtype Minute = Minute Int

derive instance eqMinute :: Eq Minute
derive instance ordMinute :: Ord Minute
derive newtype instance eqMinute :: Eq Minute
derive newtype instance ordMinute :: Ord Minute
derive instance genericMinute :: Generic Minute

instance boundedMinute :: Bounded Minute where
Expand Down Expand Up @@ -79,8 +79,8 @@ instance showMinute :: Show Minute where
-- | be lowered to a plain integer with the `fromEnum` function.
newtype Second = Second Int

derive instance eqSecond :: Eq Second
derive instance ordSecond :: Ord Second
derive newtype instance eqSecond :: Eq Second
derive newtype instance ordSecond :: Ord Second
derive instance genericSecond :: Generic Second

instance boundedSecond :: Bounded Second where
Expand Down Expand Up @@ -110,8 +110,8 @@ instance showSecond :: Show Second where
-- | function.
newtype Millisecond = Millisecond Int

derive instance eqMillisecond :: Eq Millisecond
derive instance ordMillisecond :: Ord Millisecond
derive newtype instance eqMillisecond :: Eq Millisecond
derive newtype instance ordMillisecond :: Ord Millisecond
derive instance genericMillisecond :: Generic Millisecond

instance boundedMillisecond :: Bounded Millisecond where
Expand Down
112 changes: 34 additions & 78 deletions src/Data/Time/Duration.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,69 @@ module Data.Time.Duration where
import Prelude

import Data.Generic (class Generic)
import Data.Newtype (class Newtype, over)

-- | A duration measured in milliseconds.
newtype Milliseconds = Milliseconds Number

unMilliseconds :: Milliseconds -> Number
unMilliseconds (Milliseconds ms) = ms

derive instance eqMilliseconds :: Eq Milliseconds
derive instance ordMilliseconds :: Ord Milliseconds
derive instance newtypeMilliseconds :: Newtype Milliseconds _
derive instance genericMilliseconds :: Generic Milliseconds

instance semiringMilliseconds :: Semiring Milliseconds where
add (Milliseconds x) (Milliseconds y) = Milliseconds (x + y)
mul (Milliseconds x) (Milliseconds y) = Milliseconds (x * y)
zero = Milliseconds 0.0
one = Milliseconds 1.0

instance ringMilliseconds :: Ring Milliseconds where
sub (Milliseconds x) (Milliseconds y) = Milliseconds (x - y)
derive newtype instance eqMilliseconds :: Eq Milliseconds
derive newtype instance ordMilliseconds :: Ord Milliseconds
derive newtype instance semiringMilliseconds :: Semiring Milliseconds
derive newtype instance ringMilliseconds :: Ring Milliseconds

instance showMilliseconds :: Show Milliseconds where
show (Milliseconds n) = "(Milliseconds " <> show n <> ")"

-- | A duration measured in seconds.
newtype Seconds = Seconds Number

unSeconds :: Seconds -> Number
unSeconds (Seconds s) = s

derive instance eqSeconds :: Eq Seconds
derive instance ordSeconds :: Ord Seconds
derive instance newtypeSeconds :: Newtype Seconds _
derive instance genericSeconds :: Generic Seconds

instance semiringSeconds :: Semiring Seconds where
add (Seconds x) (Seconds y) = Seconds (x + y)
mul (Seconds x) (Seconds y) = Seconds (x * y)
zero = Seconds 0.0
one = Seconds 1.0

instance ringSeconds :: Ring Seconds where
sub (Seconds x) (Seconds y) = Seconds (x - y)
derive newtype instance eqSeconds :: Eq Seconds
derive newtype instance ordSeconds :: Ord Seconds
derive newtype instance semiringSeconds :: Semiring Seconds
derive newtype instance ringSeconds :: Ring Seconds

instance showSeconds :: Show Seconds where
show (Seconds n) = "(Seconds " <> show n <> ")"

-- | A duration measured in minutes.
newtype Minutes = Minutes Number

unMinutes :: Minutes -> Number
unMinutes (Minutes m) = m

derive instance eqMinutes :: Eq Minutes
derive instance ordMinutes :: Ord Minutes
derive instance newtypeMinutes :: Newtype Minutes _
derive instance genericMinutes :: Generic Minutes

instance semiringMinutes :: Semiring Minutes where
add (Minutes x) (Minutes y) = Minutes (x + y)
mul (Minutes x) (Minutes y) = Minutes (x * y)
zero = Minutes 0.0
one = Minutes 1.0

instance ringMinutes :: Ring Minutes where
sub (Minutes x) (Minutes y) = Minutes (x - y)
derive newtype instance eqMinutes :: Eq Minutes
derive newtype instance ordMinutes :: Ord Minutes
derive newtype instance semiringMinutes :: Semiring Minutes
derive newtype instance ringMinutes :: Ring Minutes

instance showMinutes :: Show Minutes where
show (Minutes n) = "(Minutes " <> show n <> ")"

-- | A duration measured in hours.
newtype Hours = Hours Number

unHours :: Hours -> Number
unHours (Hours m) = m

derive instance eqHours :: Eq Hours
derive instance ordHours :: Ord Hours
derive instance newtypeHours :: Newtype Hours _
derive instance genericHours :: Generic Hours

instance semiringHours :: Semiring Hours where
add (Hours x) (Hours y) = Hours (x + y)
mul (Hours x) (Hours y) = Hours (x * y)
zero = Hours 0.0
one = Hours 1.0

instance ringHours :: Ring Hours where
sub (Hours x) (Hours y) = Hours (x - y)
derive newtype instance eqHours :: Eq Hours
derive newtype instance ordHours :: Ord Hours
derive newtype instance semiringHours :: Semiring Hours
derive newtype instance ringHours :: Ring Hours

instance showHours :: Show Hours where
show (Hours n) = "(Hours " <> show n <> ")"

-- | A duration measured in days, where a day is assumed to be exactly 24 hours.
newtype Days = Days Number

unDays :: Days -> Number
unDays (Days m) = m

derive instance eqDays :: Eq Days
derive instance ordDays :: Ord Days
derive instance newtypeDays :: Newtype Days _
derive instance genericDays :: Generic Days

instance semiringDays :: Semiring Days where
add (Days x) (Days y) = Days (x + y)
mul (Days x) (Days y) = Days (x * y)
zero = Days 0.0
one = Days 1.0

instance ringDays :: Ring Days where
sub (Days x) (Days y) = Days (x - y)
derive newtype instance eqDays :: Eq Days
derive newtype instance ordDays :: Ord Days
derive newtype instance semiringDays :: Semiring Days
derive newtype instance ringDays :: Ring Days

instance showDays :: Show Days where
show (Days n) = "(Days " <> show n <> ")"
Expand All @@ -128,17 +84,17 @@ instance durationMilliseconds :: Duration Milliseconds where
toDuration = id

instance durationSeconds :: Duration Seconds where
fromDuration = Milliseconds <<< (_ * 1000.0) <<< unSeconds
toDuration (Milliseconds ms) = Seconds (ms / 1000.0)
fromDuration = over Seconds (_ * 1000.0)
toDuration = over Milliseconds (_ / 1000.0)

instance durationMinutes :: Duration Minutes where
fromDuration = Milliseconds <<< (_ * 60000.0) <<< unMinutes
toDuration (Milliseconds ms) = Minutes (ms / 60000.0)
fromDuration = over Minutes (_ * 60000.0)
toDuration = over Milliseconds (_ / 60000.0)

instance durationHours :: Duration Hours where
fromDuration = Milliseconds <<< (_ * 3600000.0) <<< unHours
toDuration (Milliseconds ms) = Hours (ms / 3600000.0)
fromDuration = over Hours (_ * 3600000.0)
toDuration = over Milliseconds (_ / 3600000.0)

instance durationDays :: Duration Days where
fromDuration = Milliseconds <<< (_ * 86400000.0) <<< unDays
toDuration (Milliseconds ms) = Days (ms / 86400000.0)
fromDuration = over Days (_ * 86400000.0)
toDuration = over Milliseconds (_ / 86400000.0)
5 changes: 3 additions & 2 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Enum (class BoundedEnum, Cardinality, toEnum, enumFromTo, cardinality, runCardinality, succ, fromEnum, pred)
import Data.Enum (class BoundedEnum, Cardinality, toEnum, enumFromTo, cardinality, succ, fromEnum, pred)
import Data.Date as Date
import Data.Time as Time
import Data.Time.Duration as Duration
Expand All @@ -14,6 +14,7 @@ import Data.DateTime as DateTime
import Data.DateTime.Instant as Instant
import Data.Maybe (Maybe(..), fromJust)
import Data.Tuple (Tuple(..), snd)
import Data.Newtype (unwrap)

import Type.Proxy (Proxy(..))
import Test.Assert (ASSERT, assert)
Expand Down Expand Up @@ -144,5 +145,5 @@ checkBounded _ = do
checkBoundedEnum :: forall e. BoundedEnum e => Proxy e -> Tests
checkBoundedEnum p = do
checkBounded p
let card = runCardinality (cardinality :: Cardinality e)
let card = unwrap (cardinality :: Cardinality e)
assert $ Array.length (enumFromTo bottom (top :: e)) == card