Skip to content

Commit

Permalink
Add known_types/0 function that lists all known types (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Aug 20, 2023
1 parent e61d907 commit 97c70ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/mime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ defmodule MIME do
unquote(Macro.escape(custom_types))
end

@doc """
Returns a mapping of all known types to their extensions,
including custom types compiled into the MIME module.
## Examples
known_types()
#=> %{"application/json" => ["json"], ...}
"""
@doc since: "2.1.0"
@spec known_types() :: %{required(String.t()) => [String.t()]}
def known_types do
unquote(Macro.escape(all_types))
end

@doc """
Returns the extensions associated with a given MIME type.
Expand Down
13 changes: 13 additions & 0 deletions test/mime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ defmodule MIMETest do
assert from_path("image.psd") == "image/vnd.adobe.photoshop"
end

test "known_types/0" do
Application.put_env(:mime, :types, %{"application/dicom" => ["dcm"]})
recompile!()

assert is_map(known_types())

assert Map.has_key?(known_types(), "application/json")
assert Map.has_key?(known_types(), "application/dicom")

assert Map.get(known_types(), "application/json") == ["json"]
assert Map.get(known_types(), "application/dicom") == ["dcm"]
end

test "types map is sorted" do
quoted = Code.string_to_quoted!(File.read!("lib/mime.ex"))

Expand Down

0 comments on commit 97c70ec

Please sign in to comment.