View Source DeepSinker (deep_sinker v1.0.0)

Customizable file traverser.

Link to this section Summary

Functions

Create initial state.

Pop file and update state.

Stream filepaths.

Link to this section Types

@type filepath() :: String.t()
@type handler() :: (item_path() -> item_type())
@type item_path() :: String.t()
@type item_type() :: :file | :directory | :ignore
@type new_option() :: {:order, order()}
@type next_option() :: {:handler, handler()}
@type next_result() :: {:ok, filepath()} | :done
@type order() :: :asc | :desc
@type t() :: %DeepSinker{
  found_but_not_used_yet_items: [item_path()],
  order: order(),
  root_items: [item_path()]
}

State of traversing.

Link to this section Functions

Link to this function

new(root_items, opt \\ [])

View Source
@spec new([item_path()], [new_option()]) :: t()

Create initial state.

examples

Examples

iex> DeepSinker.new(["/path/to/dir1", "/path/to/dir2"])
...> |> is_struct(DeepSinker)
true

iex> DeepSinker.new(["/path/to/dir1", "/path/to/dir2"], order: :desc)
...> |> is_struct(DeepSinker)
true
@spec next(t(), [next_option()]) :: {t(), next_result()}

Pop file and update state.

examples

Examples

iex> DeepSinker.new(["./"])
...> |> DeepSinker.next()
...> # {state, {:ok, filepath}} | {state, :done}

iex> DeepSinker.new(["./"])
...> |> DeepSinker.next(
...>   handler: fn item_path ->
...>     cond do
...>       String.ends_with?(item_path, ".git") -> :ignore
...>       String.contains?(item_path, ".") -> :file
...>       true -> :directory
...>     end
...>   end
...> )
...> # {state, {:ok, filepath}} | {state, :done}
Link to this function

stream(state, opt \\ [])

View Source
@spec stream(t(), [next_option()]) :: Enumerable.t(filepath())

Stream filepaths.

examples

Examples

iex> DeepSinker.new(["./"])
...> # |> DeepSinker.stream()
...> # |> Enum.to_list()
...> # ["list", "of" "filepaths"]

iex> DeepSinker.new(["./"])
...> |> DeepSinker.stream(
...>   handler: fn item_path ->
...>     cond do
...>       String.ends_with?(item_path, ".git") -> :ignore
...>       String.contains?(item_path, ".") -> :file
...>       true -> :directory
...>     end
...>   end
...> )
...> # ["list", "of" "filepaths"]