Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using the mix helper with different types #620

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/phlex/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,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