Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/elixir 1.12 #5

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
70 changes: 28 additions & 42 deletions lib/ex_minimatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,31 @@ defmodule ExMinimatch do
For possible glob patterns and available options, please refer to moduledoc.
"""
def compile(glob), do: compile(glob, %{})

def compile(glob, options) do
options = %{
dot: false,
nocase: false,
match_base: false,
nonegate: false,
noext: false,
noglobstar: false,
nocomment: false,
nobrace: false,
log: nil
} |> Dict.merge(options)
options =
%{
dot: false,
nocase: false,
match_base: false,
nonegate: false,
noext: false,
noglobstar: false,
nocomment: false,
nobrace: false,
log: nil
}
|> Map.merge(options)

ExMinimatch.Compiler.compile_matcher(glob, options)
end

@doc ~S"""
Returns true when file matches the compiled %ExMinimatcher{} struct.
Returns true when file matches the glob. The glob can either be a
compiled %ExMinimatcher{} struct, or, for convenience, a string
that is being compiled on the fly.

This is intended to be used with `compile`
For possible glob patterns and available options, please refer to moduledoc.

## Examples

Expand All @@ -152,34 +157,24 @@ defmodule ExMinimatch do
iex> ["me.jpg", "images/me.png", "images/you.svg"] |> filter(compile("**/*.{png,jpg}"))
["me.jpg", "images/me.png"]

"""
def match(%ExMinimatcher{pattern: pattern}, file) when pattern == [] and file == "", do: true
def match(%ExMinimatcher{pattern: pattern}, _file) when pattern == [], do: false
def match(%ExMinimatcher{} = matcher, file), do: ExMinimatch.Matcher.match_file(file, matcher)

@doc """
Return true if the file matches the glob. This is a convenience function that
is literally `glob |> compile(options) |> match(file)`

Use this for one off matching, as the glob is recompiled every time this is
called.

For possible glob patterns and available options, please refer to moduledoc.

## Examples

iex> match("**/*.png", "qwer.png")
true

iex> match("**/*.png", "qwer/qwer.png")
true

"""
def match(%ExMinimatcher{pattern: pattern}, file) when pattern == [] and file == "", do: true
def match(%ExMinimatcher{pattern: pattern}, _file) when pattern == [], do: false
def match(%ExMinimatcher{} = matcher, file), do: ExMinimatch.Matcher.match_file(file, matcher)
def match(glob, file) when is_binary(glob), do: match(glob, file, %{})
def match(glob, file, options) when is_binary(glob), do: glob |> compile(options) |> match(file)

@doc """
Returns a list of files filtered by the compiled %ExMinimatcher{} struct.
Returns a list of files filtered by the glob. The glob can either be a
compiled %ExMinimatcher{} struct, or, for convenience, a string
that is being compiled on the fly.

For possible glob patterns and available options, please refer to moduledoc.

Note the collection argument comes first, different from `match`. This is
more suitable for piping collections.
Expand All @@ -189,24 +184,15 @@ defmodule ExMinimatch do
iex> ["me.jpg", "images/me.png", "images/you.svg"] |> filter(compile("**/*.{png,jpg}"))
["me.jpg", "images/me.png"]

"""
def filter(files, %ExMinimatcher{} = matcher), do: files |> Enum.filter(&match(matcher, &1))

@doc """
return a list of files that match the given pattern. This is a convenience
function

For possible glob patterns and available options, please refer to moduledoc.

## Examples

iex> filter(["qwer.png", "asdf/qwer.png"], "**/*.png")
["qwer.png", "asdf/qwer.png"]

iex> filter(["qwer/pic1a.png", "qwer/asdf/pic2a.png", "asdf/pic2c.jpg"], "**/*{1..2}{a,b}.{png,jpg}")
["qwer/pic1a.png", "qwer/asdf/pic2a.png"]

"""

def filter(files, %ExMinimatcher{} = matcher), do: files |> Enum.filter(&match(matcher, &1))
def filter(files, pattern) when is_binary(pattern), do: filter(files, pattern, %{})
def filter(files, pattern, options) when is_binary(pattern), do: files |> filter(compile(pattern, options))

Expand Down
Loading