git.iko.soy/rowtype-yoga/purescript-yoga-json
on main help sign in
Add DateTimeCodec
commit9590f73ac582eb75c2b7bce14a7ddcb1e589c8a3 authorMark Eibes <mark.eibes@gmail.com> dateThu 21 Jul 2022, 15:29 (4 years ago) parent2a6bca6
5 files changed+29−3
README.md+3 spago.dhall+1 src/Yoga/JSON.purs+12−1 test.dhall+1 test/BasicsSpec.purs+12−2
README.md+3
@@ -32,6 +32,9 @@ There is an inbuilt codec for `Tuple`s thanks to @ursi
3232There is an inbuilt codec for `Either`s.
3333`yoga-json` represents eithers as objects with a `type` and a `value` tag in JSON.
3434
35+### JSDate and DateTime
36+Both are encoded as ISO8601 strings as spat out by `JSDate.toISOString`.
37+
3538### Generics
3639It includes @justinwoo's codecs for en- and decoding generics inspired by
3740[simple-json-generics](https://github.com/justinwoo/purescript-simple-json-generics)
spago.dhall+1
@@ -7,6 +7,7 @@
77 , "bigints"
88 , "console"
99 , "control"
10+ , "datetime"
1011 , "effect"
1112 , "either"
1213 , "exceptions"
src/Yoga/JSON.purs+12−1
@@ -41,7 +41,9 @@ import Data.Array.NonEmpty (NonEmptyArray, fromArray, toArray)
4141import Data.Bifunctor (lmap)
4242import Data.BigInt (BigInt)
4343import Data.BigInt as BigInt
44−import Data.Either (Either(..), hush, note)
44+import Data.DateTime (DateTime(..))
45+import Data.DateTime as DateTime
46+import Data.Either (Either(..), either, hush, note)
4547import Data.FoldableWithIndex (foldrWithIndex)
4648import Data.Identity (Identity(..))
4749import Data.Int as Int
@@ -535,6 +537,15 @@ instance WriteForeign JSDate where
535537instance ReadForeign JSDate where
536538 readImpl = readImpl >>> map (JSDate.parse >>> unsafePerformEffect)
537539
540+instance WriteForeign DateTime where
541+ writeImpl = JSDate.fromDateTime >>> writeImpl
542+
543+instance ReadForeign DateTime where
544+ readImpl = readImpl >=>
545+ JSDate.toDateTime
546+ >>> note (pure $ ForeignError "Invalid date time")
547+ >>> except
548+
538549unsafeStringToInt :: String → Int
539550unsafeStringToInt = Int.fromString >>>
540551 (fromMaybe' \_ -> unsafeCrashWith "impossible")
test.dhall+1
@@ -10,5 +10,6 @@ in conf
1010 , "strings-extra"
1111 , "newtype"
1212 , "debug"
13+ , "now"
1314 ]
1415 }
test/BasicsSpec.purs+12−2
@@ -6,19 +6,20 @@ import Data.Array.NonEmpty as NEA
66import Data.BigInt (BigInt)
77import Data.Either (Either(..))
88import Data.Foldable (traverse_)
9−import Data.JSDate as JSDAte
109import Data.JSDate as JSDate
1110import Data.List as List
1211import Data.List.Lazy as LazyList
1312import Data.Map as Map
14−import Data.Maybe (Maybe(..))
13+import Data.Maybe (Maybe(..), fromMaybe')
1514import Data.Newtype (class Newtype, un)
1615import Data.Nullable as Nullable
1716import Data.Tuple (Tuple(..))
1817import Data.Tuple.Nested ((/\))
1918import Data.Variant (Variant, inj)
2019import Effect.Class (liftEffect)
20+import Effect.Now (nowDate, nowDateTime)
2121import Foreign.Object as Object
22+import Partial.Unsafe (unsafeCrashWith, unsafePartial)
2223import Test.Spec (Spec, describe, it)
2324import Test.Spec.Assertions (shouldEqual)
2425import Test.Util (roundtrips)
@@ -91,6 +92,15 @@ spec = describe "En- and decoding" $ do
9192 let expected = show "2022-01-01T00:00:00.000Z"
9293 result `shouldEqual` expected
9394
95+ describe "works with DateTime" do
96+ it "roundtrips" do
97+ now <- nowDateTime # liftEffect
98+ roundtrips now
99+ someDate <- JSDate.parse "2022-01-01:00:00:00Z" <#> (JSDate.toDateTime >>> fromMaybe' \_ -> unsafeCrashWith "nope") # liftEffect
100+ let result = writeJSON someDate
101+ let expected = show "2022-01-01T00:00:00.000Z"
102+ result `shouldEqual` expected
103+
94104 describe "works on record types" do
95105 it "roundtrips" do
96106 roundtrips { a: 12, b: "54" }