Module

Data.Json.Extended.Cursor

Package
purescript-ejson
Repository
slamdata/purescript-ejson

#Cursor Source

type Cursor = Mu CursorF

A cursor to a location in an EJson value.

The functions operating on cursor are "depth first", that is to say:

atKey (EJ.string "foo") $ atIndex 0 $ atKey (EJ.string "bar") all

Is the path:

<value>.bar[0].foo

#atIndex Source

#CursorF Source

data CursorF a

The possible steps in a cursor.

Constructors

Instances

#renderEJsonCursor Source

#peel Source

peel :: Cursor -> Maybe (Tuple Cursor Cursor)

Peels off one layer of a cursor, if possible. The resulting tuple contains the current step (made relative), and the remainder of the cursor.

peel (atKey (EJ.string "foo") $ atIndex 0 all) == Just (Tuple (atKey (EJ.string "foo") all) (atIndex 0 all))
peel (atIndex 0 all) == Just (Tuple (atIndex 0 all) all)
peel all == Nothing

#get Source

get :: Cursor -> EJson -> Maybe EJson

Takes a cursor and attempts to read from an EJson value, producing the value the cursor points to, if it exists.

#set Source

set :: Cursor -> EJson -> EJson -> EJson

Takes a cursor and attempts to set an EJson value within a larger EJson value if the value the cursor points at exists.

#getKey Source

getKey :: EJson -> EJson -> Maybe EJson

Attempts to lookup a key in an EJson Map, returning the associated value if the key exists and the value is a Map.

getKey (EJ.string "foo") (EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "foo" "bar"]) == Just (EJ.string "bar")
getKey (EJ.string "foo") (EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "key" "value"]) == Nothing
getKey (EJ.string "foo") (EJ.boolean false) == Nothing

#setKey Source

setKey :: EJson -> EJson -> EJson -> EJson

For a given key, attempts to set a new value for it in an EJson Map. If the value is not a Map, or the key does not already exist, the original value is returned.

let map = EJ.map' $ EJ.string <$> SM.fromFoldable [Tuple "foo" "bar"]
setKey (EJ.string "foo") (EJ.boolean true) map == EJ.map' (SM.fromFoldable [Tuple "foo" (EJ.boolean true)])
setKey (EJ.string "bar") (EJ.boolean true) map == map
setKey (EJ.string "foo") (EJ.boolean true) (EJ.string "not-a-map") == EJ.string "not-a-map"

#getIndex Source

getIndex :: Int -> EJson -> Maybe EJson

Attempts to lookup an index in an EJson Array, returning the associated value if there is an item at that index, and the value is an Array.

getIndex 0 (EJ.array $ EJ.string <$> ["foo"]) == Just (EJ.string "foo")
getIndex 1 (EJ.array $ EJ.string <$> ["foo"]) == Nothing
getIndex 0 (EJ.boolean false) == Nothing

#setIndex Source

setIndex :: Int -> EJson -> EJson -> EJson

For a given index, attempts to set a new value for it in an EJson Array. If the value is not a Array, or the index does not already exist, the original value is returned.

let array = EJ.array $ EJ.string <$> ["foo"]
setIndex 0 (EJ.boolean true) array == EJ.array [EJ.boolean true]
setIndex 1 (EJ.boolean true) array == array
setIndex 0 (EJ.boolean true) (EJ.string "not-an-array") == EJ.string "not-an-array"