diff --git a/components.go b/components.go index 4b5ccbb..905dc78 100644 --- a/components.go +++ b/components.go @@ -89,6 +89,18 @@ func (cb *ComponentBase) AddProperty(property ComponentProperty, value string, p cb.Properties = append(cb.Properties, r) } +// RemoveProperty removes from the component all properties that has +// the name passed in removeProp. +func (cb *ComponentBase) RemoveProperty(removeProp ComponentProperty) { + var keptProperties []IANAProperty + for i := range cb.Properties { + if cb.Properties[i].IANAToken != string(removeProp) { + keptProperties = append(keptProperties, cb.Properties[i]) + } + } + cb.Properties = keptProperties +} + const ( icalTimestampFormatUtc = "20060102T150405Z" icalTimestampFormatLocal = "20060102T150405" diff --git a/components_test.go b/components_test.go index bb38585..ffa7c44 100644 --- a/components_test.go +++ b/components_test.go @@ -132,3 +132,34 @@ func TestSetMailtoPrefix(t *testing.T) { t.Errorf("expected single mailto: prefix for email att2") } } + +func TestRemoveProperty(t *testing.T) { + testCases := []struct { + name string + output string + }{ + { + name: "test RemoveProperty - start", + output: `BEGIN:VTODO +UID:test-removeproperty +X-TEST:42 +END:VTODO +`, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + e := NewTodo("test-removeproperty") + e.AddProperty("X-TEST", "42") + e.AddProperty("X-TESTREMOVE", "FOO") + e.AddProperty("X-TESTREMOVE", "BAR") + e.RemoveProperty("X-TESTREMOVE") + + // adjust to expected linebreaks, since we're not testing the encoding + text := strings.Replace(e.Serialize(), "\r\n", "\n", -1) + + assert.Equal(t, tc.output, text) + }) + } +}