Skip to content

Commit 207c626

Browse files
author
José Valim
committed
Merge pull request #2057 from jwarwick/mix_underscore
Mix.Utils handling empty strings
2 parents c881344 + 7308d81 commit 207c626

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/mix/lib/mix/utils.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ defmodule Mix.Utils do
194194
underscore(rest)
195195
end
196196

197+
def underscore(""), do: ""
198+
197199
def underscore(<<h, t :: binary>>) do
198200
<<to_lower_char(h)>> <> do_underscore(t, h)
199201
end
@@ -210,6 +212,12 @@ defmodule Mix.Utils do
210212
<<?_>> <> do_underscore(t, ?-)
211213
end
212214

215+
defp do_underscore(<< "..", t :: binary>>, _) do
216+
<<"..">> <> underscore(t)
217+
end
218+
219+
defp do_underscore(<<?.>>, _), do: <<?.>>
220+
213221
defp do_underscore(<<?., t :: binary>>, _) do
214222
<<?/>> <> underscore(t)
215223
end
@@ -230,6 +238,8 @@ defmodule Mix.Utils do
230238
Mix.Utils.camelize "foo_bar" #=> "FooBar"
231239
232240
"""
241+
def camelize(""), do: ""
242+
233243
def camelize(<<?_, t :: binary>>) do
234244
camelize(t)
235245
end

lib/mix/test/mix/utils_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ defmodule Mix.UtilsTest do
5252
assert Mix.Utils.underscore("FoBaZa") == "fo_ba_za"
5353
assert Mix.Utils.underscore("Foo.Bar") == "foo/bar"
5454
assert Mix.Utils.underscore(Foo.Bar) == "foo/bar"
55+
assert Mix.Utils.underscore("") == ""
56+
assert Mix.Utils.underscore("..") == ".."
57+
assert Mix.Utils.underscore("/..") == "/.."
58+
assert Mix.Utils.underscore("foo/../bar") == "foo/../bar"
5559
end
5660

5761
test :camelize do
@@ -63,6 +67,7 @@ defmodule Mix.UtilsTest do
6367
assert Mix.Utils.camelize("_foo") == "Foo"
6468
assert Mix.Utils.camelize("foo__bar") == "FooBar"
6569
assert Mix.Utils.camelize("foo/bar") == "Foo.Bar"
70+
assert Mix.Utils.camelize("") == ""
6671
end
6772

6873
test :extract_files do

0 commit comments

Comments
 (0)