Get The Base Name of an Elixir Template File

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












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.







share|improve this question

























    up vote
    5
    down vote

    favorite












    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.







    share|improve this question





















      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      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.







      share|improve this question











      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.









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jan 12 at 14:45









      Onorio Catenacci

      328312




      328312




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          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





          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184958%2fget-the-base-name-of-an-elixir-template-file%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            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





            share|improve this answer



























              up vote
              5
              down vote



              accepted










              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





              share|improve this answer

























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                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





                share|improve this answer















                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






                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 31 at 23:30


























                answered May 31 at 22:22









                RubberDuck

                26.8k454153




                26.8k454153






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184958%2fget-the-base-name-of-an-elixir-template-file%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?