Skip to content

Commit 4c4e86b

Browse files
committed
Added deduplicate
1 parent 7ee81d0 commit 4c4e86b

File tree

12 files changed

+341
-10
lines changed

12 files changed

+341
-10
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ Not supported by: InterfaceSlicer, BoolSlicer
185185

186186
```
187187
a := slicer.Int([]int{5,3,4,1,2})
188-
a.Sour()
188+
a.Sort()
189189
// a is []int{1,2,3,4,5}
190190
```
191+
192+
### Deduplicate
193+
194+
Deduplicate removes all duplicates within a slice.
195+
196+
```
197+
a := slicer.Int([]int{5,3,5,1,3})
198+
a.Deduplicate()
199+
// a is []int{5,3,1}
200+
```

bool.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,18 @@ func (s *BoolSlicer) Join(separator string) string {
9999
builder.WriteString(fmt.Sprintf("%v", s.slice[index]))
100100
result := builder.String()
101101
return result
102-
}
102+
}
103+
104+
// Deduplicate removes duplicate values from the slice
105+
func (s *BoolSlicer) Deduplicate() {
106+
107+
result := Bool()
108+
109+
for _, elem := range s.slice {
110+
if !result.Contains(elem) {
111+
result.Add(elem)
112+
}
113+
}
114+
115+
s.slice = result.AsSlice()
116+
}

deduplicate_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package slicer
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestStringDeduplicate(t *testing.T) {
10+
11+
s := String()
12+
s.Add("one", "four", "two", "three", "two", "one")
13+
14+
s.Deduplicate()
15+
expected := "one four two three"
16+
actual := strings.Join(s.AsSlice(), " ")
17+
if expected != actual {
18+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
19+
}
20+
}
21+
22+
func TestInt64Deduplicate(t *testing.T) {
23+
24+
s := Int64()
25+
s.Add(1, 2, 3, 2, 3, 4, 3, 4, 5)
26+
27+
s.Deduplicate()
28+
expected := "[1,2,3,4,5]"
29+
actual, _ := json.Marshal(s.AsSlice())
30+
if expected != string(actual) {
31+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
32+
}
33+
}
34+
35+
func TestInt32Deduplicate(t *testing.T) {
36+
37+
s := Int32()
38+
s.Add(1, 2, 3, 2, 3, 4, 3, 4, 5)
39+
40+
s.Deduplicate()
41+
expected := "[1,2,3,4,5]"
42+
actual, _ := json.Marshal(s.AsSlice())
43+
if expected != string(actual) {
44+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
45+
}
46+
}
47+
48+
func TestInt16Deduplicate(t *testing.T) {
49+
50+
s := Int16()
51+
s.Add(1, 2, 3, 2, 3, 4, 3, 4, 5)
52+
53+
s.Deduplicate()
54+
expected := "[1,2,3,4,5]"
55+
actual, _ := json.Marshal(s.AsSlice())
56+
if expected != string(actual) {
57+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
58+
}
59+
}
60+
61+
func TestInt8Deduplicate(t *testing.T) {
62+
63+
s := Int8()
64+
s.Add(1, 2, 3, 2, 3, 4, 3, 4, 5)
65+
66+
s.Deduplicate()
67+
expected := "[1,2,3,4,5]"
68+
actual, _ := json.Marshal(s.AsSlice())
69+
if expected != string(actual) {
70+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
71+
}
72+
}
73+
74+
func TestIntDeduplicate(t *testing.T) {
75+
76+
s := Int()
77+
s.Add(1, 2, 3, 2, 3, 4, 3, 4, 5)
78+
79+
s.Deduplicate()
80+
expected := "[1,2,3,4,5]"
81+
actual, _ := json.Marshal(s.AsSlice())
82+
if expected != string(actual) {
83+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
84+
}
85+
}
86+
87+
func TestInterfaceDeduplicate(t *testing.T) {
88+
89+
s := Interface()
90+
var a interface{} = 1
91+
var b interface{} = "hello"
92+
s.Add(a, b, a, b)
93+
94+
s.Deduplicate()
95+
expected := `[1,"hello"]`
96+
actual, _ := json.Marshal(s.AsSlice())
97+
if expected != string(actual) {
98+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
99+
}
100+
}
101+
102+
func TestFloat32Deduplicate(t *testing.T) {
103+
104+
s := Float32()
105+
s.Add(1.5, 2.3, 3.9, 2.3, 3.9, 4.4, 3.9, 4.4, 5.2)
106+
107+
s.Deduplicate()
108+
expected := "[1.5,2.3,3.9,4.4,5.2]"
109+
actual, _ := json.Marshal(s.AsSlice())
110+
if expected != string(actual) {
111+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
112+
}
113+
}
114+
115+
func TestFloat64Deduplicate(t *testing.T) {
116+
117+
s := Float64()
118+
s.Add(1.5, 2.3, 3.9, 2.3, 3.9, 4.4, 3.9, 4.4, 5.2)
119+
120+
s.Deduplicate()
121+
expected := "[1.5,2.3,3.9,4.4,5.2]"
122+
actual, _ := json.Marshal(s.AsSlice())
123+
if expected != string(actual) {
124+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
125+
}
126+
}
127+
128+
func TestBoolDeduplicate(t *testing.T) {
129+
130+
s := Bool()
131+
s.Add(true, true, true, false, true, false, false, false)
132+
133+
s.Deduplicate()
134+
expected := "[true,false]"
135+
actual, _ := json.Marshal(s.AsSlice())
136+
if expected != string(actual) {
137+
t.Errorf("Expected '%s', but got '%s'", expected, actual)
138+
}
139+
}
140+
141+
// func TestStringAddSlice(t *testing.T) {
142+
143+
// s := String()
144+
// s.Add("one")
145+
// s.Add("two")
146+
// extras := []string{"three", "four"}
147+
// s.AddSlice(extras)
148+
149+
// expected := "one two three four"
150+
// actual := strings.Join(s.AsSlice(), " ")
151+
// if expected != actual {
152+
// t.Errorf("Expected '%s', but got '%s'", expected, actual)
153+
// }
154+
// }
155+
156+
// func TestStringAddSlicer(t *testing.T) {
157+
158+
// s := String()
159+
// s.Add("one")
160+
// s.Add("two")
161+
162+
// p := String()
163+
// p.Add("three")
164+
// p.Add("four")
165+
166+
// s.AddSlicer(p)
167+
168+
// expected := "one two three four"
169+
// actual := strings.Join(s.AsSlice(), " ")
170+
// if expected != actual {
171+
// t.Errorf("Expected '%s', but got '%s'", expected, actual)
172+
// }
173+
// }

