@@ -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+ -- | ```
94102getKey ∷ EJ.EJson → EJ.EJson → Maybe EJ.EJson
95103getKey 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+ -- | ```
99117setKey ∷ 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+ -- | ```
106132getIndex ∷ Int → EJ.EJson → Maybe EJ.EJson
107133getIndex 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+ -- | ```
111147setIndex ∷ Int → EJ.EJson → EJ.EJson → EJ.EJson
112148setIndex i (EJ.EJson x) v = case EJ .head v of
113149 EJ.Array items →
0 commit comments