Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/Data/String/Regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ exports._replaceBy = function (just) {
return function (r) {
return function (f) {
return function (s) {
return s.replace(r, function (match) {
return s.replace(r, function () {
var groups = [];
var group, i = 1;
while (typeof (group = arguments[i++]) !== "number") {
groups.push(group == null ? nothing : just(group));
}
return f(match)(groups);
return f(groups);
});
};
};
Expand Down
10 changes: 5 additions & 5 deletions src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ foreign import _replaceBy
:: (forall r. r -> Maybe r)
-> (forall r. Maybe r)
-> Regex
-> (String -> Array (Maybe String) -> String)
-> (Array (Maybe String) -> String)
-> String
-> String

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

foreign import _search
Expand Down
10 changes: 5 additions & 5 deletions test/Test/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ testStringRegex = do
assert $ replace (unsafeRegex "-" noFlags) "!" "a-b-c" == "a!b-c"

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

log "search"
assert $ search (unsafeRegex "b" noFlags) "abc" == Just 1
Expand Down