Add a new toArray function, to make it easy to deal with each matched

element individually. Also add some tests/docs for the length function.
This commit is contained in:
Gareth Daniel Smith 2018-08-04 23:02:02 +01:00
parent 534cf3d4bb
commit 78ac03a8ef
2 changed files with 33 additions and 1 deletions

View File

@ -13,9 +13,13 @@ module Cheerio (
parent, parent,
prev, prev,
siblings, siblings,
text text,
toArray
) where ) where
import Prelude
import Data.Array ((..))
import Data.Function.Uncurried (Fn2, Fn3, Fn4, runFn2, runFn3, runFn4) import Data.Function.Uncurried (Fn2, Fn3, Fn4, runFn2, runFn3, runFn4)
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
@ -67,4 +71,12 @@ html = runFn3 htmlImpl Nothing Just
foreign import text :: Cheerio -> String foreign import text :: Cheerio -> String
-- Miscellaneous -- Miscellaneous
-- | Get how many elements there are in the given Cheerio
foreign import length :: Cheerio -> Int foreign import length :: Cheerio -> Int
-- | Seperate each element out into its own Cheerio
toArray :: Cheerio -> Array Cheerio
toArray c
| length c == 0 = []
| otherwise = map (\i -> eq i c) (0 .. (length c - 1))

View File

@ -24,6 +24,7 @@ import Cheerio
, prev , prev
, siblings , siblings
, text , text
, toArray
) )
import Cheerio.Static (loadRoot) import Cheerio.Static (loadRoot)
@ -138,6 +139,25 @@ suites = do
"" ""
(emptyCheerio # text) (emptyCheerio # text)
suite "Miscellaneous" do
test "length" do
Assert.equal
0
(emptyCheerio # length)
Assert.equal
3
(loadRoot htmlEx # find "li" # length)
test "toArray" do
Assert.equal
[]
(emptyCheerio # toArray # map (attr "class"))
Assert.equal
(map Just ["apple", "orange", "pear"])
(loadRoot htmlEx # find "li" # toArray # map (attr "class"))
suite "More" do suite "More" do
test "Long chain" do test "Long chain" do
Assert.equal Assert.equal