Utils.purs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, detachNodes, run)
  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 OutputError (t DetachedNode))
  36. detachFromFile proxy filePath = do
  37. dom <- liftEffect $ jsDomFromFile filePath
  38. liftEffect $ runExceptT $ detachNodes proxy dom
  39. detachFromString :: forall t.
  40. Traversable t
  41. => CanBeQueried Document t
  42. => Proxy t
  43. -> String
  44. -> Aff (Either OutputError (t DetachedNode))
  45. detachFromString proxy string = do
  46. dom <- liftEffect $ jsDomParse string
  47. liftEffect $ runExceptT $ detachNodes proxy dom
  48. getOutputFromFile :: forall t.
  49. Traversable t
  50. => Extractible t
  51. => CanBeQueried Document t
  52. => Proxy t
  53. -> String
  54. -> Aff (Either OutputError Output)
  55. getOutputFromFile proxy string = do
  56. dom <- liftEffect $ jsDomFromFile string
  57. liftEffect $ runExceptT $ run proxy dom