@@ -6,6 +6,7 @@ module Data.Foreign
66 , ReadForeign
77 , read
88 , prop
9+ , keys
910 ) where
1011
1112import Data.Array
@@ -49,14 +50,34 @@ foreign import readMaybeImpl
4950readMaybeImpl' :: Foreign -> Maybe Foreign
5051readMaybeImpl' = runFn3 readMaybeImpl Nothing Just
5152
53+ -- We use == to check for both null and undefined
5254foreign 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
5759readPropImpl' :: String -> Foreign -> Foreign
5860readPropImpl' = 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+
6081foreign 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