Skip to content

Commit

Permalink
Add Encode and Decode instances to Identity (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurxavierx authored and paf31 committed Feb 27, 2019
1 parent 47442ad commit 918327d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"purescript-ordered-collections": "^1.0.0",
"purescript-proxy": "^3.0.0",
"purescript-exceptions": "^4.0.0",
"purescript-record": "^1.0.0"
"purescript-record": "^1.0.0",
"purescript-identity": "^4.1.0"
},
"devDependencies": {
"purescript-assert": "^4.0.0",
Expand Down
8 changes: 8 additions & 0 deletions src/Foreign/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import Control.Monad.Except (except, mapExcept)
import Data.Array ((..), zipWith, length)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Identity (Identity(..))
import Data.Maybe (Maybe, maybe)
import Data.Newtype (unwrap)
import Data.Traversable (sequence)
import Foreign (F, Foreign, ForeignError(..), readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign)
import Foreign.Internal (readObject)
Expand Down Expand Up @@ -56,6 +58,9 @@ instance numberDecode :: Decode Number where
instance intDecode :: Decode Int where
decode = readInt

instance identityDecode :: Decode a => Decode (Identity a) where
decode = map Identity <<< decode

instance arrayDecode :: Decode a => Decode (Array a) where
decode = readArray >=> readElements where
readElements :: Array Foreign -> F (Array a)
Expand Down Expand Up @@ -112,6 +117,9 @@ instance numberEncode :: Encode Number where
instance intEncode :: Encode Int where
encode = unsafeToForeign

instance identityEncode :: Encode a => Encode (Identity a) where
encode = encode <<< unwrap

instance arrayEncode :: Encode a => Encode (Array a) where
encode = unsafeToForeign <<< map encode

Expand Down

0 comments on commit 918327d

Please sign in to comment.