Skip to content

Commit d4f4ca8

Browse files
committed
update match to reflect the existence of optional capturing groups
1 parent 3810b5b commit d4f4ca8

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

docs/Data.String.Regex.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ Returns `true` if the `Regex` matches the string.
8989
#### `match`
9090

9191
``` purescript
92-
match :: Regex -> String -> Maybe [String]
92+
match :: Regex -> String -> Maybe [Maybe String]
9393
```
9494

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

99100
#### `replace`

src/Data/String/Regex.purs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,23 @@ foreign import _match
121121
"""
122122
function _match(r, s, Just, Nothing) {
123123
var m = s.match(r);
124-
return m == null ? Nothing : Just(m);
124+
if (m == null) {
125+
return Nothing;
126+
} else {
127+
var list = [];
128+
for (var i = 0; i < m.length; i++) {
129+
list.push(m[i] == null ? Nothing : Just(m[i]));
130+
}
131+
return Just(list);
132+
}
125133
}
126-
""" :: forall r. Fn4 Regex String ([String] -> r) r r
134+
""" :: Fn4 Regex String (forall r. r -> Maybe r) (forall r. Maybe r) (Maybe (Maybe r))
127135

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

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

0 commit comments

Comments
 (0)