feat: parentUntil
This commit is contained in:
parent
03835ee89d
commit
85c95036e3
6306
spago.lock
Normal file
6306
spago.lock
Normal file
File diff suppressed because it is too large
Load Diff
13
spago.yaml
13
spago.yaml
@ -1,16 +1,21 @@
|
||||
package:
|
||||
build:
|
||||
strict: true
|
||||
pedantic_packages: true
|
||||
dependencies:
|
||||
- arrays
|
||||
- effect
|
||||
- foreign
|
||||
- foreign-object
|
||||
- functions
|
||||
- maybe
|
||||
- nullable
|
||||
- ordered-collections
|
||||
- prelude
|
||||
- psci-support
|
||||
- test-unit
|
||||
- tailrec
|
||||
name: cheerio
|
||||
test:
|
||||
main: Test.Main
|
||||
dependencies:
|
||||
- test-unit
|
||||
workspace:
|
||||
extra_packages: {}
|
||||
package_set:
|
||||
|
@ -2,67 +2,81 @@ module Cheerio where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Rec.Class (Step(..), tailRecM)
|
||||
import Data.Array as Array
|
||||
import Data.Map (Map)
|
||||
import Data.Map as Map
|
||||
import Data.Maybe (Maybe)
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Data.Nullable (Nullable)
|
||||
import Data.Nullable as Nullable
|
||||
import Effect (Effect)
|
||||
import Foreign.Object (Object)
|
||||
|
||||
foreign import data CheerioNode :: Type
|
||||
foreign import data Node :: Type
|
||||
|
||||
foreign import loadImpl :: String -> Effect CheerioNode
|
||||
foreign import loadImpl :: String -> Effect Node
|
||||
|
||||
foreign import toArrayImpl :: CheerioNode -> Effect (Array CheerioNode)
|
||||
foreign import toNullableImpl :: CheerioNode -> Effect (Nullable CheerioNode)
|
||||
foreign import toArrayImpl :: Node -> Effect (Array Node)
|
||||
foreign import toNullableImpl :: Node -> Effect (Nullable Node)
|
||||
|
||||
foreign import siblingsImpl :: CheerioNode -> Effect CheerioNode
|
||||
foreign import childrenImpl :: CheerioNode -> Effect CheerioNode
|
||||
foreign import parentImpl :: CheerioNode -> Effect CheerioNode
|
||||
foreign import siblingsImpl :: Node -> Effect Node
|
||||
foreign import childrenImpl :: Node -> Effect Node
|
||||
foreign import parentImpl :: Node -> Effect Node
|
||||
|
||||
foreign import attrsImpl :: CheerioNode -> Effect (Object String)
|
||||
foreign import attrImpl :: String -> CheerioNode -> Effect (Nullable String)
|
||||
foreign import cssImpl :: CheerioNode -> Effect (Object String)
|
||||
foreign import htmlImpl :: CheerioNode -> Effect String
|
||||
foreign import textImpl :: CheerioNode -> Effect String
|
||||
foreign import attrsImpl :: Node -> Effect (Object String)
|
||||
foreign import attrImpl :: String -> Node -> Effect (Nullable String)
|
||||
foreign import cssImpl :: Node -> Effect (Object String)
|
||||
foreign import htmlImpl :: Node -> Effect String
|
||||
foreign import textImpl :: Node -> Effect String
|
||||
|
||||
foreign import isImpl :: String -> CheerioNode -> Effect Boolean
|
||||
foreign import findImpl :: String -> CheerioNode -> Effect CheerioNode
|
||||
foreign import isImpl :: String -> Node -> Effect Boolean
|
||||
foreign import findImpl :: String -> Node -> Effect Node
|
||||
|
||||
load :: String -> Effect CheerioNode
|
||||
load :: String -> Effect Node
|
||||
load = loadImpl
|
||||
|
||||
parent :: CheerioNode -> Effect (Maybe CheerioNode)
|
||||
parent :: Node -> Effect (Maybe Node)
|
||||
parent = map Nullable.toMaybe <<< toNullableImpl <=< parentImpl
|
||||
|
||||
siblings :: CheerioNode -> Effect (Array CheerioNode)
|
||||
parentUntil :: (Node -> Effect Boolean) -> Node -> Effect (Maybe Node)
|
||||
parentUntil f n =
|
||||
let
|
||||
go Nothing = pure $ Done Nothing
|
||||
go (Just p) = do
|
||||
mat <- f p
|
||||
if mat then
|
||||
pure $ Done $ Just p
|
||||
else
|
||||
Loop <$> parent p
|
||||
in
|
||||
tailRecM go =<< parent n
|
||||
|
||||
siblings :: Node -> Effect (Array Node)
|
||||
siblings = toArrayImpl <=< siblingsImpl
|
||||
|
||||
children :: CheerioNode -> Effect (Array CheerioNode)
|
||||
children :: Node -> Effect (Array Node)
|
||||
children = toArrayImpl <=< childrenImpl
|
||||
|
||||
attrs :: CheerioNode -> Effect (Map String String)
|
||||
attrs :: Node -> Effect (Map String String)
|
||||
attrs = map Map.fromFoldableWithIndex <<< attrsImpl
|
||||
|
||||
attr :: String -> CheerioNode -> Effect (Maybe String)
|
||||
attr :: String -> Node -> Effect (Maybe String)
|
||||
attr k = map Nullable.toMaybe <<< attrImpl k
|
||||
|
||||
css :: CheerioNode -> Effect (Map String String)
|
||||
css :: Node -> Effect (Map String String)
|
||||
css = map Map.fromFoldableWithIndex <<< cssImpl
|
||||
|
||||
html :: CheerioNode -> Effect String
|
||||
html :: Node -> Effect String
|
||||
html = htmlImpl
|
||||
|
||||
text :: CheerioNode -> Effect String
|
||||
text :: Node -> Effect String
|
||||
text = textImpl
|
||||
|
||||
is :: String -> CheerioNode -> Effect Boolean
|
||||
is :: String -> Node -> Effect Boolean
|
||||
is s = isImpl s
|
||||
|
||||
find :: String -> CheerioNode -> Effect (Array (CheerioNode))
|
||||
find :: String -> Node -> Effect (Array (Node))
|
||||
find s = toArrayImpl <=< findImpl s
|
||||
|
||||
findFirst :: String -> CheerioNode -> Effect (Maybe (CheerioNode))
|
||||
findFirst :: String -> Node -> Effect (Maybe (Node))
|
||||
findFirst s = map Array.head <<< find s
|
||||
|
@ -2,11 +2,9 @@ module Test.Cheerio where
|
||||
|
||||
import Prelude hiding (eq)
|
||||
|
||||
import Cheerio (CheerioNode)
|
||||
import Cheerio as Cheerio
|
||||
import Control.Monad.Error.Class (liftMaybe)
|
||||
import Data.Array as Array
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Effect (Effect)
|
||||
import Effect.Class (liftEffect)
|
||||
import Effect.Exception (error)
|
||||
|
Loading…
Reference in New Issue
Block a user