diff --git a/lib/phlex/helpers.rb b/lib/phlex/helpers.rb index f9663470..8674ce79 100644 --- a/lib/phlex/helpers.rb +++ b/lib/phlex/helpers.rb @@ -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 diff --git a/test/phlex/view/mix.rb b/test/phlex/view/mix.rb index 36ec86b1..342fe2ea 100644 --- a/test/phlex/view/mix.rb +++ b/test/phlex/view/mix.rb @@ -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