Skip to content

Commit 8ca0283

Browse files
authored
Merge pull request #2 from purescript-contrib/isforeign
Add IsForeign instance for JSDate
2 parents b4f9d61 + d107ca8 commit 8ca0283

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"purescript-datetime": "^1.0.0",
2020
"purescript-exceptions": "^1.0.0",
21+
"purescript-foreign": "^1.0.0",
2122
"purescript-integers": "^1.0.0"
2223
},
2324
"devDependencies": {

src/Data/JSDate.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Data.DateTime as DateTime
1919
import Data.DateTime.Instant (Instant)
2020
import Data.DateTime.Instant as Instant
2121
import Data.Enum (fromEnum)
22+
import Data.Foreign (unsafeReadTagged)
23+
import Data.Foreign.Class (class IsForeign)
2224
import Data.Function.Uncurried (Fn2, runFn2)
2325
import Data.Int (toNumber)
2426
import Data.Maybe (Maybe(..))
@@ -28,6 +30,9 @@ import Data.Time.Duration (Milliseconds(..))
2830
-- | The type of JavaScript `Date` objects.
2931
foreign import data JSDate :: *
3032

33+
instance isForeignJSDate :: IsForeign JSDate where
34+
read = unsafeReadTagged "Date"
35+
3136
-- | Checks whether a date value is valid. When a date is invalid, the majority
3237
-- | of the functions return `NaN`, `"Invalid Date"`, or throw an exception.
3338
foreign import isValid :: JSDate -> Boolean

test/Test/Main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
exports.myDate = new Date();

test/Test/Main.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8+
import Data.DateTime as DT
89
import Data.Enum (toEnum)
10+
import Data.Either (isRight)
11+
import Data.Foreign (F, Foreign)
12+
import Data.Foreign.Class (read)
913
import Data.JSDate as JSD
10-
import Data.DateTime as DT
1114
import Data.Maybe (Maybe(..), fromJust)
1215
import Partial.Unsafe (unsafePartial)
1316

1417
import Global (nan)
1518

1619
import Test.Assert (ASSERT, assert)
1720

21+
foreign import myDate :: Foreign
22+
1823
main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT, locale :: JSD.LOCALE | eff) Unit
1924
main = do
2025

26+
log "Check IsForeign intance will read JS date values..."
27+
assert $ isRight $ read myDate :: F JSD.JSDate
28+
2129
log "Checking that a UTC date constructed with sensible values is valid..."
2230
assert $ JSD.isValid $ JSD.jsdate defaultDateRecord
2331

0 commit comments

Comments
 (0)