mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-26 13:21:42 +03:00
Change glob API to use same type between matchers and top-level glob type.
This commit is contained in:
parent
8cfa4be975
commit
bb1ee9b7c0
81
src/Glob.elm
81
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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user