mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-28 06:05:31 +03:00
Move some logic to internals to remove low-level functions from public API.
This commit is contained in:
parent
71c212d5ec
commit
35045ed51e
@ -7,7 +7,6 @@ module DataSource.Glob exposing
|
||||
, literal
|
||||
, atLeastOne, map, oneOf, succeed, toDataSource, zeroOrMore
|
||||
, toNonEmptyWithDefault
|
||||
, run, extractMatches
|
||||
)
|
||||
|
||||
{-|
|
||||
@ -203,15 +202,11 @@ That will give us
|
||||
|
||||
@docs toNonEmptyWithDefault
|
||||
|
||||
|
||||
## Internals - TODO Remove
|
||||
|
||||
@docs run, extractMatches
|
||||
|
||||
-}
|
||||
|
||||
import DataSource
|
||||
import DataSource.Http
|
||||
import DataSource.Internal.Glob exposing (Glob(..))
|
||||
import List.Extra
|
||||
import OptimizedDecoder
|
||||
import Regex
|
||||
@ -219,8 +214,8 @@ import Secrets
|
||||
|
||||
|
||||
{-| -}
|
||||
type Glob a
|
||||
= Glob String String (String -> List String -> ( a, List String ))
|
||||
type alias Glob a =
|
||||
DataSource.Internal.Glob.Glob a
|
||||
|
||||
|
||||
{-| -}
|
||||
@ -292,7 +287,24 @@ int =
|
||||
)
|
||||
|
||||
|
||||
{-| -}
|
||||
{-| Matches any number of characters, including `/`, as long as it's the only thing in a path part.
|
||||
|
||||
In contrast, `wildcard` will never match `/`, so it only matches within a single path part.
|
||||
|
||||
This is the same as `**/*.txt`.
|
||||
|
||||
example =
|
||||
Glob.succeed Tuple.pair
|
||||
|> Glob.capture Glob.recursiveWildcard
|
||||
|> Glob.match (Glob.literal "/")
|
||||
|> Glob.capture Glob.wildcard
|
||||
|> Glob.match (Glob.literal ".txt")
|
||||
|> expect "a/b/c/d.txt"
|
||||
{ expectedMatch = ( [ "a", "b", "c" ], "d" )
|
||||
, expectedPattern = "**/*.txt"
|
||||
}
|
||||
|
||||
-}
|
||||
recursiveWildcard : Glob (List String)
|
||||
recursiveWildcard =
|
||||
Glob "**"
|
||||
@ -362,37 +374,6 @@ regexEscapePattern =
|
||||
|> Maybe.withDefault Regex.never
|
||||
|
||||
|
||||
{-| -}
|
||||
run : String -> Glob a -> { match : a, pattern : String }
|
||||
run rawInput (Glob pattern regex applyCapture) =
|
||||
let
|
||||
fullRegex =
|
||||
"^" ++ regex ++ "$"
|
||||
|
||||
regexCaptures : List String
|
||||
regexCaptures =
|
||||
Regex.find parsedRegex rawInput
|
||||
|> List.concatMap .submatches
|
||||
|> List.map (Maybe.withDefault "")
|
||||
|
||||
parsedRegex =
|
||||
Regex.fromString fullRegex |> Maybe.withDefault Regex.never
|
||||
in
|
||||
{ match =
|
||||
regexCaptures
|
||||
|> List.reverse
|
||||
|> applyCapture rawInput
|
||||
|> Tuple.first
|
||||
, pattern = pattern
|
||||
}
|
||||
|
||||
|
||||
{-| -}
|
||||
toPattern : Glob a -> String
|
||||
toPattern (Glob pattern regex applyCapture) =
|
||||
pattern
|
||||
|
||||
|
||||
{-| -}
|
||||
match : Glob a -> Glob value -> Glob value
|
||||
match (Glob matcherPattern regex1 apply1) (Glob pattern regex2 apply2) =
|
||||
@ -607,7 +588,7 @@ atLeastOne ( defaultMatch, otherMatchers ) =
|
||||
-- |> Maybe.withDefault (defaultMatch |> Tuple.second)
|
||||
-- , []
|
||||
-- )
|
||||
extractMatches (defaultMatch |> Tuple.second) allMatchers match_
|
||||
DataSource.Internal.Glob.extractMatches (defaultMatch |> Tuple.second) allMatchers match_
|
||||
|> toNonEmptyWithDefault (defaultMatch |> Tuple.second)
|
||||
, rest
|
||||
)
|
||||
@ -628,38 +609,14 @@ toNonEmptyWithDefault default list =
|
||||
( default, [] )
|
||||
|
||||
|
||||
{-| -}
|
||||
extractMatches : a -> List ( String, a ) -> String -> List a
|
||||
extractMatches defaultValue list string =
|
||||
if string == "" then
|
||||
[]
|
||||
|
||||
else
|
||||
let
|
||||
( matchedValue, updatedString ) =
|
||||
List.Extra.findMap
|
||||
(\( literalString, value ) ->
|
||||
if string |> String.startsWith literalString then
|
||||
Just ( value, string |> String.dropLeft (String.length literalString) )
|
||||
|
||||
else
|
||||
Nothing
|
||||
)
|
||||
list
|
||||
|> Maybe.withDefault ( defaultValue, "" )
|
||||
in
|
||||
matchedValue
|
||||
:: extractMatches defaultValue list updatedString
|
||||
|
||||
|
||||
{-| -}
|
||||
toDataSource : Glob a -> DataSource.DataSource (List a)
|
||||
toDataSource glob =
|
||||
DataSource.Http.get (Secrets.succeed <| "glob://" ++ toPattern glob)
|
||||
DataSource.Http.get (Secrets.succeed <| "glob://" ++ DataSource.Internal.Glob.toPattern glob)
|
||||
(OptimizedDecoder.string
|
||||
|> OptimizedDecoder.list
|
||||
|> OptimizedDecoder.map
|
||||
(\rawGlob -> rawGlob |> List.map (\matchedPath -> run matchedPath glob |> .match))
|
||||
(\rawGlob -> rawGlob |> List.map (\matchedPath -> DataSource.Internal.Glob.run matchedPath glob |> .match))
|
||||
)
|
||||
|
||||
|
||||
|
68
src/DataSource/Internal/Glob.elm
Normal file
68
src/DataSource/Internal/Glob.elm
Normal file
@ -0,0 +1,68 @@
|
||||
module DataSource.Internal.Glob exposing
|
||||
( Glob(..)
|
||||
, extractMatches
|
||||
, run
|
||||
, toPattern
|
||||
)
|
||||
|
||||
import List.Extra
|
||||
import Regex
|
||||
|
||||
|
||||
{-| -}
|
||||
type Glob a
|
||||
= Glob String String (String -> List String -> ( a, List String ))
|
||||
|
||||
|
||||
run : String -> Glob a -> { match : a, pattern : String }
|
||||
run rawInput (Glob pattern regex applyCapture) =
|
||||
let
|
||||
fullRegex =
|
||||
"^" ++ regex ++ "$"
|
||||
|
||||
regexCaptures : List String
|
||||
regexCaptures =
|
||||
Regex.find parsedRegex rawInput
|
||||
|> List.concatMap .submatches
|
||||
|> List.map (Maybe.withDefault "")
|
||||
|
||||
parsedRegex =
|
||||
Regex.fromString fullRegex |> Maybe.withDefault Regex.never
|
||||
in
|
||||
{ match =
|
||||
regexCaptures
|
||||
|> List.reverse
|
||||
|> applyCapture rawInput
|
||||
|> Tuple.first
|
||||
, pattern = pattern
|
||||
}
|
||||
|
||||
|
||||
{-| -}
|
||||
toPattern : Glob a -> String
|
||||
toPattern (Glob pattern regex applyCapture) =
|
||||
pattern
|
||||
|
||||
|
||||
{-| -}
|
||||
extractMatches : a -> List ( String, a ) -> String -> List a
|
||||
extractMatches defaultValue list string =
|
||||
if string == "" then
|
||||
[]
|
||||
|
||||
else
|
||||
let
|
||||
( matchedValue, updatedString ) =
|
||||
List.Extra.findMap
|
||||
(\( literalString, value ) ->
|
||||
if string |> String.startsWith literalString then
|
||||
Just ( value, string |> String.dropLeft (String.length literalString) )
|
||||
|
||||
else
|
||||
Nothing
|
||||
)
|
||||
list
|
||||
|> Maybe.withDefault ( defaultValue, "" )
|
||||
in
|
||||
matchedValue
|
||||
:: extractMatches defaultValue list updatedString
|
@ -1,6 +1,7 @@
|
||||
module GlobTests exposing (all)
|
||||
|
||||
import DataSource.Glob as Glob
|
||||
import DataSource.Internal.Glob
|
||||
import Expect
|
||||
import Test exposing (Test, describe, test)
|
||||
|
||||
@ -199,7 +200,7 @@ expect :
|
||||
-> Expect.Expectation
|
||||
expect filePath { expectedMatch, expectedPattern } glob =
|
||||
glob
|
||||
|> Glob.run filePath
|
||||
|> DataSource.Internal.Glob.run filePath
|
||||
|> Expect.equal
|
||||
{ pattern = expectedPattern
|
||||
, match = expectedMatch
|
||||
@ -216,7 +217,7 @@ expectAll expectedPairs glob =
|
||||
(\( filePath, expectedMatch ) ->
|
||||
( filePath
|
||||
, glob
|
||||
|> Glob.run filePath
|
||||
|> DataSource.Internal.Glob.run filePath
|
||||
|> .match
|
||||
)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user