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

Add .formatter.exs and run it with mix format #125

Open
wants to merge 1 commit into
base: upgrade-versions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
2 changes: 1 addition & 1 deletion lib/crutches.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Crutches do
"""
defmacro __using__(_opts) do
quote do
alias Crutches, as: C
alias Crutches, as: C
alias Crutches.Format, as: F
end
end
Expand Down
15 changes: 8 additions & 7 deletions lib/crutches/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Crutches.Enum do
Simply call any function (with any options if applicable) to make use of it.
"""

@type t :: Enumerable.t
@type t :: Enumerable.t()
@type element :: any

@doc ~S"""
Expand All @@ -28,10 +28,10 @@ defmodule Crutches.Enum do
"""
@spec without(list(any), list(any)) :: list(any)
def without(collection, elements) when is_list(collection) do
if Keyword.keyword? collection do
Keyword.drop collection, elements
if Keyword.keyword?(collection) do
Keyword.drop(collection, elements)
else
Enum.reject collection, &Enum.member?(elements, &1)
Enum.reject(collection, &Enum.member?(elements, &1))
end
end

Expand Down Expand Up @@ -94,7 +94,7 @@ defmodule Crutches.Enum do
@spec none?(t) :: boolean
@spec none?(t, (element -> as_boolean(term))) :: boolean

def none?(enumerable, fun \\ fn(x) -> x end)
def none?(enumerable, fun \\ fn x -> x end)

def none?(enumerable, fun) do
not Enum.any?(enumerable, fun)
Expand Down Expand Up @@ -127,8 +127,8 @@ defmodule Crutches.Enum do
"""
@spec one?(t) :: boolean
@spec one?(t, (element -> as_boolean(term))) :: boolean
def one?(enumerable, fun \\ fn(x) -> x end) do
match? [_], Stream.filter(enumerable, fun) |> Enum.take(2)
def one?(enumerable, fun \\ fn x -> x end) do
match?([_], Stream.filter(enumerable, fun) |> Enum.take(2))
end

@doc ~S"""
Expand All @@ -150,6 +150,7 @@ defmodule Crutches.Enum do
def compact(list) when is_list(list) do
Enum.reject(list, &is_nil(&1))
end

def compact(map) when is_map(map) do
for {key, value} <- map, !is_nil(value), do: {key, value}, into: %{}
end
Expand Down
17 changes: 8 additions & 9 deletions lib/crutches/format/integer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@ defmodule Crutches.Format.Integer do
iex> Integer.ordinal(-23)
"rd"
"""
@spec ordinal(integer) :: String.t
@spec ordinal(integer) :: String.t()
def ordinal(n) when is_integer(n) do
cond do
n == 1 -> "st"
n == 2 -> "nd"
n == 3 -> "rd"
n in 0..13 -> "th"
n == 1 -> "st"
n == 2 -> "nd"
n == 3 -> "rd"
n in 0..13 -> "th"
n in 14..99 -> n |> rem(10) |> abs |> ordinal
true -> n |> rem(100) |> abs |> ordinal
true -> n |> rem(100) |> abs |> ordinal
end
end


@doc ~S"""
Return `n` and it's ordinal as a string.

Expand All @@ -56,6 +55,6 @@ defmodule Crutches.Format.Integer do
iex> Integer.ordinalize(-8)
"-8th"
"""
@spec ordinalize(integer) :: String.t
@spec ordinalize(integer) :: String.t()
def ordinalize(n) when is_integer(n), do: Integer.to_string(n) <> ordinal(n)
end
end
8 changes: 5 additions & 3 deletions lib/crutches/format/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ defmodule Crutches.Format.List do
]
]

@spec as_sentence(list(any)) :: String.t
@spec as_sentence(list(any)) :: String.t()
def as_sentence(words, opts \\ @as_sentence[:defaults])
def as_sentence([], _), do: ""
def as_sentence([], _), do: ""
def as_sentence([word], _), do: "#{word}"

def as_sentence([first, last], opts) do
opts = Option.combine!(opts, @as_sentence)

first <> opts[:two_words_connector] <> last
end

def as_sentence(words, opts) when is_list(words) do
opts = Option.combine!(opts, @as_sentence)

Expand All @@ -80,7 +82,7 @@ defmodule Crutches.Format.List do
{:ok, shortened_list} -> Enum.join(shortened_list, opts[:words_connector])
_ -> []
end

last = List.last(words)

init <> opts[:last_word_connector] <> last
Expand Down
16 changes: 9 additions & 7 deletions lib/crutches/integer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Crutches.Integer do
Convenience functions for Integers.
"""


