Skip to content

add ?~> and ?:= combinators #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Data/Argonaut.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Data.Argonaut
-- Combinators
( (:=)
, (?:=)
, (~>)
, (?~>)
, (?>>=)
, (.?)
-- Core
Expand Down Expand Up @@ -83,12 +85,16 @@ module Data.Argonaut
-- | Combinators

infix 7 :=
infix 7 ?:=
infix 7 .?
infixr 6 ~>
infixr 6 ?~>
infixl 1 ?>>=

(:=) = Combinators.(:=)
(?:=) = Combinators.(?:=)
(~>) = Combinators.(~>)
(?~>) = Combinators.(?~>)
(?>>=) = Combinators.(?>>=)
(.?) = Combinators.(.?)

Expand All @@ -113,7 +119,7 @@ module Data.Argonaut
downField = JCursor.downField
downIndex = JCursor.downIndex
insideOut = JCursor.insideOut
toPrims = JCursor.toPrims
toPrims = JCursor.toPrims

-- Prims
primNull = JCursor.primNull
Expand Down
9 changes: 9 additions & 0 deletions src/Data/Argonaut/Combinators.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Data.Argonaut.Combinators
( (:=)
, (?:=)
, (~>)
, (?~>)
, (?>>=)
, (.?)
) where
Expand Down Expand Up @@ -29,6 +31,13 @@ module Data.Argonaut.Combinators
(:=) :: forall a. (EncodeJson a) => String -> a -> JAssoc
(:=) k v = Tuple k $ encodeJson v

(?:=) :: forall a. (EncodeJson a) => String -> Maybe a -> Maybe JAssoc
(?:=) k v = ((:=) k) <<< encodeJson <$> v

(?~>) :: forall a. (EncodeJson a) => Maybe JAssoc -> a -> Json
(?~>) (Just kv) x = kv ~> x
(?~>) Nothing x = encodeJson x

(~>) :: forall a. (EncodeJson a) => JAssoc -> a -> Json
(~>) (Tuple k v) a = foldJsonObject (jsonSingletonObject k v) (M.insert k v >>> fromObject) (encodeJson a)

Expand Down
6 changes: 6 additions & 0 deletions src/Data/Argonaut/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
(:=) :: forall a. (EncodeJson a) => String -> a -> JAssoc


(?:=) :: forall a. (EncodeJson a) => String -> Maybe a -> Maybe JAssoc


(?>>=) :: forall a b. Maybe a -> String -> Either String a


(?~>) :: forall a. (EncodeJson a) => Maybe JAssoc -> a -> Json


(~>) :: forall a. (EncodeJson a) => JAssoc -> a -> Json


Expand Down