Utils.purs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. module LinkedIn.Profile.Utils where
  2. import Prelude
  3. import Control.Alt ((<|>))
  4. import Data.Either (Either(..), hush)
  5. import Data.Foldable (class Foldable, findMap)
  6. import Data.List (List)
  7. import Data.List as L
  8. import Data.Maybe (Maybe(..))
  9. import LinkedIn.DetachedNode (DetachedNode(..))
  10. import LinkedIn.UIElements.Parser (uiElementP)
  11. import LinkedIn.UIElements.Types (UIElement(..))
  12. import Parsing (ParseError(..), initialPos, runParser)
  13. maybeGetInList ::
  14. ∀ a. (UIElement → Maybe a)
  15. -> List (Either ParseError UIElement)
  16. -> Int
  17. -> Maybe a
  18. maybeGetInList extract idx list = L.index idx list >>= hush >>= extract
  19. maybeExtractFromMaybe ∷
  20. ∀ a. (UIElement → Maybe a)
  21. → Maybe (Either ParseError UIElement)
  22. → Maybe a
  23. maybeExtractFromMaybe extract maybeNode = maybeNode >>= hush >>= extract
  24. maybeFindInMaybeNEL ∷
  25. ∀ a f. Foldable f ⇒
  26. (UIElement → Maybe a)
  27. → Maybe (f (Either ParseError UIElement))
  28. → Maybe a
  29. maybeFindInMaybeNEL extract = case _ of
  30. Just nel -> findMap (hush >>> (extract =<< _)) nel
  31. Nothing -> Nothing
  32. -- TODO : should certainly use another type than ParseError here
  33. toUIElement ∷ DetachedNode → Either ParseError UIElement
  34. toUIElement (DetachedElement {content}) = runParser content uiElementP
  35. toUIElement (DetachedComment str) = runParser str uiElementP
  36. toUIElement (DetachedText str) = runParser str uiElementP
  37. toUIElement (DetachedSvgElement {id, dataTestIcon, tag: "svg"}) = case id <|> dataTestIcon of
  38. Just i -> Right (UIIcon i)
  39. Nothing -> Left (ParseError "SVG element could not be identified" initialPos)
  40. toUIElement (DetachedSvgElement _) = Left (ParseError "SVG element could not be identified" initialPos)
  41. toUIElement (DetachedButton {content, role}) = map toButton $ runParser content uiElementP
  42. where toButton ui = UIButton role ui
  43. toUIElement (DetachedA {content, href}) = map toLink $ runParser content uiElementP
  44. where toLink ui = UILink href ui