@doc ~S"""
Check whether `n` is evenly divisible by `divisor`.

Expand Down Expand Up @@ -37,7 +36,9 @@ defmodule Crutches.Integer do
def is_prime?(int) do
is_prime?(int, 2, :math.sqrt(int))
end

def is_prime?(_int, divisor, limit) when divisor > limit, do: true

def is_prime?(int, divisor, limit) do
cond do
rem(int, divisor) === 0 -> false
Expand All @@ -61,20 +62,21 @@ defmodule Crutches.Integer do
"""
# TODO remove this when we drop support for Elixir < 1.1
try do
Elixir.Integer.digits(1140392)
Elixir.Integer.digits(1_140_392)
def digits(n), do: digits(n, 10)
defdelegate digits(n, base), to: Elixir.Integer
rescue
UndefinedFunctionError ->
def digits(n, base \\ 10) when is_integer(n) and n >= 0
and is_integer(base) and base >= 2 do
def digits(n, base \\ 10)
when is_integer(n) and n >= 0 and is_integer(base) and base >= 2 do
do_digits(n, base, [])
end

defp do_digits(0, _base, []), do: [0]
defp do_digits(0, _base, []), do: [0]
defp do_digits(0, _base, acc), do: acc
defp do_digits(n, base, acc) do
do_digits div(n, base), base, [rem(n, base) | acc]

defp do_digits(n, base, acc) do
do_digits(div(n, base), base, [rem(n, base) | acc])
end
end
end
32 changes: 20 additions & 12 deletions lib/crutches/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ defmodule Crutches.List do
shorten(list, amount, length(list))
end

defp shorten(_, amount, len) when len < amount,
do: {:error, "Amount to shorten by is greater than the length of the list"}
defp shorten(_, amount, len) when len < amount,
do: {:error, "Amount to shorten by is greater than the length of the list"}

defp shorten(list, amount, len) do
shortened_list = Enum.take(list, len - amount)
{:ok, shortened_list}
Expand Down Expand Up @@ -119,16 +119,20 @@ defmodule Crutches.List do
"""
@spec split(list(any), any) :: list(any)
def split([], _), do: [[]]

def split(collection, predicate) when not is_function(predicate) do
split(collection, &(&1 == predicate))
end

def split(collection, predicate) do
{head, tail} = List.foldr collection, {[], []}, fn elem, {head, acc} ->
case predicate.(elem) do
true -> {[], [head | acc]}
false -> {[elem | head], acc}
end
end
{head, tail} =
List.foldr(collection, {[], []}, fn elem, {head, acc} ->
case predicate.(elem) do
true -> {[], [head | acc]}
false -> {[elem | head], acc}
end
end)

[head] ++ tail
end

Expand Down Expand Up @@ -166,6 +170,7 @@ defmodule Crutches.List do
[["1", "2", "3"], ["4", "5", "6"], ["7", "8", "a"]]
"""
def in_groups(collection, number, elem \\ nil)

def in_groups(collection, number, elem) when is_function(elem) do
in_groups(collection, number, nil, elem)
end
Expand All @@ -177,7 +182,7 @@ defmodule Crutches.List do

