|
3 | 3 |
|
4 | 4 | module Data.Foreign.Index |
5 | 5 | ( class Index |
| 6 | + , class Indexable |
6 | 7 | , readProp |
7 | 8 | , readIndex |
8 | 9 | , ix, (!) |
9 | | - , ixKleisli, (!!) |
| 10 | + , index |
10 | 11 | , hasProperty |
11 | 12 | , hasOwnProperty |
12 | 13 | , errorAt |
13 | 14 | ) where |
14 | 15 |
|
15 | 16 | import Prelude |
16 | 17 |
|
| 18 | +import Control.Monad.Except.Trans (ExceptT) |
| 19 | + |
17 | 20 | import Data.Foreign (Foreign, F, ForeignError(..), typeOf, isUndefined, isNull, fail) |
18 | 21 | import Data.Function.Uncurried (Fn2, runFn2, Fn4, runFn4) |
| 22 | +import Data.Identity (Identity) |
| 23 | +import Data.List.NonEmpty (NonEmptyList) |
19 | 24 |
|
20 | 25 | -- | This type class identifies types that act like _property indices_. |
21 | 26 | -- | |
22 | 27 | -- | The canonical instances are for `String`s and `Int`s. |
23 | 28 | class Index i where |
24 | | - ix :: Foreign -> i -> F Foreign |
| 29 | + index :: Foreign -> i -> F Foreign |
25 | 30 | hasProperty :: i -> Foreign -> Boolean |
26 | 31 | hasOwnProperty :: i -> Foreign -> Boolean |
27 | 32 | errorAt :: i -> ForeignError -> ForeignError |
28 | 33 |
|
29 | | -infixl 9 ix as ! |
30 | | - |
31 | | -ixKleisli :: forall i. Index i => F Foreign -> i -> F Foreign |
32 | | -ixKleisli f i = f >>= (_ ! i) |
| 34 | +class Indexable a where |
| 35 | + ix :: forall i. Index i => a -> i -> F Foreign |
33 | 36 |
|
34 | | -infixl 9 ixKleisli as !! |
| 37 | +infixl 9 ix as ! |
35 | 38 |
|
36 | 39 | foreign import unsafeReadPropImpl :: forall r k. Fn4 r (Foreign -> r) k Foreign r |
37 | 40 |
|
@@ -64,13 +67,19 @@ hasPropertyImpl p value | typeOf value == "object" || typeOf value == "function" |
64 | 67 | hasPropertyImpl _ value = false |
65 | 68 |
|
66 | 69 | instance indexString :: Index String where |
67 | | - ix = flip readProp |
| 70 | + index = flip readProp |
68 | 71 | hasProperty = hasPropertyImpl |
69 | 72 | hasOwnProperty = hasOwnPropertyImpl |
70 | 73 | errorAt = ErrorAtProperty |
71 | 74 |
|
72 | 75 | instance indexInt :: Index Int where |
73 | | - ix = flip readIndex |
| 76 | + index = flip readIndex |
74 | 77 | hasProperty = hasPropertyImpl |
75 | 78 | hasOwnProperty = hasOwnPropertyImpl |
76 | 79 | errorAt = ErrorAtIndex |
| 80 | + |
| 81 | +instance indexableForeign :: Indexable Foreign where |
| 82 | + ix = index |
| 83 | + |
| 84 | +instance indexableExceptT :: Indexable (ExceptT (NonEmptyList ForeignError) Identity Foreign) where |
| 85 | + ix f i = flip index i =<< f |
0 commit comments