From bb1ee9b7c0755f13e28019295bedd13e3396432c Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Tue, 30 Mar 2021 19:43:37 -0700 Subject: [PATCH] Change glob API to use same type between matchers and top-level glob type. --- src/Glob.elm | 81 +++++++++++++++++++++++++++++++++++++++++++++ tests/GlobTests.elm | 36 ++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/src/Glob.elm b/src/Glob.elm index f7c9cfdc..689a5432 100644 --- a/src/Glob.elm +++ b/src/Glob.elm @@ -7,6 +7,10 @@ type Glob a = Glob String (List String -> a) +type NewGlob a + = NewGlob String (List String -> ( a, List String )) + + type GlobMatcher a = GlobMatcher String (CaptureToValue a) @@ -16,11 +20,57 @@ type CaptureToValue a | Dynamic (String -> a) +map : (a -> b) -> NewGlob a -> NewGlob b +map mapFn (NewGlob pattern applyCapture) = + NewGlob pattern + (\captures -> + captures + |> applyCapture + |> Tuple.mapFirst mapFn + ) + + succeed : constructor -> Glob constructor succeed constructor = Glob "" (\captures -> constructor) +succeed2 : constructor -> NewGlob constructor +succeed2 constructor = + NewGlob "" (\captures -> ( constructor, captures )) + + +star2 : NewGlob String +star2 = + NewGlob "*" + (\captures -> + case captures of + first :: rest -> + ( first, rest ) + + --applyCapture rest (toValueFn first) + [] -> + ( "ERROR", [] ) + --applyCapture [] (toValueFn "ERROR") + ) + + +literal2 : String -> NewGlob String +literal2 string = + NewGlob string (\captures -> ( string, captures )) + + +runNew : List String -> NewGlob a -> { match : a, pattern : String } +runNew captures (NewGlob pattern applyCapture) = + { match = + captures + |> List.reverse + |> applyCapture + |> Tuple.first + , pattern = pattern + } + + run : List String -> Glob a -> { match : a, pattern : String } run captures (Glob pattern applyCapture) = { match = @@ -65,6 +115,37 @@ popCapture toValueFn applyCapture = applyCapture [] (toValueFn "ERROR") +drop2 : NewGlob a -> NewGlob value -> NewGlob value +drop2 (NewGlob matcherPattern apply1) (NewGlob pattern apply2) = + NewGlob + (pattern ++ matcherPattern) + apply2 + + + +--keep : GlobMatcher a -> Glob (a -> value) -> Glob value + + +keep2 : NewGlob a -> NewGlob (a -> value) -> NewGlob value +keep2 (NewGlob matcherPattern apply1) (NewGlob pattern apply2) = + NewGlob + (pattern ++ matcherPattern) + (\captures -> + let + ( applied1, captured1 ) = + captures + |> apply1 + + ( applied2, captured2 ) = + captured1 + |> apply2 + in + ( applied1 |> applied2 + , captured2 + ) + ) + + drop : GlobMatcher a -> Glob value -> Glob value drop (GlobMatcher matcherPattern toValue) (Glob pattern applyCapture) = Glob diff --git a/tests/GlobTests.elm b/tests/GlobTests.elm index 457cf4c7..341f3eb3 100644 --- a/tests/GlobTests.elm +++ b/tests/GlobTests.elm @@ -74,6 +74,26 @@ all = , expectedMatch = Just "ba" , expectedPattern = "test/a*(a|b)/x.js" } + , test "new star" <| + \() -> + Glob.star2 + |> expect2 + { captures = [ "star-pattern" ] + , expectedMatch = "star-pattern" + , expectedPattern = "*" + } + , test "new star with literal" <| + \() -> + Glob.succeed2 Tuple.pair + |> Glob.keep2 Glob.star2 + |> Glob.drop2 (Glob.literal2 "/") + |> Glob.keep2 (Glob.star2 |> Glob.map String.toUpper) + |> Glob.drop2 (Glob.literal2 ".txt") + |> expect2 + { captures = [ "before-slash", "after-slash" ] + , expectedMatch = ( "before-slash", "AFTER-SLASH" ) + , expectedPattern = "*/*.txt" + } ] @@ -104,3 +124,19 @@ expect { captures, expectedMatch, expectedPattern } glob = { pattern = expectedPattern , match = expectedMatch } + + +expect2 : + { captures : List String + , expectedMatch : match + , expectedPattern : String + } + -> Glob.NewGlob match + -> Expect.Expectation +expect2 { captures, expectedMatch, expectedPattern } glob = + glob + |> Glob.runNew captures + |> Expect.equal + { pattern = expectedPattern + , match = expectedMatch + }