Skip to content

Commit c6b78b8

Browse files
committed
Implement more of JArray in the FFI
1 parent 28f1435 commit c6b78b8

File tree

5 files changed

+72
-33
lines changed

5 files changed

+72
-33
lines changed

src/JSON/Array.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const singleton = (x) => [x];

src/JSON/Array.purs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module JSON.Array
22
( fromFoldable
3-
, empty
43
, singleton
54
, index
65
, toUnfoldable
@@ -9,26 +8,22 @@ module JSON.Array
98

109
import Data.Array as Array
1110
import Data.Foldable (class Foldable)
12-
import Data.Maybe (Maybe)
11+
import Data.Function.Uncurried (runFn4)
12+
import Data.Maybe (Maybe(..))
1313
import Data.Unfoldable (class Unfoldable)
14-
import JSON.Internal (JArray, JSON, fromArray, toArray)
15-
import JSON.Internal (JArray, fromArray, toArray) as Exports
14+
import JSON.Internal (JArray, JSON, fromArray, toArray, _index)
15+
import JSON.Internal (JArray, empty, length, fromArray, toArray) as Exports
1616

1717
-- | Creates a `JArray` from a `Foldable` source of `JSON`.
1818
fromFoldable :: forall f. Foldable f => f JSON -> JArray
1919
fromFoldable js = fromArray (Array.fromFoldable js)
2020

21-
-- | An empty `JArray`.
22-
empty :: JArray
23-
empty = fromArray []
24-
2521
-- | Creates a `JArray` with a single entry.
26-
singleton :: JSON -> JArray
27-
singleton j = fromArray [ j ]
22+
foreign import singleton :: JSON -> JArray
2823

2924
-- | Attempts to read a value from the specified index of a `JArray`.
30-
index :: JArray -> Int -> Maybe JSON
31-
index js = Array.index (toArray js)
25+
index :: Int -> JArray -> Maybe JSON
26+
index ix arr = runFn4 _index Nothing Just ix arr
3227

3328
-- | Unfolds a `JArray` into `JSON` items
3429
toUnfoldable :: forall f. Unfoldable f => JArray -> f JSON

src/JSON/Internal.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@ export const _entries = (tuple, obj) =>
4848

4949
export const _lookup = (nothing, just, key, obj) =>
5050
hasOwnProperty.call(obj, key) ? just(obj[key]) : nothing;
51+
52+
export const empty = [];
53+
54+
export const length = (arr) => arr.length;
55+
56+
export const _index = (nothing, just, ix, arr) =>
57+
ix >= 0 && ix < arr.length ? just(arr[ix]) : nothing;
58+
59+
export const _append = (xs, ys) => xs.concat(ys);

src/JSON/Internal.purs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ module JSON.Internal where
22

33
import Prelude
44

5-
import Data.Either (Either)
65
import Data.Function.Uncurried (Fn2, Fn3, Fn4, Fn7, runFn2, runFn7)
7-
import Data.Maybe (Maybe)
86
import Data.Tuple (Tuple(..))
97

108
-- | A type that represents all varieties of JSON value.
@@ -54,17 +52,22 @@ foreign import toArray :: JArray -> Array JSON
5452
-- | Converts an `Array` of `JSON` values into a `JArray`.
5553
foreign import fromArray :: Array JSON -> JArray
5654

55+
-- | An empty `JArray`.
56+
foreign import empty :: JArray
57+
5758
instance Eq JArray where
58-
eq x y = eq (toArray x) (toArray y)
59+
eq xs ys
60+
| length xs == length ys = eq (toArray xs) (toArray ys)
61+
| otherwise = false
5962

6063
instance Ord JArray where
6164
compare x y = compare (toArray x) (toArray y)
6265

6366
instance Semigroup JArray where
64-
append x y = fromArray (append (toArray x) (toArray y))
67+
append xs ys = runFn2 _append xs ys
6568

6669
instance Monoid JArray where
67-
mempty = fromArray []
70+
mempty = empty
6871

6972
-- | A type that represents JSON objects. Similar to the JSON type, this is not a PureScript type,
7073
-- | but represents the underlying representation for JSON objects.
@@ -77,11 +80,12 @@ instance Ord JObject where
7780
compare x y = compare (runFn2 _entries Tuple x) (runFn2 _entries Tuple y)
7881

7982
foreign import _parse
80-
:: Fn3
81-
(forall a b. a -> Either a b)
82-
(forall a b. b -> Either a b)
83+
:: forall f
84+
. Fn3
85+
(forall a b. a -> f a b)
86+
(forall a b. b -> f a b)
8387
String
84-
(Either String JSON)
88+
(f String JSON)
8589

8690
foreign import _fromNumberWithDefault :: Fn2 Int Number JSON
8791

@@ -102,18 +106,37 @@ foreign import _insert :: Fn3 String JSON JObject JObject
102106
foreign import _delete :: Fn2 String JObject JObject
103107

104108
foreign import _fromEntries
105-
:: Fn3
106-
(forall x y. Tuple x y -> x)
107-
(forall x y. Tuple x y -> y)
108-
(Prim.Array (Tuple String JSON))
109+
:: forall f
110+
. Fn3
111+
(forall x y. f x y -> x)
112+
(forall x y. f x y -> y)
113+
(Prim.Array (f String JSON))
109114
JObject
110115

111116
foreign import _entries :: forall c. Fn2 (String -> JSON -> c) JObject (Prim.Array c)
112117

113118
foreign import _lookup
114-
:: Fn4
115-
(forall a. Maybe a)
116-
(forall a. a -> Maybe a)
119+
:: forall f
120+
. Fn4
121+
(forall a. f a)
122+
(forall a. a -> f a)
117123
String
118124
JObject
119-
(Maybe JSON)
125+
(f JSON)
126+
127+
foreign import _index
128+
:: forall f
129+
. Fn4
130+
(forall a. f a)
131+
(forall a. a -> f a)
132+
Int
133+
JArray
134+
(f JSON)
135+
136+
foreign import length :: JArray -> Int
137+
138+
foreign import _append
139+
:: Fn2
140+
JArray
141+
JArray
142+
JArray

test/Main.purs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Test.Main where
22

33
import Prelude
44

5+
import Data.Maybe (Maybe(..))
56
import Data.Tuple (Tuple(..))
67
import Effect (Effect)
78
import Effect.Console (log)
@@ -25,10 +26,20 @@ main = do
2526

2627
log "Check array comparisons"
2728
assertTrue $ J.fromJArray (JA.fromArray []) == J.fromJArray (JA.fromArray [])
28-
assertTrue $ J.fromJArray (JA.fromArray [J.fromInt 1]) == J.fromJArray (JA.fromArray [J.fromInt 1])
29-
assertTrue $ J.fromJArray (JA.fromArray [J.fromInt 1]) < J.fromJArray (JA.fromArray [J.fromInt 2])
29+
assertTrue $ J.fromJArray (JA.fromArray [ J.fromInt 1 ]) == J.fromJArray (JA.fromArray [ J.fromInt 1 ])
30+
assertTrue $ J.fromJArray (JA.fromArray [ J.fromInt 1 ]) < J.fromJArray (JA.fromArray [ J.fromInt 2 ])
3031

3132
log "Check object comparisons"
3233
assertTrue $ JO.empty == JO.empty
33-
assertTrue $ J.fromJObject (JO.fromEntries [Tuple "a" (J.fromInt 1)]) == J.fromJObject (JO.fromEntries [Tuple "a" (J.fromInt 1)])
34-
assertTrue $ J.fromJObject (JO.fromEntries [Tuple "a" (J.fromInt 1)]) < J.fromJObject (JO.fromEntries [Tuple "a" (J.fromInt 2)])
34+
assertTrue $ J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 1) ]) == J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 1) ])
35+
assertTrue $ J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 1) ]) < J.fromJObject (JO.fromEntries [ Tuple "a" (J.fromInt 2) ])
36+
37+
log "Check array index"
38+
assertTrue $ JA.index (-1) (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Nothing
39+
assertTrue $ JA.index 0 (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Just (J.fromInt 0)
40+
assertTrue $ JA.index 1 (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Just (J.fromInt 2)
41+
assertTrue $ JA.index 2 (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Just (J.fromInt 4)
42+
assertTrue $ JA.index 3 (JA.fromArray (J.fromInt <$> [ 0, 2, 4 ])) == Nothing
43+
44+
log "Check array concat"
45+
assertTrue $ JA.fromArray (J.fromInt <$> [ 1, 2 ]) <> JA.fromArray (J.fromInt <$> [ 2, 3 ]) == JA.fromArray (J.fromInt <$> [ 1, 2, 2, 3 ])

0 commit comments

Comments
 (0)