Skip to content

Commit

Permalink
Merge pull request #2 from purescript-contrib/isforeign
Browse files Browse the repository at this point in the history
Add IsForeign instance for JSDate
  • Loading branch information
garyb authored Jul 13, 2016
2 parents b4f9d61 + d107ca8 commit 8ca0283
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"purescript-datetime": "^1.0.0",
"purescript-exceptions": "^1.0.0",
"purescript-foreign": "^1.0.0",
"purescript-integers": "^1.0.0"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/Data/JSDate.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Data.DateTime as DateTime
import Data.DateTime.Instant (Instant)
import Data.DateTime.Instant as Instant
import Data.Enum (fromEnum)
import Data.Foreign (unsafeReadTagged)
import Data.Foreign.Class (class IsForeign)
import Data.Function.Uncurried (Fn2, runFn2)
import Data.Int (toNumber)
import Data.Maybe (Maybe(..))
Expand All @@ -28,6 +30,9 @@ import Data.Time.Duration (Milliseconds(..))
-- | The type of JavaScript `Date` objects.
foreign import data JSDate :: *

instance isForeignJSDate :: IsForeign JSDate where
read = unsafeReadTagged "Date"

-- | Checks whether a date value is valid. When a date is invalid, the majority
-- | of the functions return `NaN`, `"Invalid Date"`, or throw an exception.
foreign import isValid :: JSDate -> Boolean
Expand Down
3 changes: 3 additions & 0 deletions test/Test/Main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

exports.myDate = new Date();
10 changes: 9 additions & 1 deletion test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.DateTime as DT
import Data.Enum (toEnum)
import Data.Either (isRight)
import Data.Foreign (F, Foreign)
import Data.Foreign.Class (read)
import Data.JSDate as JSD
import Data.DateTime as DT
import Data.Maybe (Maybe(..), fromJust)
import Partial.Unsafe (unsafePartial)

import Global (nan)

import Test.Assert (ASSERT, assert)

foreign import myDate :: Foreign

main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT, locale :: JSD.LOCALE | eff) Unit
main = do

log "Check IsForeign intance will read JS date values..."
assert $ isRight $ read myDate :: F JSD.JSDate

log "Checking that a UTC date constructed with sensible values is valid..."
assert $ JSD.isValid $ JSD.jsdate defaultDateRecord

Expand Down

0 comments on commit 8ca0283

Please sign in to comment.