瀏覽代碼

Calls to chooseOne/chooseOne3 can in fact be replaced by <|>

jherve 1 年之前
父節點
當前提交
bb5c8a454d
共有 2 個文件被更改,包括 11 次插入30 次删除
  1. 3 25
      src/LinkedIn/QueryRunner.purs
  2. 8 5
      src/LinkedIn/UI/Components/JobsUnifiedTopCard.purs

+ 3 - 25
src/LinkedIn/QueryRunner.purs

@@ -3,7 +3,7 @@ module LinkedIn.QueryRunner where
 import Prelude
 
 import Control.Alt ((<|>))
-import Control.Monad.Except (ExceptT(..), mapExceptT, runExceptT, throwError)
+import Control.Monad.Except (ExceptT(..), mapExceptT, runExceptT)
 import Data.Argonaut.Encode (class EncodeJson)
 import Data.Argonaut.Encode.Generic (genericEncodeJson)
 import Data.Array as A
@@ -31,6 +31,8 @@ derive instance Generic QueryError _
 derive instance Eq QueryError
 instance Show QueryError where
   show = genericShow
+instance Semigroup QueryError where
+  append a _ = a
 
 -- QueryRunner' is a generalization of QueryRunner for all Queryable instances (e.g. Document)
 type QueryRunner' q a = q → ExceptT QueryError Effect a
@@ -82,27 +84,3 @@ subQueryMany query selector n = traverse query =<< queryAll selector n
 
 subQueryOne ∷ ∀ a q. Queryable q ⇒ QueryRunner a → String → QueryRunner' q a
 subQueryOne query selector n = query =<< queryOne selector n
-
-chooseOne ∷ ∀ a t m. Monad m ⇒ (t → ExceptT QueryError m a) → (t → ExceptT QueryError m a) → (t → ExceptT QueryError m a)
-chooseOne q1 q2 n = do
-  maybeN1 <- (ignoreErrors <<< q1) n
-  maybeN2 <- (ignoreErrors <<< q2) n
-
-  case maybeN1 <|> maybeN2 of
-    Nothing -> throwError QChooseError
-    Just n' -> pure n'
-
-chooseOne3 ∷ ∀ a t m.
-  Monad m
-  ⇒ (t → ExceptT QueryError m a)
-  → (t → ExceptT QueryError m a)
-  → (t → ExceptT QueryError m a)
-  → (t → ExceptT QueryError m a)
-chooseOne3 q1 q2 q3 n = do
-  maybeN1 <- (ignoreErrors <<< q1) n
-  maybeN2 <- (ignoreErrors <<< q2) n
-  maybeN3 <- (ignoreErrors <<< q3) n
-
-  case maybeN1 <|> maybeN2 <|> maybeN3 of
-    Nothing -> throwError QChooseError
-    Just n' -> pure n'

+ 8 - 5
src/LinkedIn/UI/Components/JobsUnifiedTopCard.purs

@@ -2,6 +2,7 @@ module LinkedIn.UI.Components.JobsUnifiedTopCard where
 
 import Prelude
 
+import Control.Alt ((<|>))
 import Control.Monad.Error.Class (throwError)
 import Data.Foldable (class Foldable, foldMap, foldlDefault, foldrDefault)
 import Data.Generic.Rep (class Generic)
@@ -12,7 +13,7 @@ import Data.Maybe (Maybe(..))
 import Data.Show.Generic (genericShow)
 import Data.Traversable (class Traversable, sequence, traverse, traverseDefault)
 import Data.Tuple (Tuple(..))
-import LinkedIn.QueryRunner (QueryError(..), QueryRunner, chooseOne, chooseOne3, ignoreNotFound, queryAll, queryOne, queryText)
+import LinkedIn.QueryRunner (QueryError(..), QueryRunner, ignoreNotFound, queryAll, queryOne, queryText)
 import Type.Proxy (Proxy(..))
 import Web.DOM (Node)
 import Web.DOM.Node as N
@@ -194,7 +195,7 @@ queryTopCardSecondaryInsightPlain n = pure $ TopCardSecondaryInsightPlain n
 
 queryTopCardSecondaryInsight :: QueryRunner (TopCardSecondaryInsight Node)
 queryTopCardSecondaryInsight n =
-  chooseOne queryTopCardSecondaryInsightNested queryTopCardSecondaryInsightPlain n
+  queryTopCardSecondaryInsightNested n <|> queryTopCardSecondaryInsightPlain n
 
 queryTopCardInsightContentSingle :: QueryRunner (TopCardInsightContent Node)
 queryTopCardInsightContentSingle n = pure $ TopCardInsightContentSingle n
@@ -216,17 +217,19 @@ queryTopCardInsightContentSecondary n = do
 
 queryTopCardInsightContent :: QueryRunner (TopCardInsightContent Node)
 queryTopCardInsightContent n =
-  chooseOne3 queryTopCardInsightContentSecondary queryTopCardInsightContentButton queryTopCardInsightContentSingle n
+  queryTopCardInsightContentSecondary n
+  <|> queryTopCardInsightContentButton n
+  <|> queryTopCardInsightContentSingle n
 
 queryTopCardInsight :: QueryRunner (TopCardInsight Node)
 queryTopCardInsight n = do
-  icon <- chooseOne (queryOne ":scope li-icon") (queryOne ":scope svg") n
+  icon <- queryOne ":scope li-icon" n <|> queryOne ":scope svg" n
   content <- queryTopCardInsightContent =<< getContentNode n
 
   pure $ TopCardInsight {icon, content}
 
   where
-    getContentNode = chooseOne (queryOne ":scope > span") (queryOne ":scope > button")
+    getContentNode n' = queryOne ":scope > span" n' <|> queryOne ":scope > button" n'
 
 queryTopCardPrimaryDescription :: QueryRunner (TopCardPrimaryDescription Node)
 queryTopCardPrimaryDescription n = do