Skip to content

Commit d6c6b58

Browse files
committed
Use NonEmptyArray for Regex match
1 parent 00030c2 commit d6c6b58

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/Data/String/Regex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ exports._match = function (just) {
4646
return function (r) {
4747
return function (s) {
4848
var m = s.match(r);
49-
if (m == null) {
49+
if (m == null || m.length === 0) {
5050
return nothing;
5151
} else {
5252
for (var i = 0; i < m.length; i++) {

src/Data/String/Regex.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Data.String.Regex
1818

1919
import Prelude
2020

21+
import Data.Array.NonEmpty (NonEmptyArray)
2122
import Data.Either (Either(..))
2223
import Data.Maybe (Maybe(..))
2324
import Data.String (Pattern(..), contains)
@@ -82,13 +83,13 @@ foreign import _match
8283
-> (forall r. Maybe r)
8384
-> Regex
8485
-> String
85-
-> Maybe (Array (Maybe String))
86+
-> Maybe (NonEmptyArray (Maybe String))
8687

8788
-- | Matches the string against the `Regex` and returns an array of matches
8889
-- | if there were any. Each match has type `Maybe String`, where `Nothing`
8990
-- | represents an unmatched optional capturing group.
9091
-- | See [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match).
91-
match :: Regex -> String -> Maybe (Array (Maybe String))
92+
match :: Regex -> String -> Maybe (NonEmptyArray (Maybe String))
9293
match = _match Just Nothing
9394

9495
-- | Replaces occurences of the `Regex` with the first string. The replacement

test/Test/Data/String/Regex.purs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module Test.Data.String.Regex (testStringRegex) where
22

3-
import Prelude (Unit, ($), (<>), discard, (==), not)
4-
5-
import Effect (Effect)
6-
import Effect.Console (log)
3+
import Data.String.Regex
74

5+
import Data.Array.NonEmpty (NonEmptyArray, fromArray)
86
import Data.Either (isLeft)
9-
import Data.Maybe (Maybe(..))
10-
import Data.String.Regex
7+
import Data.Maybe (Maybe(..), fromJust)
118
import Data.String.Regex.Flags (global, ignoreCase, noFlags)
129
import Data.String.Regex.Unsafe (unsafeRegex)
13-
10+
import Effect (Effect)
11+
import Effect.Console (log)
12+
import Partial.Unsafe (unsafePartial)
13+
import Prelude (type (~>), Unit, discard, not, ($), (<<<), (<>), (==))
1414
import Test.Assert (assert)
1515

1616
testStringRegex :: Effect Unit
@@ -26,7 +26,8 @@ testStringRegex = do
2626
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" (global <> ignoreCase)) "qux" "foobarFOObaz"
2727

2828
log "match"
29-
assert $ match (unsafeRegex "^abc$" noFlags) "abc" == Just [Just "abc"]
29+
assert $ match (unsafeRegex "^abc$" noFlags) "abc" == Just (nea [Just "abc"])
30+
assert $ match (unsafeRegex "^abc$" noFlags) "xyz" == Nothing
3031

3132
log "replace"
3233
assert $ replace (unsafeRegex "-" noFlags) "!" "a-b-c" == "a!b-c"
@@ -50,3 +51,6 @@ testStringRegex = do
5051
let pattern = unsafeRegex "a" (parseFlags "g")
5152
assert $ test pattern "a"
5253
assert $ test pattern "a"
54+
55+
nea :: Array ~> NonEmptyArray
56+
nea = unsafePartial fromJust <<< fromArray

0 commit comments

Comments
 (0)