Change oneOf format to use (pattern1|pattern2) notation.

This commit is contained in:
Dillon Kearns 2021-03-31 13:26:59 -07:00
parent 3fb2893dee
commit 78f704d578
2 changed files with 45 additions and 5 deletions

View File

@ -92,6 +92,27 @@ not string =
)
notOneOf : ( String, List String ) -> Glob String
notOneOf ( firstPattern, otherPatterns ) =
let
allPatterns =
firstPattern :: otherPatterns
in
Glob
("!("
++ (allPatterns |> String.join "|")
++ ")"
)
(\captures ->
case captures of
first :: rest ->
( first, rest )
[] ->
( "ERROR", [] )
)
run : List String -> Glob a -> { match : a, pattern : String }
run captures (Glob pattern applyCapture) =
{ match =
@ -142,9 +163,9 @@ oneOf ( defaultMatch, otherMatchers ) =
defaultMatch :: otherMatchers
in
Glob
("{"
++ (allMatchers |> List.map Tuple.first |> String.join ",")
++ "}"
("("
++ (allMatchers |> List.map Tuple.first |> String.join "|")
++ ")"
)
(\captures ->
case captures of

View File

@ -38,10 +38,11 @@ all =
]
)
)
-- https://runkit.com/embed/05epbnc0c7g1
|> expect
{ captures = [ "data-file", "json" ]
, expectedMatch = ( "data-file", Json )
, expectedPattern = "*.{yml,json}"
, expectedPattern = "*.(yml|json)"
}
, test "optional group - no match" <|
\() ->
@ -108,7 +109,10 @@ all =
, test "not" <|
\() ->
Glob.succeed Tuple.pair
|> Glob.keep (Glob.not "xyz")
|> Glob.keep
(Glob.notOneOf
( "xyz", [] )
)
|> Glob.drop (Glob.literal "/")
|> Glob.keep Glob.wildcard
|> Glob.drop (Glob.literal ".txt")
@ -119,6 +123,21 @@ all =
, expectedMatch = ( "abc", "d" )
, expectedPattern = "!(xyz)/*.txt"
}
, test "not with multiple patterns" <|
\() ->
Glob.succeed Tuple.pair
|> Glob.keep
(Glob.notOneOf ( "abz", [ "xyz" ] ))
|> Glob.drop (Glob.literal "/")
|> Glob.keep Glob.wildcard
|> Glob.drop (Glob.literal ".txt")
|> expect
-- abc/d.txt
-- https://runkit.com/embed/05epbnc0c7g1
{ captures = [ "abc", "d" ]
, expectedMatch = ( "abc", "d" )
, expectedPattern = "!(abz|xyz)/*.txt"
}
]