From 97c70ec4ed246055b82a6fffa98f636ef4d3c915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Tom=C3=A1s?= Date: Sun, 20 Aug 2023 09:04:26 +0100 Subject: [PATCH] Add known_types/0 function that lists all known types (#79) --- lib/mime.ex | 16 ++++++++++++++++ test/mime_test.exs | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/mime.ex b/lib/mime.ex index dfa736f..0126725 100644 --- a/lib/mime.ex +++ b/lib/mime.ex @@ -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. diff --git a/test/mime_test.exs b/test/mime_test.exs index d425a2e..d5cbd4c 100644 --- a/test/mime_test.exs +++ b/test/mime_test.exs @@ -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"))