Skip to content

Commit 5815ea9

Browse files
update replace' to reflect the existence of optional capturing groups (#126)
* Update CI to use v0.14.0-rc5 * update replace' to reflect the existence of optional capturing groups Co-authored-by: Jordan Martinez <[email protected]>
1 parent 3de404e commit 5815ea9

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
- uses: purescript-contrib/setup-purescript@main
1616
with:
17-
purescript: "0.14.0-rc3"
17+
purescript: "0.14.0-rc5"
1818

1919
- uses: actions/setup-node@v1
2020
with:

src/Data/String/Regex.js

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

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

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(..)
@@ -95,18 +95,25 @@ foreign import _match
9595
match :: Regex -> String -> Maybe (NonEmptyArray (Maybe String))
9696
match = _match Just Nothing
9797

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

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

109-
foreign import replaceBy :: Regex -> (String -> Array String -> String) -> String -> String
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.
114+
-- | 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
116+
replace' = _replaceBy Just Nothing
110117

111118
foreign import _search
112119
:: (forall r. r -> Maybe r)
@@ -120,5 +127,5 @@ foreign import _search
120127
search :: Regex -> String -> Maybe Int
121128
search = _search Just Nothing
122129

123-
-- | Split the string into an array of substrings along occurences of the `Regex`.
130+
-- | Split the string into an array of substrings along occurrences of the `Regex`.
124131
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
@@ -35,6 +35,10 @@ testStringRegex = do
3535

3636
log "replace'"
3737
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\")]"
3842

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

0 commit comments

Comments
 (0)