Skip to content

Commit cc45a34

Browse files
committed
simplify type of replace'
The ‘match’ argument provided by String#replace (when given a function) is often irrelevant. If one does require access to the matched substring as a whole, one can parenthesize the whole pattern to capture it.
1 parent 5815ea9 commit cc45a34

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/Data/String/Regex.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ exports._replaceBy = function (just) {
7373
return function (r) {
7474
return function (f) {
7575
return function (s) {
76-
return s.replace(r, function (match) {
76+
return s.replace(r, function () {
7777
var groups = [];
7878
var group, i = 1;
7979
while (typeof (group = arguments[i++]) !== "number") {
8080
groups.push(group == null ? nothing : just(group));
8181
}
82-
return f(match)(groups);
82+
return f(groups);
8383
});
8484
};
8585
};

src/Data/String/Regex.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ foreign import _replaceBy
104104
:: (forall r. r -> Maybe r)
105105
-> (forall r. Maybe r)
106106
-> Regex
107-
-> (String -> Array (Maybe String) -> String)
107+
-> (Array (Maybe String) -> String)
108108
-> String
109109
-> String
110110

111-
-- | Transforms occurrences of the `Regex` using a function of the matched
112-
-- | substring and a list of captured substrings of type `Maybe String`,
113-
-- | where `Nothing` represents an unmatched optional capturing group.
111+
-- | Transforms occurrences of the `Regex` using a function of
112+
-- | a list of captured substrings of type `Maybe String`, where
113+
-- | `Nothing` represents an unmatched optional capturing group.
114114
-- | See the [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter).
115-
replace' :: Regex -> (String -> Array (Maybe String) -> String) -> String -> String
115+
replace' :: Regex -> (Array (Maybe String) -> String) -> String -> String
116116
replace' = _replaceBy Just Nothing
117117

118118
foreign import _search

test/Test/Data/String/Regex.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ testStringRegex = do
3434
assert $ replace (unsafeRegex "-" noFlags) "!" "a-b-c" == "a!b-c"
3535

3636
log "replace'"
37-
assert $ replace' (unsafeRegex "-" noFlags) (\s xs -> "!") "a-b-c" == "a!b-c"
38-
assert $ replace' (unsafeRegex "(foo)(bar)?" noFlags) (\s xs -> show xs) "<>" == "<>"
39-
assert $ replace' (unsafeRegex "(foo)(bar)?" noFlags) (\s xs -> show xs) "<foo>" == "<[(Just \"foo\"),Nothing]>"
40-
assert $ replace' (unsafeRegex "(foo)(bar)?" noFlags) (\s xs -> show xs) "<foobar>" == "<[(Just \"foo\"),(Just \"bar\")]>"
41-
assert $ replace' (unsafeRegex "@(?<username>\\w+)" noFlags) (\s xs -> show xs) "@purescript" == "[(Just \"purescript\")]"
37+
assert $ replace' (unsafeRegex "-" noFlags) (xs -> "!") "a-b-c" == "a!b-c"
38+
assert $ replace' (unsafeRegex "(foo)(bar)?" noFlags) show "<>" == "<>"
39+
assert $ replace' (unsafeRegex "(foo)(bar)?" noFlags) show "<foo>" == "<[(Just \"foo\"),Nothing]>"
40+
assert $ replace' (unsafeRegex "(foo)(bar)?" noFlags) show "<foobar>" == "<[(Just \"foo\"),(Just \"bar\")]>"
41+
assert $ replace' (unsafeRegex "@(?<username>\\w+)" noFlags) show "@purescript" == "[(Just \"purescript\")]"
4242

4343
log "search"
4444
assert $ search (unsafeRegex "b" noFlags) "abc" == Just 1

0 commit comments

Comments
 (0)