Skip to content

Commit

Permalink
update replace' to reflect the existence of optional capturing groups (
Browse files Browse the repository at this point in the history
…#126)

* Update CI to use v0.14.0-rc5

* update replace' to reflect the existence of optional capturing groups

Co-authored-by: Jordan Martinez <jordanalex.martinez@gmail.com>
  • Loading branch information
davidchambers and JordanMartinez authored Jan 6, 2021
1 parent 3de404e commit 5815ea9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: purescript-contrib/setup-purescript@main
with:
purescript: "0.14.0-rc3"
purescript: "0.14.0-rc5"

- uses: actions/setup-node@v1
with:
Expand Down
21 changes: 15 additions & 6 deletions src/Data/String/Regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,21 @@ exports.replace = function (r) {
};
};

exports.replaceBy = function (r) {
return function (f) {
return function (s2) {
return s2.replace(r, function (match) {
return f(match)(Array.prototype.splice.call(arguments, 1, arguments.length - 3));
});
exports._replaceBy = function (just) {
return function (nothing) {
return function (r) {
return function (f) {
return function (s) {
return s.replace(r, function (match) {
var groups = [];
var group, i = 1;
while (typeof (group = arguments[i++]) !== "number") {
groups.push(group == null ? nothing : just(group));
}
return f(match)(groups);
});
};
};
};
};
};
Expand Down
25 changes: 16 additions & 9 deletions src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- | Wraps Javascript's `RegExp` object that enables matching strings with
-- | patternes defined by regular expressions.
-- | patterns defined by regular expressions.
-- | For details of the underlying implementation, see [RegExp Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).
module Data.String.Regex
( Regex(..)
Expand Down Expand Up @@ -95,18 +95,25 @@ foreign import _match
match :: Regex -> String -> Maybe (NonEmptyArray (Maybe String))
match = _match Just Nothing

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

-- | Transforms occurences of the `Regex` using a function of the matched
-- | substring and a list of submatch strings.
-- | 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 String -> String) -> String -> String
replace' = replaceBy
foreign import _replaceBy
:: (forall r. r -> Maybe r)
-> (forall r. Maybe r)
-> Regex
-> (String -> Array (Maybe String) -> String)
-> String
-> String

foreign import replaceBy :: Regex -> (String -> Array 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.
-- | 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' = _replaceBy Just Nothing

foreign import _search
:: (forall r. r -> Maybe r)
Expand All @@ -120,5 +127,5 @@ foreign import _search
search :: Regex -> String -> Maybe Int
search = _search Just Nothing

-- | Split the string into an array of substrings along occurences of the `Regex`.
-- | Split the string into an array of substrings along occurrences of the `Regex`.
foreign import split :: Regex -> String -> Array String
6 changes: 5 additions & 1 deletion test/Test/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Data.String.Regex.Unsafe (unsafeRegex)
import Effect (Effect)
import Effect.Console (log)
import Partial.Unsafe (unsafePartial)
import Prelude (type (~>), Unit, discard, not, ($), (<<<), (<>), (==))
import Prelude (type (~>), Unit, discard, not, show, ($), (<<<), (<>), (==))
import Test.Assert (assert)

testStringRegex :: Effect Unit
Expand All @@ -35,6 +35,10 @@ testStringRegex = do

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\")]"

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

0 comments on commit 5815ea9

Please sign in to comment.