Skip to content

update match to reflect the existence of optional capturing groups #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2015
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
5 changes: 3 additions & 2 deletions docs/Data.String.Regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ Returns `true` if the `Regex` matches the string.
#### `match`

``` purescript
match :: Regex -> String -> Maybe [String]
match :: Regex -> String -> Maybe [Maybe String]
```

Matches the string against the `Regex` and returns an array of matches
if there were any.
if there were any. Each match has type `Maybe String`, where `Nothing`
represents an unmatched optional capturing group.
See [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match).

#### `replace`
Expand Down
17 changes: 13 additions & 4 deletions src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,23 @@ foreign import _match
"""
function _match(r, s, Just, Nothing) {
var m = s.match(r);
return m == null ? Nothing : Just(m);
if (m == null) {
return Nothing;
} else {
var list = [];
for (var i = 0; i < m.length; i++) {
list.push(m[i] == null ? Nothing : Just(m[i]));
}
return Just(list);
}
}
""" :: forall r. Fn4 Regex String ([String] -> r) r r
""" :: Fn4 Regex String (forall r. r -> Maybe r) (forall r. Maybe r) (Maybe (Maybe r))

-- | Matches the string against the `Regex` and returns an array of matches
-- | if there were any.
-- | if there were any. Each match has type `Maybe String`, where `Nothing`
-- | represents an unmatched optional capturing group.
-- | See [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match).
match :: Regex -> String -> Maybe [String]
match :: Regex -> String -> Maybe [Maybe String]
match r s = runFn4 _match r s Just Nothing

-- | Replaces occurences of the `Regex` with the first string. The replacement
Expand Down