浏览代码

Add Foldable + Traversable instances to allow call to toDetached

jherve 2 年之前
父节点
当前提交
cf0ff5cc59
共有 2 个文件被更改,包括 43 次插入3 次删除
  1. 14 3
      src/Content.purs
  2. 29 0
      src/LinkedIn/ArtDecoCardAlt.purs

+ 14 - 3
src/Content.purs

@@ -8,17 +8,19 @@ import Data.Either (Either(..))
 import Data.List.NonEmpty (NonEmptyList)
 import Data.List.NonEmpty (NonEmptyList)
 import Data.List.NonEmpty as NEL
 import Data.List.NonEmpty as NEL
 import Data.Maybe (Maybe(..))
 import Data.Maybe (Maybe(..))
+import Data.Traversable (traverse)
 import Effect (Effect)
 import Effect (Effect)
 import Effect.Class.Console (logShow)
 import Effect.Class.Console (logShow)
 import Effect.Console (log)
 import Effect.Console (log)
 import LinkedIn.ArtDecoCard (parseArtDecoCard)
 import LinkedIn.ArtDecoCard (parseArtDecoCard)
-import LinkedIn.ArtDecoCardAlt (queryArtDecoCardAlt)
+import LinkedIn.ArtDecoCardAlt (ArtDecoCardAltElement, queryArtDecoCardAlt)
 import LinkedIn.ArtDecoTab (parseArtDecoTab)
 import LinkedIn.ArtDecoTab (parseArtDecoTab)
 import LinkedIn.JobsUnifiedTopCard (parseJobsUnifiedTopCardElement)
 import LinkedIn.JobsUnifiedTopCard (parseJobsUnifiedTopCardElement)
 import LinkedIn.Profile.Project as PP
 import LinkedIn.Profile.Project as PP
 import LinkedIn.Profile.Skill as PS
 import LinkedIn.Profile.Skill as PS
 import LinkedIn.Profile.Utils (toUIElement)
 import LinkedIn.Profile.Utils (toUIElement)
 import LinkedIn.Profile.WorkExperience as PWE
 import LinkedIn.Profile.WorkExperience as PWE
+import Web.DOM (Node)
 import Yoga.Tree (Tree, showTree)
 import Yoga.Tree (Tree, showTree)
 
 
 main :: Effect Unit
 main :: Effect Unit
@@ -39,16 +41,25 @@ main = do
   case artDecoCards of
   case artDecoCards of
     Nothing -> log "nothing"
     Nothing -> log "nothing"
     Just l -> do
     Just l -> do
-      queried <- (\(LinkedInUIElement _ n) -> queryArtDecoCardAlt n) $ NEL.head l
       parsed <- (\(LinkedInUIElement _ n) -> parseArtDecoCard n) $ NEL.head l
       parsed <- (\(LinkedInUIElement _ n) -> parseArtDecoCard n) $ NEL.head l
       logShow parsed
       logShow parsed
-      logShow queried
       case parsed of
       case parsed of
         Left l -> logShow l
         Left l -> logShow l
         Right p -> do
         Right p -> do
           logShow $ toUIElement <$> p
           logShow $ toUIElement <$> p
           logShow $ PWE.fromUI p
           logShow $ PWE.fromUI p
           logShow $ PP.fromUI p
           logShow $ PP.fromUI p
+
+  case artDecoCards of
+    Nothing -> log "nothing"
+    Just l -> do
+      queried <- (\(LinkedInUIElement _ n) -> queryArtDecoCardAlt n) $ NEL.head l
+      case queried of
+        Left l -> logShow l
+        Right p -> do
+          detached <- traverse toDetached p :: Effect (ArtDecoCardAltElement DetachedNode)
+          logShow detached
+
   case artDecoTabs of
   case artDecoTabs of
     Nothing -> log "nothing"
     Nothing -> log "nothing"
     Just l -> do
     Just l -> do

+ 29 - 0
src/LinkedIn/ArtDecoCardAlt.purs

