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

Commit fa43bc8

Browse files
committed
Add some comments
1 parent 26727a6 commit fa43bc8

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/Data/Json/Extended/Cursor.purs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ import Data.Tuple (Tuple(..), lookup)
1414

1515
import Matryoshka (Algebra, cata)
1616

17+
-- | A cursor to a location in an EJson value.
18+
-- |
19+
-- | The functions operating on cursor are "depth first", that is to say:
20+
-- | ``` purescript
21+
-- | atKey (EJ.string "foo") $ atIndex 0 $ atKey (EJ.string "bar") all
22+
-- | ```
23+
-- | Is the path:
24+
-- | ```
25+
-- | <value>.bar[0].foo
26+
-- | ```
27+
type Cursor = Mu CursorF
28+
29+
all Cursor
30+
all = roll All
31+
32+
atKey EJ.EJson Cursor Cursor
33+
atKey k = roll <<< AtKey k
34+
35+
atIndex Int Cursor Cursor
36+
atIndex i = roll <<< AtIndex i
37+
38+
-- | The possible steps in a cursor.
1739
data CursorF a
1840
= All
1941
| AtKey EJson a
@@ -35,23 +57,22 @@ instance showCursorF ∷ Show a => Show (CursorF a) where
3557
AtKey k a → "(AtKey " <> show k <> " " <> show a <> ")"
3658
AtIndex i a → "(AtIndex " <> show i <> " " <> show a <> ")"
3759

38-
type Cursor = Mu CursorF
39-
40-
all Cursor
41-
all = roll All
42-
43-
atKey EJ.EJson Cursor Cursor
44-
atKey k = roll <<< AtKey k
45-
46-
atIndex Int Cursor Cursor
47-
atIndex i = roll <<< AtIndex i
48-
60+
-- | Peels off one layer of a cursor, if possible. The resulting tuple contains
61+
-- | the current step (made relative), and the remainder of the cursor.
62+
-- |
63+
-- | ``` purescript
64+
-- | peel (atKey (EJ.string "foo") $ atIndex 0 all) == Just (Tuple (atKey (EJ.string "foo") all) (atIndex 0 all))
65+
-- | peel (atIndex 0 all) == Just (Tuple (atIndex 0 all) all)
66+
-- | peel all == Nothing
67+
-- | ```
4968
peel Cursor Maybe (Tuple Cursor Cursor)
5069
peel c = case unroll c of
5170
AllNothing
5271
AtKey k rest → Just $ Tuple (atKey k all) rest
5372
AtIndex i rest → Just $ Tuple (atIndex i all) rest
5473

74+
-- | Takes a cursor and attempts to read from an EJson value, producing the
75+
-- | value the cursor points to, if it exists.
5576
get Cursor EJson Maybe EJson
5677
get = cata go
5778
where
@@ -61,12 +82,14 @@ get = cata go
6182
AtKey k prior → getKey k <=< prior
6283
AtIndex i prior → getIndex i <=< prior
6384

64-
set Cursor EJson EJson Maybe EJson
85+
-- | Takes a cursor and attempts to set an EJson value within a larger EJson
86+
-- | value if the value the cursor points at exists.
87+
set Cursor EJson EJson EJson
6588
set cur x v = case lmap unroll <$> peel cur of
66-
NothingJust x
67-
Just (Tuple All _) → Just x
68-
Just (Tuple (AtKey k _) path) → setKey k x <$> get path v
69-
Just (Tuple (AtIndex i _) path) → setIndex i x <$> get path v
89+
Nothing → x
90+
Just (Tuple All _) → x
91+
Just (Tuple (AtKey k _) path) → maybe v (setKey k x) $ get path v
92+
Just (Tuple (AtIndex i _) path) → maybe v (setIndex i x) $ get path v
7093

7194
getKey EJ.EJson EJ.EJson Maybe EJ.EJson
7295
getKey k v = case EJ.head v of

0 commit comments

Comments
 (0)