diff --git a/src/JSON.purs b/src/JSON.purs index c911b6e..7d44e6c 100644 --- a/src/JSON.purs +++ b/src/JSON.purs @@ -30,7 +30,7 @@ import Data.Function.Uncurried (runFn2, runFn3, runFn7) import Data.Int as Int import Data.Maybe (Maybe(..)) import JSON.Internal (JArray, JObject, JSON) -import JSON.Internal (JArray, JObject, JSON) as Exports +import JSON.Internal (JArray, JObject, JSON, isNull) as Exports import JSON.Internal as Internal -- | Attempts to parse a string as a JSON value. If parsing fails, an error message detailing the diff --git a/src/JSON/Internal.js b/src/JSON/Internal.js index 8c4e161..32c0c41 100644 --- a/src/JSON/Internal.js +++ b/src/JSON/Internal.js @@ -57,3 +57,5 @@ export const _index = (nothing, just, ix, arr) => ix >= 0 && ix < arr.length ? just(arr[ix]) : nothing; export const _append = (xs, ys) => xs.concat(ys); + +export const isNull = (json) => json == null; diff --git a/src/JSON/Internal.purs b/src/JSON/Internal.purs index 503cfbc..0eb7bba 100644 --- a/src/JSON/Internal.purs +++ b/src/JSON/Internal.purs @@ -140,3 +140,5 @@ foreign import _append JArray JArray JArray + +foreign import isNull :: JSON -> Boolean diff --git a/test/Main.purs b/test/Main.purs index d3f57e0..dc5288d 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -35,6 +35,10 @@ main = do assertTrue $ J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 1) ]) == J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 1) ]) assertTrue $ J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 1) ]) < J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 2) ]) + log "Check isNull" + assertTrue $ J.isNull J.null + assertTrue $ not $ J.isNull (J.fromInt 1) + log "Check array index" assertTrue $ JA.index (-1) (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Nothing assertTrue $ JA.index 0 (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Just (J.fromInt 0)