Utils.purs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.Extractible (class Extractible)
  14. import LinkedIn.Output (Output, OutputError, run, runToDetached)
  15. import LinkedIn.QueryRunner (QueryError)
  16. import LinkedIn.UI.Basic.Parser (toYear)
  17. import LinkedIn.UI.Basic.Types (MonthYear(..))
  18. import LinkedIn.UI.Elements.Parser (toUIElement)
  19. import LinkedIn.UI.Elements.Types (UIElement)
  20. import Node.JsDom (jsDomFromFile, jsDomParse)
  21. import Partial.Unsafe (unsafePartial)
  22. import Type.Proxy (Proxy)
  23. import Web.DOM (Document)
  24. toMonthYear' :: Month -> Int -> MonthYear
  25. toMonthYear' m y = MonthYear m y' where
  26. y' = unsafePartial $ fromJust $ toYear $ toNumber y
  27. fromDetachedToUI ∷ ∀ t. Traversable t ⇒ t DetachedNode → Either String (t UIElement)
  28. fromDetachedToUI el = case traverse toUIElement el of
  29. Left _ -> Left "error on conversion to UI element"
  30. Right ui -> Right ui
  31. detachFromFile :: forall t.
  32. Traversable t
  33. => CanBeQueried Document t
  34. => Proxy t
  35. -> String
  36. -> Aff (Either QueryError (t DetachedNode))
  37. detachFromFile proxy filePath = do
  38. dom <- liftEffect $ jsDomFromFile filePath
  39. liftEffect $ runExceptT $ runToDetached proxy dom
  40. detachFromString :: forall t.
  41. Traversable t
  42. => CanBeQueried Document t
  43. => Proxy t
  44. -> String
  45. -> Aff (Either QueryError (t DetachedNode))
  46. detachFromString proxy string = do
  47. dom <- liftEffect $ jsDomParse string
  48. liftEffect $ runExceptT $ runToDetached proxy dom
  49. getOutputFromFile :: forall t.
  50. Traversable t
  51. => Extractible t
  52. => CanBeQueried Document t
  53. => Proxy t
  54. -> String
  55. -> Aff (Either OutputError Output)
  56. getOutputFromFile proxy string = do
  57. dom <- liftEffect $ jsDomFromFile string
  58. liftEffect $ runExceptT $ run proxy dom