module Haste.Foreign (
ToAny (..), FromAny (..), JSAny,
Opaque, toOpaque, fromOpaque,
nullValue, toObject, has, get, index,
getMaybe, hasAll, lookupAny,
FFI, JSFunc,
ffi, constant, export
#if __GLASGOW_HASKELL__ >= 710
, safe_ffi, StaticPtr
#endif
) where
import Haste.Prim.Foreign
import Haste.Prim (JSString)
import qualified Haste.JSString as J
import Control.Monad (foldM)
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative ((<$>), pure)
#endif
getMaybe :: (FromAny a) => JSAny -> JSString -> IO (Maybe a)
getMaybe a k = do exists <- has a k
if exists then Just <$> get a k
else pure Nothing
hasAll :: JSAny -> [JSString] -> IO Bool
hasAll a ks = and <$> mapM (has a) ks
lookupAny :: JSAny -> JSString -> IO (Maybe JSAny)
lookupAny root i = foldM hasGet (Just root) $ J.match dotsplit i
where hasGet Nothing _ = pure Nothing
hasGet (Just parent) ident = do h <- has parent ident
if h then Just <$> get parent ident
else pure Nothing
dotsplit = J.regex "[^.]+" "g"