Skip to content

Commit 27e834c

Browse files
committed
added typed convenience functions where possible
1 parent 449b49e commit 27e834c

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/Web/DOM/XPath.purs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,42 @@ evaluate ::
3030
evaluate xpath ctxt nsres resType res doc =
3131
evaluateInternal xpath ctxt (toNullable nsres) resType (toNullable res) doc
3232

33+
-- | Convenience function to avoid two funciton calls and possibly mismatched types.
34+
evaluateNumber ::
35+
String
36+
-> Node
37+
-> Maybe NSResolver
38+
-> Maybe XPathResult
39+
-> Document
40+
-> Effect Number
41+
evaluateNumber xpath ctxt nsres res doc = do
42+
xr <- evaluateInternal xpath ctxt (toNullable nsres) RT.number_type (toNullable res) doc
43+
numberValue xr
44+
45+
-- | Convenience function to avoid two funciton calls and possibly mismatched types.
46+
evaluateString ::
47+
String
48+
-> Node
49+
-> Maybe NSResolver
50+
-> Maybe XPathResult
51+
-> Document
52+
-> Effect String
53+
evaluateString xpath ctxt nsres res doc = do
54+
xr <- evaluateInternal xpath ctxt (toNullable nsres) RT.string_type (toNullable res) doc
55+
stringValue xr
56+
57+
-- | Convenience function to avoid two funciton calls and possibly mismatched types.
58+
evaluateBoolean ::
59+
String
60+
-> Node
61+
-> Maybe NSResolver
62+
-> Maybe XPathResult
63+
-> Document
64+
-> Effect Boolean
65+
evaluateBoolean xpath ctxt nsres res doc = do
66+
xr <- evaluateInternal xpath ctxt (toNullable nsres) RT.boolean_type (toNullable res) doc
67+
booleanValue xr
68+
3369
foreign import evaluateInternal ::
3470
String
3571
-> Node
@@ -72,7 +108,8 @@ snapshotItem :: XPathResult -> Natural -> Effect (Maybe Node)
72108
snapshotItem xpres ix = map toMaybe $
73109
snapshotItemInternal xpres (toNumber $ natToInt $ ix)
74110

75-
-- | High level wrapper around `snapshotItem` and `snapshotLength`
111+
-- | High level wrapper around [snapshotItem](#v:snapshotItem)
112+
-- | and [snapshotLength](#v:snapshotLength)
76113
-- | that directly returns an `Array` of `Node`s.
77114
snapshot :: XPathResult -> Effect (Array Node)
78115
snapshot xpres = case snapMay of

test/Main.purs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,12 @@ getMetajeloResolver node doc = do
6868
Just ns -> ns
6969

7070
mkCdYear :: Document -> Node -> Aff String
71-
mkCdYear doc node = liftEffect $ do
72-
cdRes <- XP.evaluate
73-
"YEAR"
74-
node
75-
Nothing
76-
RT.string_type
77-
Nothing
78-
doc
79-
XP.stringValue cdRes
71+
mkCdYear doc node = liftEffect $ XP.evaluateString
72+
"YEAR"
73+
node
74+
Nothing
75+
Nothing
76+
doc
8077

8178
main :: Effect Unit
8279
main = runTest do

0 commit comments

Comments
 (0)