Skip to content

Commit

Permalink
feature: add delete by value in slice
Browse files Browse the repository at this point in the history
  • Loading branch information
bimaahida authored Nov 4, 2022
1 parent d9bb2be commit ed0d47a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
21 changes: 14 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# go-utils

<a name="v1.28.0"></a>
## [v1.28.0] - 2022-11-04
### New Features
- add DeleteByValue


<a name="v1.27.0"></a>
## [v1.27.0] - 2022-09-22
### New Features
Expand Down Expand Up @@ -58,11 +64,11 @@
- fix marshal issue on gorm.DeletedAt empty value ([#32](https://github.com/kumparan/kumnats/issues/32))


<a name="v.1.20.0"></a>
## [v.1.20.0] - 2022-03-11

<a name="v1.20.0"></a>
## [v1.20.0] - 2022-03-11

<a name="v.1.20.0"></a>
## [v.1.20.0] - 2022-03-11
### New Features
- add constraint size gql directive ([#30](https://github.com/kumparan/kumnats/issues/30))

Expand Down Expand Up @@ -229,7 +235,8 @@
- init go-utils


[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.27.0...HEAD
[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.28.0...HEAD
[v1.28.0]: https://github.com/kumparan/kumnats/compare/v1.27.0...v1.28.0
[v1.27.0]: https://github.com/kumparan/kumnats/compare/v1.26.0...v1.27.0
[v1.26.0]: https://github.com/kumparan/kumnats/compare/v1.25.1...v1.26.0
[v1.25.1]: https://github.com/kumparan/kumnats/compare/v1.25.0...v1.25.1
Expand All @@ -238,9 +245,9 @@
[v1.23.0]: https://github.com/kumparan/kumnats/compare/v1.22.0...v1.23.0
[v1.22.0]: https://github.com/kumparan/kumnats/compare/v1.21.0...v1.22.0
[v1.21.0]: https://github.com/kumparan/kumnats/compare/v1.20.1...v1.21.0
[v1.20.1]: https://github.com/kumparan/kumnats/compare/v.1.20.0...v1.20.1
[v.1.20.0]: https://github.com/kumparan/kumnats/compare/v1.20.0...v.1.20.0
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v1.20.0
[v1.20.1]: https://github.com/kumparan/kumnats/compare/v1.20.0...v1.20.1
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v.1.20.0...v1.20.0
[v.1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v.1.20.0
[v1.19.3]: https://github.com/kumparan/kumnats/compare/v1.19.2...v1.19.3
[v1.19.2]: https://github.com/kumparan/kumnats/compare/v1.19.1...v1.19.2
[v1.19.1]: https://github.com/kumparan/kumnats/compare/v1.19.0...v1.19.1
Expand Down
12 changes: 12 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ func ValueOrDefault[T comparable](value, defaultValue T) T {

return value
}

// DeleteByValue use for delete value in slice
func DeleteByValue[T comparable](a []T, x T) []T {
var newValue []T
for _, n := range a {
if x != n {
newValue = append(newValue, n)
}
}

return newValue
}
39 changes: 39 additions & 0 deletions generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,42 @@ func Test_ValueOrDefault(t *testing.T) {
assert.EqualValues(t, 10.1, ValueOrDefault[float64](0.0, 10.1))
assert.EqualValues(t, 11.2, ValueOrDefault[float64](11.2, 10.1))
}

func Test_DeleteByValue(t *testing.T) {
int64s := []int64{1, 2, 3, 4, 5}
int64ExpectTrueDelete := []int64{2, 3, 4, 5}

int64sUnique := []int64{1, 2, 3, 4, 5, 1}
int64sUniqueExpectTrueDelete := []int64{2, 3, 4, 5}

int32s := []int32{1, 2, 3, 4, 5}
int32ExpectTrueDelete := []int32{1, 2, 4, 5}

strings := []string{"a", "be", "see", "deep"}
stringExpectTrueDelete := []string{"a", "be", "deep"}

float64s := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
float64ExpectTrueDelete := []float64{1.1, 3.3, 4.4, 5.5}

float32s := []float32{1.2, 2.3, 3.4, 4.5, 5.6}
float32ExpectTrueDelete := []float32{1.2, 2.3, 3.4, 4.5}

assert.EqualValues(t, int64ExpectTrueDelete, DeleteByValue[int64](int64s, 1))
assert.EqualValues(t, int64s, DeleteByValue[int64](int64s, 10))

assert.EqualValues(t, int64sUniqueExpectTrueDelete, DeleteByValue[int64](int64sUnique, 1))
assert.EqualValues(t, int64sUnique, DeleteByValue[int64](int64sUnique, 10))

assert.EqualValues(t, int32ExpectTrueDelete, DeleteByValue[int32](int32s, 3))
assert.EqualValues(t, int32s, DeleteByValue[int32](int32s, 10))

assert.EqualValues(t, stringExpectTrueDelete, DeleteByValue[string](strings, "see"))
assert.EqualValues(t, strings, DeleteByValue[string](strings, "lol"))

assert.EqualValues(t, float64ExpectTrueDelete, DeleteByValue[float64](float64s, 2.2))
assert.EqualValues(t, float64s, DeleteByValue[float64](float64s, 5.7))

assert.EqualValues(t, float32ExpectTrueDelete, DeleteByValue[float32](float32s, 5.6))
assert.EqualValues(t, float32s, DeleteByValue[float32](float32s, 5.7))

}

0 comments on commit ed0d47a

Please sign in to comment.