Utils.purs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. module Test.Utils where
  2. import Prelude
  3. import Control.Monad.Except (runExceptT)
  4. import Data.Date (Month)
  5. import Data.Either (Either(..))
  6. import Data.Int (toNumber)
  7. import Data.Maybe (fromJust)
  8. import Data.Traversable (class Traversable, traverse)
  9. import Effect.Aff (Aff)
  10. import Effect.Class (liftEffect)
  11. import LinkedIn.CanBeQueried (class CanBeQueried)
  12. import LinkedIn.DetachedNode (DetachedNode)
  13. import LinkedIn.Output (runToDetached)
  14. import LinkedIn.QueryRunner (QueryError)
  15. import LinkedIn.UI.Basic.Parser (toYear)
  16. import LinkedIn.UI.Basic.Types (MonthYear(..))
  17. import LinkedIn.UI.Elements.Parser (toUIElement)
  18. import LinkedIn.UI.Elements.Types (UIElement)
  19. import Node.JsDom (jsDomFromFile, jsDomParse)
  20. import Partial.Unsafe (unsafePartial)
  21. import Type.Proxy (Proxy)
  22. import Web.DOM (Document)
  23. toMonthYear' :: Month -> Int -> MonthYear
  24. toMonthYear' m y = MonthYear m y' where
  25. y' = unsafePartial $ fromJust $ toYear $ toNumber y
  26. fromDetachedToUI ∷ ∀ t. Traversable t ⇒ t DetachedNode → Either String (t UIElement)
  27. fromDetachedToUI el = case traverse toUIElement el of
  28. Left _ -> Left "error on conversion to UI element"
  29. Right ui -> Right ui
  30. detachFromFile :: forall t.
  31. Traversable t
  32. => CanBeQueried Document t
  33. => Proxy t
  34. -> String
  35. -> Aff (Either QueryError (t DetachedNode))
  36. detachFromFile proxy filePath = do
  37. dom <- liftEffect $ jsDomFromFile filePath
  38. liftEffect $ runExceptT $ runToDetached proxy dom
  39. detachFromString :: forall t.
  40. Traversable t
  41. => CanBeQueried Document t
  42. => Proxy t
  43. -> String
  44. -> Aff (Either QueryError (t DetachedNode))
  45. detachFromString proxy string = do
  46. dom <- liftEffect $ jsDomParse string
  47. liftEffect $ runExceptT $ runToDetached proxy dom