Skip to content
Browse files

File.join and File.split

  • Loading branch information...
1 parent 6021398 commit 7546e0c2751f89428e35f373a45314f6fe5452bb @bellthoven bellthoven committed Mar 13, 2012
Showing with 50 additions and 1 deletion.
  1. +35 −0 lib/file.ex
  2. +15 −1 test/elixir/file_test.exs
View
35 lib/file.ex
@@ -53,6 +53,41 @@ defmodule File do
FN.basename(path, extension)
end
+ @doc """
+ Returns a string with one or more paths components joint by the path separator.
+ This function should be used to convert a list of strings in a path.
+
+ ## Examples
+
+ File.join(["~", "foo"])
+ #=> "~/foo"
+ File.join(["foo"])
+ #=> "foo"
+ File.join(["/", "foo", "bar"])
+ #=> "/foo/bar"
+ """
+ def join(paths) do
+ FN.join(paths)
+ end
+
+ @doc """
+ Returns a list with the path splitted by the path separator. If an empty string
+ is given, then it returns the root path.
+
+ ## Examples
+
+ File.split("")
+ #=> ["/"]
+ File.split("foo")
+ #=> ["foo"]
+ File.split("/foo/bar")
+ #=> ["/", "foo", "bar"]
+
+ """
+ def split(path) do
+ FN.split(path)
+ end
+
# Points to Elixir wildcard version that also handles "**".
def wildcard(path, relative_to // '.') do
Erlang.elixir_glob.wildcard(path, relative_to)
View
16 test/elixir/file_test.exs
@@ -29,8 +29,22 @@ defmodule FileTest do
assert_equal "bar", File.basename("/foo/bar")
assert_equal "", File.basename("/")
- assert_equal "bar" , File.basename("~/foo/bar.ex", ".ex")
+ assert_equal "bar", File.basename("~/foo/bar.ex", ".ex")
assert_equal "bar.exs", File.basename("~/foo/bar.exs", ".ex")
assert_equal "bar.old", File.basename("~/for/bar.old.ex", ".ex")
end
+
+ test :join do
+ assert_equal "", File.join([""])
+ assert_equal "foo", File.join(["foo"])
+ assert_equal "/foo/bar", File.join(["/", "foo", "bar"])
+ assert_equal "~/foo/bar", File.join(["~", "foo", "bar"])
+ end
+
+ test :split do
+ assert_equal ["/"], File.split("")
+ assert_equal ["foo"], File.split("foo")
+ assert_equal ["/", "foo", "bar"], File.split("/foo/bar")
+ end
+
end

0 comments on commit 7546e0c

Please sign in to comment.
Something went wrong with that request. Please try again.