Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit b820caa

Browse files
committed
More comments, tests, fix bug in setKey
1 parent fa43bc8 commit b820caa

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

src/Data/Json/Extended/Cursor.purs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,59 @@ set cur x v = case lmap unroll <$> peel cur of
9191
Just (Tuple (AtKey k _) path) → maybe v (setKey k x) $ get path v
9292
Just (Tuple (AtIndex i _) path) → maybe v (setIndex i x) $ get path v
9393

94+
-- | Attempts to lookup a key in an EJson Map, returning the associated value
95+
-- | if the key exists and the value is a Map.
96+
-- |
97+
-- | ``` purescript
98+
-- | getKey (EJ.string "foo") (EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "foo" "bar"]) == Just (EJ.string "bar")
99+
-- | getKey (EJ.string "foo") (EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "key" "value"]) == Nothing
100+
-- | getKey (EJ.string "foo") (EJ.boolean false) == Nothing
101+
-- | ```
94102
getKey EJ.EJson EJ.EJson Maybe EJ.EJson
95103
getKey k v = case EJ.head v of
96104
EJ.Map fields → EJ.EJson <$> lookup (EJ.getEJson k) fields
97105
_ → Nothing
98106

107+
-- | For a given key, attempts to set a new value for it in an EJson Map. If the
108+
-- | value is not a Map, or the key does not already exist, the original value
109+
-- | is returned.
110+
-- |
111+
-- | ``` purescript
112+
-- | let map = EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "foo" "bar"]
113+
-- | setKey (EJ.string "foo") (EJ.boolean true) map == EJ.map' (SM.fromFoldable [Tuple "foo" (EJ.boolean true)])
114+
-- | setKey (EJ.string "bar") (EJ.boolean true) map == map
115+
-- | setKey (EJ.string "foo") (EJ.boolean true) (EJ.string "not-a-map") == EJ.string "not-a-map"
116+
-- | ```
99117
setKey EJ.EJson EJ.EJson EJ.EJson EJ.EJson
100-
setKey k (EJ.EJson x) v = case EJ.head v of
118+
setKey (EJ.EJson k) (EJ.EJson x) v = case EJ.head v of
101119
EJ.Map fields →
102120
EJ.EJson <<< roll <<< EJ.Map $ map
103-
(\(kv@(Tuple k v)) → if k == k then Tuple k x else kv) fields
121+
(\(kv@(Tuple k' v)) → if k == k' then Tuple k x else kv) fields
104122
_ → v
105123

124+
-- | Attempts to lookup an index in an EJson Array, returning the associated
125+
-- | value if there is an item at that index, and the value is an Array.
126+
-- |
127+
-- | ``` purescript
128+
-- | getIndex 0 (EJ.array $ EJ.string <$> ["foo"]) == Just (EJ.string "foo")
129+
-- | getIndex 1 (EJ.array $ EJ.string <$> ["foo"]) == Nothing
130+
-- | getIndex 0 (EJ.boolean false) == Nothing
131+
-- | ```
106132
getIndex Int EJ.EJson Maybe EJ.EJson
107133
getIndex i v = case EJ.head v of
108134
EJ.Array items → EJ.EJson <$> A.index items i
109135
_ → Nothing
110136

137+
-- | For a given index, attempts to set a new value for it in an EJson Array. If
138+
-- | the value is not a Array, or the index does not already exist, the original
139+
-- | value is returned.
140+
-- |
141+
-- | ``` purescript
142+
-- | let array = EJ.array $ EJ.string <$> ["foo"]
143+
-- | setIndex 0 (EJ.boolean true) array == EJ.array [EJ.boolean true]
144+
-- | setIndex 1 (EJ.boolean true) array == array
145+
-- | setIndex 0 (EJ.boolean true) (EJ.string "not-an-array") == EJ.string "not-an-array"
146+
-- | ```
111147
setIndex Int EJ.EJson EJ.EJson EJ.EJson
112148
setIndex i (EJ.EJson x) v = case EJ.head v of
113149
EJ.Array items →

test/Main.purs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import Control.Monad.Eff.Console (CONSOLE)
99
import Data.Argonaut.Decode (decodeJson)
1010
import Data.Argonaut.Encode (encodeJson)
1111
import Data.Either as E
12+
import Data.Maybe
13+
import Data.StrMap as SM
14+
import Data.Tuple
1215
import Data.Json.Extended (EJson, arbitraryJsonEncodableEJsonOfSize, arbitraryEJsonOfSize, renderEJson, parseEJson)
16+
import Data.Json.Extended as EJ
17+
import Data.Json.Extended.Cursor as EJC
1318

1419
import Text.Parsing.Parser as P
1520

@@ -45,7 +50,61 @@ testRenderParse =
4550
E.Right y → x == y SC.<?> "Mismatch:\n" <> show x <> "\n" <> show y
4651
E.Left err → SC.Failed $ "Parse error: " <> show err <> " when parsing:\n\n " <> renderEJson x <> "\n\n"
4752

53+
testCursorExamples Eff TestEffects Unit
54+
testCursorExamples = do
55+
assertEq
56+
(EJC.peel (EJC.atKey (EJ.string "foo") $ EJC.atIndex 0 EJC.all))
57+
(Just (Tuple (EJC.atKey (EJ.string "foo") EJC.all) (EJC.atIndex 0 EJC.all)))
58+
assertEq
59+
(EJC.peel (EJC.atIndex 0 EJC.all))
60+
(Just (Tuple (EJC.atIndex 0 EJC.all) EJC.all))
61+
assertEq
62+
(EJC.peel EJC.all)
63+
Nothing
64+
assertEq
65+
(EJC.getKey (EJ.string "foo") (EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "foo" "bar"]))
66+
(Just (EJ.string "bar"))
67+
assertEq
68+
(EJC.getKey (EJ.string "foo") (EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "key" "value"]))
69+
Nothing
70+
assertEq
71+
(EJC.getKey (EJ.string "foo") (EJ.boolean false))
72+
Nothing
73+
assertEq
74+
(EJC.getIndex 0 (EJ.array $ EJ.string <$> ["foo"]))
75+
(Just (EJ.string "foo"))
76+
assertEq
77+
(EJC.getIndex 1 (EJ.array $ EJ.string <$> ["foo"]))
78+
Nothing
79+
assertEq
80+
(EJC.getIndex 0 (EJ.boolean false))
81+
Nothing
82+
let map = EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "foo" "bar"]
83+
assertEq
84+
(EJC.setKey (EJ.string "foo") (EJ.boolean true) map)
85+
(EJ.map' (SM.fromFoldable [Tuple "foo" (EJ.boolean true)]))
86+
assertEq
87+
(EJC.setKey (EJ.string "bar") (EJ.boolean true) map)
88+
map
89+
assertEq
90+
(EJC.setKey (EJ.string "foo") (EJ.boolean true) (EJ.string "not-a-map"))
91+
(EJ.string "not-a-map")
92+
let array = EJ.array $ EJ.string <$> ["foo"]
93+
assertEq
94+
(EJC.setIndex 0 (EJ.boolean true) array)
95+
(EJ.array [EJ.boolean true])
96+
assertEq
97+
(EJC.setIndex 1 (EJ.boolean true) array)
98+
array
99+
assertEq
100+
(EJC.setIndex 0 (EJ.boolean true) (EJ.string "not-an-array"))
101+
(EJ.string "not-an-array")
102+
where
103+
assertEq a. (Show a, Eq a) a a Eff TestEffects Unit
104+
assertEq x y = SC.assert $ SC.assertEq x y
105+
48106
main :: Eff TestEffects Unit
49107
main = do
50108
testJsonSerialization
51109
testRenderParse
110+
testCursorExamples

0 commit comments

Comments
 (0)