Skip to content

Commit 9485d42

Browse files
committed
Merge pull request #6 from michaelficarra/GH-5
fixes #5: implement RegexFlags with parsing, rendering, and retrieval
2 parents 37fadf1 + 4d4f864 commit 9485d42

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
data Regex :: *
4747

48+
type RegexFlags = { unicode :: Boolean, sticky :: Boolean, multiline :: Boolean, ignoreCase :: Boolean, global :: Boolean }
49+
4850

4951
### Type Class Instances
5052

@@ -53,14 +55,22 @@
5355

5456
### Values
5557

58+
flags :: Regex -> RegexFlags
59+
5660
match :: Regex -> String -> [String]
5761

58-
regex :: String -> String -> Regex
62+
parseFlags :: String -> RegexFlags
63+
64+
regex :: String -> RegexFlags -> Regex
65+
66+
renderFlags :: RegexFlags -> String
5967

6068
replace :: Regex -> String -> String -> String
6169

6270
replace' :: Regex -> (String -> [String] -> String) -> String -> String
6371

6472
search :: Regex -> String -> Number
6573

74+
source :: Regex -> String
75+
6676
test :: Regex -> String -> Boolean

src/Data/String/Regex.purs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
module Data.String.Regex (
22
Regex(..),
3+
RegexFlags(..),
34
regex,
5+
source,
6+
flags,
7+
renderFlags,
8+
parseFlags,
49
test,
510
match,
611
replace,
712
replace',
813
search
914
) where
1015

16+
import Data.String (indexOf)
17+
1118
foreign import data Regex :: *
1219

1320
foreign import showRegex'
@@ -18,13 +25,57 @@ foreign import showRegex'
1825
instance showRegex :: Show Regex where
1926
show = showRegex'
2027

21-
foreign import regex
22-
"function regex(s1) {\
28+
type RegexFlags =
29+
{ global :: Boolean
30+
, ignoreCase :: Boolean
31+
, multiline :: Boolean
32+
, sticky :: Boolean
33+
, unicode :: Boolean
34+
}
35+
36+
foreign import regex'
37+
"function regex$prime(s1) {\
2338
\ return function(s2) {\
2439
\ return new RegExp(s1, s2);\
2540
\ };\
2641
\}" :: String -> String -> Regex
2742

43+
regex :: String -> RegexFlags -> Regex
44+
regex source flags = regex' source $ renderFlags flags
45+
46+
foreign import source
47+
"function source(r) {\
48+
\ return r.source;\
49+
\}" :: Regex -> String
50+
51+
foreign import flags
52+
"function source(r) {\
53+
\ return {\
54+
\ multiline: r.multiline,\
55+
\ ignoreCase: r.ignoreCase,\
56+
\ global: r.global,\
57+
\ sticky: !!r.sticky,\
58+
\ unicode: !!r.unicode\
59+
\ };\
60+
\}" :: Regex -> RegexFlags
61+
62+
renderFlags :: RegexFlags -> String
63+
renderFlags flags =
64+
(if flags.global then "g" else "") ++
65+
(if flags.ignoreCase then "i" else "") ++
66+
(if flags.multiline then "m" else "") ++
67+
(if flags.sticky then "y" else "") ++
68+
(if flags.unicode then "u" else "")
69+
70+
parseFlags :: String -> RegexFlags
71+
parseFlags s =
72+
{ global: indexOf "g" s >= 0
73+
, ignoreCase: indexOf "i" s >= 0
74+
, multiline: indexOf "m" s >= 0
75+
, sticky: indexOf "y" s >= 0
76+
, unicode: indexOf "u" s >= 0
77+
}
78+
2879
foreign import test
2980
"function test(r) {\
3081
\ return function (s) {\

0 commit comments

Comments
 (0)