-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from chasefleming/chasefleming/65
Add `styles.Merge` for combining styles
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |