-
-
Save djbender/3837125 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Extensions | |
module_function | |
def separate(path) | |
extensions = path.reverse | |
.scan(/\G\w+\./) | |
.map { |e| e[0..-2].reverse } | |
.reverse | |
file_name = path.split(".")[0..-(extensions.size + 1)].join(".") | |
[file_name, extensions] | |
end | |
end | |
if $PROGRAM_NAME == __FILE__ | |
require "minitest/autorun" | |
class SeparateExtensions < MiniTest::Unit::TestCase | |
def test_extensions_are_pulled_off_the_end | |
assert_equal( | |
["some file name with", ["dots", "html", "markdown"]], | |
Extensions.separate("some file name with.dots.html.markdown") | |
) | |
end | |
def test_sections_with_spaces_are_ignored | |
assert_equal( | |
["some file name.with dots", ["html", "markdown"]], | |
Extensions.separate("some file name.with dots.html.markdown") | |
) | |
end | |
def test_treats_the_leading_section_as_part_of_the_file_name | |
assert_equal( | |
["some.file name with.multiple dots", ["html", "markdown"]], | |
Extensions.separate("some.file name with.multiple dots.html.markdown") | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment