Skip to content

Commit

Permalink
Chain class renames
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Jul 24, 2024
1 parent 24b317c commit d05d272
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
58 changes: 39 additions & 19 deletions lib/bitstyles_phoenix/bitstyles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ defmodule BitstylesPhoenix.Bitstyles do
@default_version "5.0.1"
@cdn_url "https://cdn.jsdelivr.net/npm/bitstyles"

defguard should_downgrade_from(version, target_version, current_version)
when target_version < version and current_version >= version

def cdn_url do
"#{@cdn_url}@#{version(:string)}"
end
Expand All @@ -14,9 +17,8 @@ defmodule BitstylesPhoenix.Bitstyles do
"""
def classname(name), do: classname(name, version(:tuple))

def classname(class, version) when is_tuple(version) and version > {5, 0, 1} do
IO.warn("Version #{version_to_string(version)} of bitstyles is not yet supported")
class
def classname(class, version) when is_tuple(version) do
downgrade_classname(class, version, version(:tuple))
end

# Note about class renaming:
Expand All @@ -25,11 +27,24 @@ defmodule BitstylesPhoenix.Bitstyles do
# If it does exist, then doing this renaming in the classname/2 function would make it impossible
# for users of older bitstyles to use the "class-name-A" classname.

def classname(class, version) when is_tuple(version) and version >= {5, 0, 0} do
defp downgrade_classname(class, target_version, _current_version)
when target_version > {5, 0, 1} do
IO.warn("Version #{version_to_string(target_version)} of bitstyles is not yet supported")
class
end

defp downgrade_classname(class, target_version, _current_version)
when target_version >= {5, 0, 0} do
class
end

def classname(class, version) when is_tuple(version) and version >= {4, 2, 0} do
defp downgrade_classname(class, target_version, current_version)
when target_version >= current_version do
class
end

defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({5, 0, 0}, target_version, current_version) do
sizes_renaming = %{
"3xs" => "xxxs",
"2xs" => "xxs",
Expand All @@ -56,7 +71,6 @@ defmodule BitstylesPhoenix.Bitstyles do
String.replace(acc, new_border_color, old_border_color)
end)

# class from version 5 getting renamed to work for < 5
class =
case class do
"u-list-none" -> "a-list-reset"
Expand All @@ -68,20 +82,22 @@ defmodule BitstylesPhoenix.Bitstyles do
class -> class
end

classname(class, {5, 0, 0})
downgrade_classname(class, target_version, {4, 2, 0})
end

def classname(class, version) when is_tuple(version) and version >= {4, 0, 0} do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({4, 2, 0}, target_version, current_version) do
mapping =
case class do
"u-border-radius-" <> variant -> "u-round-#{variant}"
_ -> class
end

classname(mapping, {4, 3, 0})
downgrade_classname(mapping, target_version, {4, 0, 0})
end

def classname(class, version) when is_tuple(version) and version >= {2, 0, 0} do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({4, 0, 0}, target_version, current_version) do
# credo:disable-for-previous-line Credo.Check.Refactor.CyclomaticComplexity

mapping =
Expand All @@ -103,42 +119,46 @@ defmodule BitstylesPhoenix.Bitstyles do
"u-border-radius-" <> variant -> "u-round--#{variant}"
"u-overflow-x-auto" -> "u-overflow--x"
"u-overflow-y-auto" -> "u-overflow--y"
"u-version-3" -> "u-version-1-5"
"u-version-4" -> "u-version-2"
_ -> class
end

classname(mapping, {4, 0, 0})
downgrade_classname(mapping, target_version, {2, 0, 0})
end

def classname(class, version) when is_tuple(version) and version >= {1, 5, 0} do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({2, 0, 0}, target_version, current_version) do
mapping =
case class do
"u-flex-shrink-" <> number -> "u-flex__shrink-#{number}"
"u-flex-grow-" <> number -> "u-flex__grow-#{number}"
"u-flex-wrap" -> "u-flex--wrap"
"u-flex-col" -> "u-flex--col"
"u-version-1-5" -> "u-version-1-3"
"u-version-2" -> "u-version-1-5"
_ -> class
end

classname(mapping, {2, 0, 0})
downgrade_classname(mapping, target_version, {1, 5, 0})
end

def classname(class, version) when is_tuple(version) and version >= {1, 3, 0} do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({1, 5, 0}, target_version, current_version) do
mapping =
case class do
"u-grid-cols-" <> number -> "u-grid--#{number}-col"
"u-col-span-" <> number -> "u-grid__col-span-#{number}"
"u-col-start-" <> number -> "u-grid__col-#{number}"
"u-version-1-5" -> "u-version-1-3"
_ -> class
end

classname(mapping, {1, 5, 0})
downgrade_classname(mapping, target_version, {1, 3, 0})
end

def classname(_class, version) when is_tuple(version) do
defp downgrade_classname(_class, target_version, current_version)
when should_downgrade_from({1, 3, 0}, target_version, current_version) do
raise("""
The version #{version_to_string(version)} of bitstyles is not supported. The helpers will produce incorrect classes.
The version #{version_to_string(target_version)} of bitstyles is not supported. The helpers will produce incorrect classes.
Please upgrade bitsyles and set the `bitsyles_version` to the updated version.
""")
end
Expand Down
2 changes: 2 additions & 0 deletions test/bitstyles_phoenix/bitstyles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ defmodule BitstylesPhoenix.BitstylesTest do
)
end

# TODO: delete
test "chained version" do
assert classname("u-version-5-0-0", {5, 0, 1}) == "u-version-5-0-0"
assert classname("u-version-5-0-0", {5, 0, 0}) == "u-version-5-0-0"
assert classname("u-version-5-0-0", {4, 9, 0}) == "u-version-4"
assert classname("u-version-5-0-0", {4, 4, 0}) == "u-version-4"
Expand Down

0 comments on commit d05d272

Please sign in to comment.