Skip to content

Commit 918327d

Browse files
arthurxavierxpaf31
authored andcommitted
Add Encode and Decode instances to Identity (paf31#50)
1 parent 47442ad commit 918327d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"purescript-ordered-collections": "^1.0.0",
2323
"purescript-proxy": "^3.0.0",
2424
"purescript-exceptions": "^4.0.0",
25-
"purescript-record": "^1.0.0"
25+
"purescript-record": "^1.0.0",
26+
"purescript-identity": "^4.1.0"
2627
},
2728
"devDependencies": {
2829
"purescript-assert": "^4.0.0",

src/Foreign/Class.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import Control.Monad.Except (except, mapExcept)
66
import Data.Array ((..), zipWith, length)
77
import Data.Bifunctor (lmap)
88
import Data.Either (Either(..))
9+
import Data.Identity (Identity(..))
910
import Data.Maybe (Maybe, maybe)
11+
import Data.Newtype (unwrap)
1012
import Data.Traversable (sequence)
1113
import Foreign (F, Foreign, ForeignError(..), readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign)
1214
import Foreign.Internal (readObject)
@@ -56,6 +58,9 @@ instance numberDecode :: Decode Number where
5658
instance intDecode :: Decode Int where
5759
decode = readInt
5860

61+
instance identityDecode :: Decode a => Decode (Identity a) where
62+
decode = map Identity <<< decode
63+
5964
instance arrayDecode :: Decode a => Decode (Array a) where
6065
decode = readArray >=> readElements where
6166
readElements :: Array Foreign -> F (Array a)
@@ -112,6 +117,9 @@ instance numberEncode :: Encode Number where
112117
instance intEncode :: Encode Int where
113118
encode = unsafeToForeign
114119

120+
instance identityEncode :: Encode a => Encode (Identity a) where
121+
encode = encode <<< unwrap
122+
115123
instance arrayEncode :: Encode a => Encode (Array a) where
116124
encode = unsafeToForeign <<< map encode
117125

0 commit comments

Comments
 (0)