From 7d64493b23a5355b8b13ad4206490477d7ab8d25 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Mon, 6 Nov 2023 20:11:47 -0800 Subject: [PATCH 1/2] Add styles.Merge for combining styles --- styles/utils.go | 12 ++++++++++ styles/utils_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 styles/utils.go create mode 100644 styles/utils_test.go diff --git a/styles/utils.go b/styles/utils.go new file mode 100644 index 0000000..21cc81d --- /dev/null +++ b/styles/utils.go @@ -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 +} diff --git a/styles/utils_test.go b/styles/utils_test.go new file mode 100644 index 0000000..fcb43a0 --- /dev/null +++ b/styles/utils_test.go @@ -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) +} From 311c8dd026ba153a90ce109a095f647c07ef499c Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Mon, 6 Nov 2023 20:26:46 -0800 Subject: [PATCH 2/2] Switch to constants --- styles/utils_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/styles/utils_test.go b/styles/utils_test.go index fcb43a0..fda399f 100644 --- a/styles/utils_test.go +++ b/styles/utils_test.go @@ -46,10 +46,10 @@ func TestMergeThreeStyles(t *testing.T) { mergedStyle := Merge(baseStyle, secondaryStyle, tertiaryStyle) expectedStyle := Props{ - "padding": "10px", - "margin": "10px", // From secondaryStyle - "color": "blue", // From tertiaryStyle - "border": "1px solid black", + Padding: "10px", + Margin: "10px", // From secondaryStyle + Color: "blue", // From tertiaryStyle + Border: "1px solid black", } assert.Equal(t, expectedStyle, mergedStyle)