Skip to content

Commit 5cdae2b

Browse files
committed
update replace' to reflect the existence of optional capturing groups
1 parent 5b0351d commit 5cdae2b

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/Data/String/Regex.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,21 @@ exports.replace = function (r) {
6767
};
6868
};
6969

70-
exports.replaceBy = function (r) {
71-
return function (f) {
72-
return function (s2) {
73-
return s2.replace(r, function (match) {
74-
return f(match)(Array.prototype.splice.call(arguments, 1, arguments.length - 3));
75-
});
70+
exports._replaceBy = function (just) {
71+
return function (nothing) {
72+
return function (r) {
73+
return function (f) {
74+
return function (s) {
75+
return s.replace(r, function (match) {
76+
var groups = [];
77+
var group, i = 1;
78+
while (typeof (group = arguments[i++]) !== "number") {
79+
groups.push(group == null ? nothing : just(group));
80+
}
81+
return f(match)(groups);
82+
});
83+
};
84+
};
7685
};
7786
};
7887
};

src/Data/String/Regex.purs

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- | Wraps Javascript's `RegExp` object that enables matching strings with
2-
-- | patternes defined by regular expressions.
2+
-- | patterns defined by regular expressions.
33
-- | For details of the underlying implementation, see [RegExp Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).
44
module Data.String.Regex
55
( Regex(..)
@@ -93,18 +93,25 @@ foreign import _match
9393
match :: Regex -> String -> Maybe (NonEmptyArray (Maybe String))
9494
match = _match Just Nothing
9595

96-
-- | Replaces occurences of the `Regex` with the first string. The replacement
96+
-- | Replaces occurrences of the `Regex` with the first string. The replacement
9797
-- | string can include special replacement patterns escaped with `"$"`.
9898
-- | See [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace).
9999
foreign import replace :: Regex -> String -> String -> String
100100

101-
-- | Transforms occurences of the `Regex` using a function of the matched
102-
-- | substring and a list of submatch strings.
103-
-- | See the [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter).
104-
replace' :: Regex -> (String -> Array String -> String) -> String -> String
105-
replace' = replaceBy
101+
foreign import _replaceBy
102+
:: (forall r. r -> Maybe r)
103+
-> (forall r. Maybe r)
104+
-> Regex
105+
-> (String -> Array (Maybe String) -> String)
106+
-> String
107+
-> String
106108

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

109116
foreign import _search
110117
:: (forall r. r -> Maybe r)
@@ -118,5 +125,5 @@ foreign import _search
118125
search :: Regex -> String -> Maybe Int
119126
search = _search Just Nothing
120127

121-
-- | Split the string into an array of substrings along occurences of the `Regex`.
128+
-- | Split the string into an array of substrings along occurrences of the `Regex`.
122129
foreign import split :: Regex -> String -> Array String

test/Test/Data/String/Regex.purs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Data.String.Regex.Unsafe (unsafeRegex)
1010
import Effect (Effect)
1111
import Effect.Console (log)
1212
import Partial.Unsafe (unsafePartial)
13-
import Prelude (type (~>), Unit, discard, not, ($), (<<<), (<>), (==))
13+
import Prelude (type (~>), Unit, discard, not, show, ($), (<<<), (<>), (==))
1414
import Test.Assert (assert)
1515

1616
testStringRegex :: Effect Unit
@@ -34,6 +34,10 @@ testStringRegex = do
3434

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

3842
log "search"
3943
assert $ search (unsafeRegex "b" noFlags) "abc" == Just 1

0 commit comments

Comments
 (0)