5
\$\begingroup\$

I've got the following Elixir code:

defmodule T do
    @eex_extension "eex"
    @file_separator "."
    def get_base_file_name_from_template_name(template_file_name) do
        [base, extension, @eex_extension] = String.split(template_file_name,@file_separator)
        base <> @file_separator <> extension
    end
end  

It works like this:

iex(3)> f = T.get_base_file_name_from_template_name("index.html.eex")
"index.html"

1.) Is there a built-in library function I'm missing that would do this?

2.) Is there a built-in attribute for the eex file extension?

3.) Is there an OS built-in value for the "." BTW, I think file separator is not a great name for that but I can't come up with a better name.

Thoughts, comments, suggestions would be greatly appreciated. Even better yet if this is a built-in function in one of the Elixir libs and I've missed it a pointer to it would be most welcome.

\$\endgroup\$
5
\$\begingroup\$

Since you seem to know what the file extension is, you can use Path.basename/2.

"foo.html.eex"
  |> Path.basename(".eex")
  |> IO.puts

If you don’t know the file extension, you can use Path.extname/1 in conjunction.

filename = "foo.html.eex"
filename
  |> Path.basename(Path.extname(filename))
  |> IO.puts
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.