Skip to content

Commit 09b7320

Browse files
committed
Merge pull request #6 from andreypopp/master
Add keys :: String -> ForeignParser [String]
2 parents 8f8c1c9 + 070a5d5 commit 09b7320

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@
4949

5050
parseJSON :: forall a. (ReadForeign a) => String -> Either String a
5151

52-
prop :: forall a. (ReadForeign a) => String -> ForeignParser a
52+
prop :: forall a. (ReadForeign a) => String -> ForeignParser a
53+
54+
keys :: String -> ForeignParser [String]

src/Data/Foreign.purs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Data.Foreign
66
, ReadForeign
77
, read
88
, prop
9+
, keys
910
) where
1011

1112
import Data.Array
@@ -49,14 +50,34 @@ foreign import readMaybeImpl
4950
readMaybeImpl' :: Foreign -> Maybe Foreign
5051
readMaybeImpl' = runFn3 readMaybeImpl Nothing Just
5152

53+
-- We use == to check for both null and undefined
5254
foreign import readPropImpl
5355
"function readPropImpl(k, obj) { \
54-
\ return obj === undefined ? undefined : obj[k];\
56+
\ return obj == undefined ? undefined : obj[k];\
5557
\}" :: forall a. Fn2 String Foreign Foreign
5658

5759
readPropImpl' :: String -> Foreign -> Foreign
5860
readPropImpl' = runFn2 readPropImpl
5961

62+
-- We use == to check for both null and undefined
63+
foreign import readKeysImpl
64+
"function readKeysImpl(left, right, k, obj) { \
65+
\ if (obj == undefined) { \
66+
\ return left('cannot get a key from an undefined or null value'); \
67+
\ } else if (obj[k] == undefined) { \
68+
\ return left('value is undefined or null'); \
69+
\ } else if (Array.isArray(obj[k])) { \
70+
\ return left('value is an array'); \
71+
\ } else if (typeof obj[k] !== 'object') { \
72+
\ return left('value is not an object'); \
73+
\ } \
74+
\ return right(Object.keys(obj[k])); \
75+
\}"
76+
:: forall a. Fn4 (String -> Either String a) (a -> Either String a) String Foreign (Either String [String])
77+
78+
readKeysImpl' :: String -> Foreign -> Either String [String]
79+
readKeysImpl' = runFn4 readKeysImpl Left Right
80+
6081
foreign import showForeignImpl
6182
"var showForeignImpl = JSON.stringify;" :: Foreign -> String
6283

@@ -124,3 +145,8 @@ prop p = (ForeignParser \x -> Right $ readPropImpl' p x) >>= \x ->
124145
ForeignParser \_ -> case parseForeign read x of
125146
Right result -> Right result
126147
Left err -> Left $ "Error reading property '" ++ p ++ "':\n" ++ err
148+
149+
keys :: String -> ForeignParser [String]
150+
keys p = ForeignParser \x -> case readKeysImpl' p x of
151+
Right result -> Right result
152+
Left err -> Left $ "Error reading object keys of '" ++ p ++ "':\n" ++ err

0 commit comments

Comments
 (0)