Skip to content

Commit

Permalink
Merge pull request #6 from michaelficarra/GH-5
Browse files Browse the repository at this point in the history
fixes #5: implement RegexFlags with parsing, rendering, and retrieval
  • Loading branch information
garyb committed Jul 20, 2014
2 parents 37fadf1 + 4d4f864 commit 9485d42
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
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 }


### 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

0 comments on commit 9485d42

Please sign in to comment.