Skip to content

Commit

Permalink
refact: is_* functions have no prefix and end with a question mark(#4)
Browse files Browse the repository at this point in the history
To conform with elixir's conventions

Note the convention in Elixir is to name functions/macros allowed in guards with the is_ prefix, such as is_list/1. If, however, the function/macro returns a boolean and is not allowed in guards, it should have no prefix and end with a question mark, such as Keyword.keyword?/1.

https://hexdocs.pm/elixir/Kernel.html#defguard/1
  • Loading branch information
daskycodes authored Jan 13, 2022
1 parent 61404b4 commit 090c050
Show file tree
Hide file tree
Showing 14 changed files with 540 additions and 535 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0 (2022-01-09)

* Changes
* `is_*` functions have no prefix and end with a question mark, to conform with elixir's conventions

## 0.1.1 (2022-01-09)

* Enhancements
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ by adding `infer` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:infer, "~> 0.1.1"}
{:infer, "~> 0.2.0"}
]
end
```
Expand All @@ -29,7 +29,7 @@ Takes the binary file contents as argument and returns the `Infer.Type.t()` if t
```elixir
iex> binary = File.read!("test/images/sample.png")
iex> Infer.get(binary)
%Infer.Type{extension: "png", matcher: &Infer.Image.is_png/1, matcher_type: :image, mime_type: "image/png"}
%Infer.Type{extension: "png", matcher: &Infer.Image.png?/1, matcher_type: :image, mime_type: "image/png"}
```

### `Infer.get_from_path/1`
Expand All @@ -38,58 +38,58 @@ Similar to `Infer.get/1`, but takes the file path as argument.

```elixir
iex> Infer.get_from_path("test/images/sample.png")
%Infer.Type{extension: "png", matcher: &Infer.Image.is_png/1, matcher_type: :image, mime_type: "image/png"}
%Infer.Type{extension: "png", matcher: &Infer.Image.png?/1, matcher_type: :image, mime_type: "image/png"}
```

### `Infer.is/2`
### `Infer.is?/2`

Takes the binary content and the file extension as arguments. Returns whether the file content is
of the given extension.

```elixir
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is(binary, "png")
iex> Infer.is?(binary, "png")
true
```

### `Infer.is_mime/2`
### `Infer.mime?/2`

Takes the binary content and the file extension as arguments. Returns whether the file content is
of the given mime type.

```elixir
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_mime(binary, "image/png")
iex> Infer.mime?(binary, "image/png")
true
```

### `Infer.is_image/1`
### `Infer.image?/1`

Takes the binary file contents as argument and returns whether the file is an image or not.

```elixir
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_image(binary)
iex> Infer.image?(binary)
true
```

### `Infer.is_document/1`
### `Infer.document?/1`

Takes the binary file contents as argument and returns whether the file is a document (microsoft office, open office)

```elixir
iex> binary = File.read!("test/docs/sample.xlsx")
iex> Infer.is_document(binary)
iex> Infer.document?(binary)
true
```

### `Infer.Doc.is_docx/1`
### `Infer.Doc.docx?/1`

Takes the binary file contents as arguments. Returns `true` if it's Microsoft Word Open XML Format Document (DOCX) data.

```elixir
iex> binary = File.read!("test/docs/sample.docx")
iex> Infer.Doc.is_docx(binary)
iex> Infer.Doc.docx?(binary)
true
```

Expand Down
96 changes: 48 additions & 48 deletions lib/infer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Infer do
iex> binary = File.read!("test/images/sample.png")
iex> Infer.get(binary)
%Infer.Type{extension: "png", matcher: &Infer.Image.is_png/1, matcher_type: :image, mime_type: "image/png"}
%Infer.Type{extension: "png", matcher: &Infer.Image.png?/1, matcher_type: :image, mime_type: "image/png"}
"""
@spec get(binary()) :: Infer.Type.t() | nil
Expand All @@ -37,7 +37,7 @@ defmodule Infer do
## Examples
iex> Infer.get_from_path("test/images/sample.png")
%Infer.Type{extension: "png", matcher: &Infer.Image.is_png/1, matcher_type: :image, mime_type: "image/png"}
%Infer.Type{extension: "png", matcher: &Infer.Image.png?/1, matcher_type: :image, mime_type: "image/png"}
"""
@spec get_from_path(binary()) :: Infer.Type.t() | nil
Expand All @@ -53,12 +53,12 @@ defmodule Infer do
## Examples
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is(binary, "png")
iex> Infer.is?(binary, "png")
true
"""
@spec is(binary(), Infer.Type.extension()) :: boolean()
def is(binary, extension), do: Enum.any?(@matchers, &(&1.extension == extension && &1.matcher.(binary)))
@spec is?(binary(), Infer.Type.extension()) :: boolean()
def is?(binary, extension), do: Enum.any?(@matchers, &(&1.extension == extension && &1.matcher.(binary)))

@doc """
Takes the binary content and the file extension as arguments. Returns whether the file content is
Expand All @@ -67,178 +67,178 @@ defmodule Infer do
## Examples
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_mime(binary, "image/png")
iex> Infer.mime?(binary, "image/png")
true
"""
@spec is_mime(binary(), Infer.Type.mime_type()) :: boolean()
def is_mime(binary, mime_type), do: Enum.any?(@matchers, &(&1.mime_type == mime_type && &1.matcher.(binary)))
@spec mime?(binary(), Infer.Type.mime_type()) :: boolean()
def mime?(binary, mime_type), do: Enum.any?(@matchers, &(&1.mime_type == mime_type && &1.matcher.(binary)))

@doc """
Returns whether the given extension is supported.
## Examples
iex> Infer.is_supported("png")
iex> Infer.supported?("png")
true
"""
@spec is_supported(Infer.Type.extension()) :: boolean()
def is_supported(extension), do: Enum.any?(@matchers, &(&1.extension == extension))
@spec supported?(Infer.Type.extension()) :: boolean()
def supported?(extension), do: Enum.any?(@matchers, &(&1.extension == extension))

@doc """
Returns whether the given mime type is supported.
## Examples
iex> Infer.is_mime_supported("image/png")
iex> Infer.mime_supported?("image/png")
true
"""
@spec is_mime_supported(Infer.Type.mime_type()) :: boolean()
def is_mime_supported(mime_type), do: Enum.any?(@matchers, &(&1.mime_type == mime_type))
@spec mime_supported?(Infer.Type.mime_type()) :: boolean()
def mime_supported?(mime_type), do: Enum.any?(@matchers, &(&1.mime_type == mime_type))

@doc """
Takes the binary file contents as argument and returns whether the file is an application or not.
## Examples
iex> binary = File.read!("test/app/sample.wasm")
iex> Infer.is_app(binary)
iex> Infer.app?(binary)
true
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_app(binary)
iex> Infer.app?(binary)
false
"""
@spec is_app(binary()) :: boolean()
def is_app(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :app && &1.matcher.(binary)))
@spec app?(binary()) :: boolean()
def app?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :app && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is an archive or not.
## Examples
iex> binary = File.read!("test/archives/sample.zip")
iex> Infer.is_archive(binary)
iex> Infer.archive?(binary)
true
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_archive(binary)
iex> Infer.archive?(binary)
false
"""
@spec is_archive(binary()) :: boolean()
def is_archive(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :archive && &1.matcher.(binary)))
@spec archive?(binary()) :: boolean()
def archive?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :archive && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is an archive or not.
## Examples
iex> binary = File.read!("test/audio/sample.mp3")
iex> Infer.is_audio(binary)
iex> Infer.audio?(binary)
true
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_audio(binary)
iex> Infer.audio?(binary)
false
"""
@spec is_audio(binary()) :: boolean()
def is_audio(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :audio && &1.matcher.(binary)))
@spec audio?(binary()) :: boolean()
def audio?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :audio && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is an book (epub or mobi) or not.
## Examples
iex> binary = File.read!("test/books/sample.epub")
iex> Infer.is_book(binary)
iex> Infer.book?(binary)
true
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_book(binary)
iex> Infer.book?(binary)
false
"""
@spec is_book(binary()) :: boolean()
def is_book(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :book && &1.matcher.(binary)))
@spec book?(binary()) :: boolean()
def book?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :book && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is a document (microsoft office, open office)
## Examples
iex> binary = File.read!("test/docs/sample.xlsx")
iex> Infer.is_document(binary)
iex> Infer.document?(binary)
true
iex> binary = File.read!("test/docs/sample.pptx")
iex> Infer.is_document(binary)
iex> Infer.document?(binary)
true
iex> binary = File.read!("test/docs/sample.odp")
iex> Infer.is_document(binary)
iex> Infer.document?(binary)
true
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_document(binary)
iex> Infer.document?(binary)
false
"""
@spec is_document(binary()) :: boolean()
def is_document(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :doc && &1.matcher.(binary)))
@spec document?(binary()) :: boolean()
def document?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :doc && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is a font or not.
## Examples
iex> binary = File.read!("test/fonts/sample.ttf")
iex> Infer.is_font(binary)
iex> Infer.font?(binary)
true
iex> binary = File.read!("test/app/sample.wasm")
iex> Infer.is_font(binary)
iex> Infer.font?(binary)
false
"""
@spec is_font(binary()) :: boolean()
def is_font(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :font && &1.matcher.(binary)))
@spec font?(binary()) :: boolean()
def font?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :font && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is an image or not.
## Examples
iex> binary = File.read!("test/images/sample.png")
iex> Infer.is_image(binary)
iex> Infer.image?(binary)
true
iex> binary = File.read!("test/app/sample.wasm")
iex> Infer.is_image(binary)
iex> Infer.image?(binary)
false
"""
@spec is_image(binary()) :: boolean()
def is_image(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :image && &1.matcher.(binary)))
@spec image?(binary()) :: boolean()
def image?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :image && &1.matcher.(binary)))

@doc """
Takes the binary file contents as argument and returns whether the file is a video or not.
## Examples
iex> binary = File.read!("test/videos/sample.mp4")
iex> Infer.is_video(binary)
iex> Infer.video?(binary)
true
iex> binary = File.read!("test/app/sample.wasm")
iex> Infer.is_video(binary)
iex> Infer.video?(binary)
false
"""
@spec is_video(binary()) :: boolean()
def is_video(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :video && &1.matcher.(binary)))
@spec video?(binary()) :: boolean()
def video?(binary), do: Enum.any?(@matchers, &(&1.matcher_type == :video && &1.matcher.(binary)))
end
Loading

0 comments on commit 090c050

Please sign in to comment.