Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

data Regex :: *

type RegexFlags = { unicode :: Boolean, sticky :: Boolean, multiline :: Boolean, ignoreCase :: Boolean, global :: Boolean }
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paf31: Are the double spaces here (RegexFlags  =) a bug in docgen?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the type arguments (if any) go between the two spaces.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



### Type Class Instances

Expand All @@ -53,14 +55,22 @@

### Values

flags :: Regex -> RegexFlags

match :: Regex -> String -> [String]

regex :: String -> String -> Regex
parseFlags :: String -> RegexFlags

regex :: String -> RegexFlags -> Regex

renderFlags :: RegexFlags -> String

replace :: Regex -> String -> String -> String

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

search :: Regex -> String -> Number

source :: Regex -> String

test :: Regex -> String -> Boolean
55 changes: 53 additions & 2 deletions src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
module Data.String.Regex (
Regex(..),
RegexFlags(..),
regex,
source,
flags,
renderFlags,
parseFlags,
test,
match,
replace,
replace',
search
) where

import Data.String (indexOf)

foreign import data Regex :: *

foreign import showRegex'
Expand All @@ -18,13 +25,57 @@ foreign import showRegex'
instance showRegex :: Show Regex where
show = showRegex'

foreign import regex
"function regex(s1) {\
type RegexFlags =
{ global :: Boolean
, ignoreCase :: Boolean
, multiline :: Boolean
, sticky :: Boolean
, unicode :: Boolean
}

foreign import regex'
"function regex$prime(s1) {\
\ return function(s2) {\
\ return new RegExp(s1, s2);\
\ };\
\}" :: String -> String -> Regex

regex :: String -> RegexFlags -> Regex
regex source flags = regex' source $ renderFlags flags

foreign import source
"function source(r) {\
\ return r.source;\
\}" :: Regex -> String

foreign import flags
"function source(r) {\
\ return {\
\ multiline: r.multiline,\
\ ignoreCase: r.ignoreCase,\
\ global: r.global,\
\ sticky: !!r.sticky,\
\ unicode: !!r.unicode\
\ };\
\}" :: Regex -> RegexFlags

renderFlags :: RegexFlags -> String
renderFlags flags =
(if flags.global then "g" else "") ++
(if flags.ignoreCase then "i" else "") ++
(if flags.multiline then "m" else "") ++
(if flags.sticky then "y" else "") ++
(if flags.unicode then "u" else "")

parseFlags :: String -> RegexFlags
parseFlags s =
{ global: indexOf "g" s >= 0
, ignoreCase: indexOf "i" s >= 0
, multiline: indexOf "m" s >= 0
, sticky: indexOf "y" s >= 0
, unicode: indexOf "u" s >= 0
}

foreign import test
"function test(r) {\
\ return function (s) {\
Expand Down