@@ -5,11 +5,13 @@ import Prelude
 
 
 import Control.Monad.Writer (Writer, tell)
 import Control.Monad.Writer (Writer, tell)
 import Data.Either (Either(..), hush, note)
 import Data.Either (Either(..), hush, note)
+import Data.Foldable (class Foldable, foldMap, foldlDefault, foldrDefault)
 import Data.Generic.Rep (class Generic)
 import Data.Generic.Rep (class Generic)
 import Data.List (List)
 import Data.List (List)
 import Data.List.Types (NonEmptyList)
 import Data.List.Types (NonEmptyList)
 import Data.Maybe (Maybe(..))
 import Data.Maybe (Maybe(..))
 import Data.Show.Generic (genericShow)
 import Data.Show.Generic (genericShow)
+import Data.Traversable (class Traversable, sequence, traverseDefault)
 import Effect (Effect)
 import Effect (Effect)
 import Effect.Class.Console (log, logShow)
 import Effect.Class.Console (log, logShow)
 import LinkedIn (DetachedNode)
 import LinkedIn (DetachedNode)
@@ -34,6 +36,20 @@ instance Functor ArtDecoCenterHeaderAlt where
   map f (ArtDecoCenterHeaderAlt {bold, normal}) =
   map f (ArtDecoCenterHeaderAlt {bold, normal}) =
     ArtDecoCenterHeaderAlt ({bold: f bold, normal: map f normal})
     ArtDecoCenterHeaderAlt ({bold: f bold, normal: map f normal})
 
 
+instance Foldable ArtDecoCenterHeaderAlt where
+  foldMap f (ArtDecoCenterHeaderAlt {bold, normal}) = mempty <> (f bold) <> (foldMap f normal)
+
+  foldl = \x -> foldlDefault x
+  foldr = \x -> foldrDefault x
+
+instance Traversable ArtDecoCenterHeaderAlt where
+  sequence (ArtDecoCenterHeaderAlt {bold, normal}) = ado
+    b <- bold
+    n <- sequence normal
+  in ArtDecoCenterHeaderAlt {bold: b, normal: n}
+
+  traverse = \x -> traverseDefault x
+
 data ArtDecoCardAltElement a = ArtDecoCardAltElement {
 data ArtDecoCardAltElement a = ArtDecoCardAltElement {
   pvs_entity :: ArtDecoCenterHeaderAlt a
   pvs_entity :: ArtDecoCenterHeaderAlt a
 }
 }
@@ -50,6 +66,19 @@ instance Functor ArtDecoCardAltElement where
   map f (ArtDecoCardAltElement {pvs_entity}) =
   map f (ArtDecoCardAltElement {pvs_entity}) =
     ArtDecoCardAltElement ({pvs_entity: map f pvs_entity})
     ArtDecoCardAltElement ({pvs_entity: map f pvs_entity})
 
 
+instance Foldable ArtDecoCardAltElement where
+  foldMap f (ArtDecoCardAltElement {pvs_entity}) = mempty <> (foldMap f pvs_entity)
+
+  foldl = \x -> foldlDefault x
+  foldr = \x -> foldrDefault x
+
+instance Traversable ArtDecoCardAltElement where
+  sequence (ArtDecoCardAltElement {pvs_entity}) = ado
+    p <- sequence pvs_entity
+  in ArtDecoCardAltElement {pvs_entity: p}
+
+  traverse = \x -> traverseDefault x
+
 queryArtDecoCardAlt :: Node → Effect (Either QueryError (ArtDecoCardAltElement Node))
 queryArtDecoCardAlt :: Node → Effect (Either QueryError (ArtDecoCardAltElement Node))
 queryArtDecoCardAlt n = runExceptT do
 queryArtDecoCardAlt n = runExceptT do
   pvs <- runOne ":scope div.pvs-entity--padded" n
   pvs <- runOne ":scope div.pvs-entity--padded" n