Skip to content

Commit b82924c

Browse files
authored
updated add and remove tags (#75)
1 parent 6a22dde commit b82924c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

board.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,20 @@ func NewBoard(title string) *Board {
172172
Timezone: "browser",
173173
Editable: true,
174174
HideControls: false,
175-
Rows: []*Row{}}
175+
Rows: []*Row{},
176+
}
176177
}
177178

178179
func (b *Board) RemoveTags(tags ...string) {
179-
tagFound := make(map[string]int, len(b.Tags))
180-
for i, tag := range b.Tags {
181-
tagFound[tag] = i
182-
}
183-
for _, removeTag := range tags {
184-
if i, ok := tagFound[removeTag]; ok {
185-
b.Tags = append(b.Tags[:i], b.Tags[i+1:]...)
180+
// order might change after removing the tags
181+
for _, toRemoveTag := range tags {
182+
tagLen := len(b.Tags)
183+
for i, tag := range b.Tags {
184+
if tag == toRemoveTag {
185+
b.Tags[tagLen-1], b.Tags[i] = b.Tags[i], b.Tags[tagLen-1]
186+
b.Tags = b.Tags[:tagLen-1]
187+
break
188+
}
186189
}
187190
}
188191
}
@@ -197,6 +200,7 @@ func (b *Board) AddTags(tags ...string) {
197200
continue
198201
}
199202
b.Tags = append(b.Tags, tag)
203+
tagFound[tag] = true
200204
}
201205
}
202206

board_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ func TestAddTags(t *testing.T) {
3737

3838
func TestBoardRemoveTags_Existent(t *testing.T) {
3939
b := sdk.NewBoard("Sample")
40-
b.AddTags("1", "2", "3", "4")
4140

42-
b.RemoveTags("1", "2")
41+
b.AddTags("1", "2", "3", "4", "4")
42+
b.RemoveTags("1", "2", "5")
43+
b.AddTags("1", "4")
4344

44-
if len(b.Tags) != 2 {
45-
t.Errorf("len(tags) should be 2 but got %d", len(b.Tags))
45+
if len(b.Tags) != 3 {
46+
t.Errorf("len(tags) should be 2 but got %d %v ", len(b.Tags), b.Tags)
47+
}
48+
for _, tag := range b.Tags {
49+
if tag == "2" || tag == "5" {
50+
t.Errorf("2 & 5 tag should not be present but got in tags %v", b.Tags)
51+
}
4652
}
4753
}
4854

0 commit comments

Comments
 (0)