Skip to content

Commit

Permalink
Merge pull request #66 from chasefleming/chasefleming/65
Browse files Browse the repository at this point in the history
Add `styles.Merge` for combining styles
  • Loading branch information
chasefleming authored Nov 7, 2023
2 parents 4b4da3f + 311c8dd commit 9eabdf5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions styles/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package styles

// Merge combines multiple styles.Props maps into one, with later styles overriding earlier ones.
func Merge(styleMaps ...Props) Props {
mergedStyles := Props{}
for _, styleMap := range styleMaps {
for key, value := range styleMap {
mergedStyles[key] = value
}
}
return mergedStyles
}
56 changes: 56 additions & 0 deletions styles/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package styles

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMerge(t *testing.T) {
baseStyle := Props{
Width: "100px",
Color: "blue",
}

additionalStyle := Props{
Color: "red", // This should override the blue color in baseStyle
BackgroundColor: "yellow",
}

expectedMergedStyle := Props{
Width: "100px",
Color: "red",
BackgroundColor: "yellow",
}

mergedStyle := Merge(baseStyle, additionalStyle)

assert.Equal(t, expectedMergedStyle, mergedStyle)
}

func TestMergeThreeStyles(t *testing.T) {
baseStyle := Props{
Padding: "10px",
Margin: "5px",
}

secondaryStyle := Props{
Margin: "10px", // This should override the baseStyle margin
Color: "red",
}

tertiaryStyle := Props{
Color: "blue", // This should override the secondaryStyle color
Border: "1px solid black",
}

mergedStyle := Merge(baseStyle, secondaryStyle, tertiaryStyle)

expectedStyle := Props{
Padding: "10px",
Margin: "10px", // From secondaryStyle
Color: "blue", // From tertiaryStyle
Border: "1px solid black",
}

assert.Equal(t, expectedStyle, mergedStyle)
}

0 comments on commit 9eabdf5

Please sign in to comment.