{result, _} =
Enum.to_list(1..number)
|> Enum.reduce({[], collection}, fn(x, acc) ->
|> Enum.reduce({[], collection}, fn x, acc ->
{list, kollection} = acc

if x <= group_rem do
Expand All @@ -186,17 +191,20 @@ defmodule Crutches.List do
case group_rem do
0 ->
{[Enum.take(kollection, group_min) | list], Enum.drop(kollection, group_min)}

_ ->
case elem do
false ->
{[Enum.take(kollection, group_min) | list], Enum.drop(kollection, group_min)}

_ ->
{[(Enum.take(kollection, group_min) |> Enum.concat([elem])) | list], Enum.drop(kollection, group_min)}
{[Enum.take(kollection, group_min) |> Enum.concat([elem]) | list],
Enum.drop(kollection, group_min)}
end
end
end
end)

Enum.reverse(result)
Enum.reverse(result)
end
end
20 changes: 13 additions & 7 deletions lib/crutches/map.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Crutches.Map do
require Logger

@moduledoc ~s"""
Convenience functions for maps.

Expand All @@ -25,12 +26,16 @@ defmodule Crutches.Map do
"""
def dkeys_update(map, fun), do: dkeys_update(map, fun, %{})
defp dkeys_update(map, _, acc) when map == %{}, do: acc

defp dkeys_update(map, fun, acc) do
key = Map.keys(map) |> List.first
value = case is_map(map[key]) do
true -> dkeys_update(map[key], fun)
_ -> map[key]
end
key = Map.keys(map) |> List.first()

value =
case is_map(map[key]) do
true -> dkeys_update(map[key], fun)
_ -> map[key]
end

dkeys_update(Map.delete(map, key), fun, Map.put(acc, fun.(key), value))
end

Expand All @@ -45,7 +50,7 @@ defmodule Crutches.Map do

"""
@spec filter(map, ({key, value} -> as_boolean(term))) :: map
def filter(map, fun), do: :maps.filter(fn(k, v) -> fun.({k, v}) end, map)
def filter(map, fun), do: :maps.filter(fn k, v -> fun.({k, v}) end, map)

@doc ~S"""
Filters the map, i.e. returns only elements
Expand All @@ -58,7 +63,7 @@ defmodule Crutches.Map do

"""
@spec reject(map, ({key, value} -> as_boolean(term))) :: map
def reject(map, fun), do: :maps.filter(fn(k, v) -> !fun.({k, v}) end, map)
def reject(map, fun), do: :maps.filter(fn k, v -> !fun.({k, v}) end, map)

@doc """
Invert a map. Duplicate values raises an error.
Expand Down Expand Up @@ -90,6 +95,7 @@ defmodule Crutches.Map do
%{"foo" => "bar", "baz" => %{qux: 1}}
"""
def shallow_stringify_keys(nil), do: nil

def shallow_stringify_keys(map) when is_map(map) do
for {k, v} <- map, do: {Atom.to_string(k), v}, into: %{}
end
Expand Down
14 changes: 9 additions & 5 deletions lib/crutches/option.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Crutches.Option do
@type t(value) :: [{key, value}]

@type ok(value) :: {:ok, value}
@type error ::{:error, any}
@type error :: {:error, any}

@doc """
Validates the `opts` keyword list according to `config`, combines defaults.
Expand Down Expand Up @@ -148,7 +148,9 @@ defmodule Crutches.Option do
@spec combine!(t, t | t(t), (t, t -> t)) :: t
def combine!(opts, config, combinator) do
case combine(opts, config, combinator) do
{:ok, opts} -> opts
{:ok, opts} ->
opts

{:error, invalid} ->
invalid = invalid |> Enum.join(" ")
raise ArgumentError, message: "invalid key #{invalid}"
Expand Down Expand Up @@ -196,7 +198,9 @@ defmodule Crutches.Option do
@spec validate!(t, [atom]) :: true
def validate!(opts, valid) do
case validate(opts, valid) do
{:ok, _} -> true
{:ok, _} ->
true

{:error, invalid_options} ->
raise ArgumentError, "invalid key " <> Enum.join(invalid_options, ", ")
end
Expand All @@ -222,7 +226,7 @@ defmodule Crutches.Option do

defp invalid_options(opts, valid) do
opts
|> Keyword.keys
|> Enum.reject(& &1 in valid)
|> Keyword.keys()
|> Enum.reject(&(&1 in valid))
end
end
16 changes: 9 additions & 7 deletions lib/crutches/protocols/blankable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end

defimpl Crutches.Protocols.Blankable, for: List do
def blank?([]), do: true
def blank?(_), do: false
def blank?(_), do: false
end

defimpl Crutches.Protocols.Blankable, for: Map do
Expand All @@ -29,19 +29,21 @@ defimpl Crutches.Protocols.Blankable, for: BitString do

@doc "Optimise this common use case"
def blank?(""), do: true

def blank?(string) do
Regex.match?(@blank_regex, string)
end
end

defimpl Crutches.Protocols.Blankable, for: Any do
def blank?(nil), do: true
def blank?(false), do: true
def blank?(atom) when is_atom(atom), do: false
def blank?(function) when is_function(function), do: false
def blank?(number) when is_number(number), do: false
def blank?(nil), do: true
def blank?(false), do: true
def blank?(atom) when is_atom(atom), do: false
def blank?(function) when is_function(function), do: false
def blank?(number) when is_number(number), do: false

def blank?(term) do
try do
try do
Enum.empty?(term)
rescue
Protocol.UndefinedError -> false
Expand Down
Loading