float32.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *Float32Slicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *Float32Slicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *Float32Slicer) Deduplicate() {
112+
113+
result := Float32()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

float64.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *Float64Slicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *Float64Slicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *Float64Slicer) Deduplicate() {
112+
113+
result := Float64()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

int.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *IntSlicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *IntSlicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *IntSlicer) Deduplicate() {
112+
113+
result := Int()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

int16.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *Int16Slicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *Int16Slicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *Int16Slicer) Deduplicate() {
112+
113+
result := Int16()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

int32.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *Int32Slicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *Int32Slicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *Int32Slicer) Deduplicate() {
112+
113+
result := Int32()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

int64.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *Int64Slicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *Int64Slicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *Int64Slicer) Deduplicate() {
112+
113+
result := Int64()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

int8.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ func (s *Int8Slicer) Join(separator string) string {
101101
result := builder.String()
102102
return result
103103
}
104+
104105
// Sort the slice values
105106
func (s *Int8Slicer) Sort() {
106107
sort.Slice(s.slice, func(i, j int) bool { return s.slice[i] < s.slice[j] })
107-
}
108+
}
109+
110+
// Deduplicate removes duplicate values from the slice
111+
func (s *Int8Slicer) Deduplicate() {
112+
113+
result := Int8()
114+
115+
for _, elem := range s.slice {
116+
if !result.Contains(elem) {
117+
result.Add(elem)
118+
}
119+
}
120+
121+
s.slice = result.AsSlice()
122+
}

0 commit comments

Comments
 (0)