haskelldomghcjsghcjs-dom

Filter [Node] to [Element] with ghcjs-dom


I am using ghcjs-dom. How can I filter the elements out of an array of nodes?

import GHCJS.DOM.Types

nodesToElements :: [Node] -> [Element]
nodesToElements = ??

or

nodesToElements :: [Node] -> IO [Element]
nodesToElements = ??

Solution

  • import GHCJS.DOM.Types
    import Data.Maybe (mapMaybe)
    
    nodeToElement :: Node -> Maybe Element
    nodeToElement node = if node `isA` gTypeElement
                         then Just $ castToElement node
                         else Nothing
    
    nodesToElements :: [Node] -> [Element]
    nodesToElements = mapMaybe nodeToElement