Skip to content

Commit

Permalink
Add method to remove property
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
quite committed Aug 23, 2024
1 parent ea4ce01 commit 0a291f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions components.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 0a291f4

Please sign in to comment.