Skip to content

Commit

Permalink
Add support for using the mix helper with different types (#620)
Browse files Browse the repository at this point in the history
See #613 for examples where this is necessary.
  • Loading branch information
willcosgrove committed Jan 30, 2024
1 parent a910442 commit 0c00be1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/phlex/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,23 @@ def mix(*args)
when Hash
old.is_a?(Hash) ? mix(old, new) : new
when Array
old.is_a?(Array) ? (old + new) : new
case old
when Array then old + new
when Set then old.to_a + new
when Hash then new
else
[old] + new
end
when Set
case old
when Set then old + new
when Array then old + new.to_a
when Hash then new
else
new + [old]
end
when String
old.is_a?(String) ? "#{old} #{new}" : new
old.is_a?(String) ? "#{old} #{new}" : old + old.class[new]
else
new
end
Expand Down
30 changes: 30 additions & 0 deletions test/phlex/view/mix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,34 @@

expect(output).to be == { data: { controller: "bar" } }
end

it "supports mixing between arrays and strings" do
output = mix({ class: ["foo"] }, { class: "bar" })

expect(output).to be == { class: ["foo", "bar"] }

output = mix({ class: "foo" }, { class: ["bar"] })

expect(output).to be == { class: ["foo", "bar"] }
end

it "supports mixing between sets and strings" do
output = mix({ class: Set["foo"] }, { class: "bar" })

expect(output).to be == { class: Set["foo", "bar"] }

output = mix({ class: "foo" }, { class: Set["bar"] })

expect(output).to be == { class: Set["foo", "bar"] }
end

it "supports mixing between arrays and sets, keeping the less restrictive type" do
output = mix({ class: ["foo"] }, { class: Set["bar"] })

expect(output).to be == { class: ["foo", "bar"] }

output = mix({ class: Set["foo"] }, { class: ["bar"] })

expect(output).to be == { class: ["foo", "bar"] }
end
end

0 comments on commit 0c00be1

Please sign in to comment.