Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add IsUniqueSliceItem #59

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
# go-utils

<a name="v1.37.0"></a>
## [v1.37.0] - 2024-03-06
### New Features
- add IsUniqueSliceItem


<a name="v1.36.0"></a>
## [v1.36.0] - 2024-01-18
### New Features
- add IsURLReachable ([#58](https://github.com/kumparan/kumnats/issues/58))


<a name="v1.35.0"></a>
## [v1.35.0] - 2023-10-09
### New Features
- add DoHTTPRequest generic method ([#57](https://github.com/kumparan/kumnats/issues/57))


<a name="v1.34.0"></a>
## [v1.34.0] - 2023-09-19
## [v1.34.0] - 2023-09-20
### New Features
- add generate uuid functionality and update linter

Expand Down Expand Up @@ -112,9 +130,6 @@
- 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
### New Features
Expand Down Expand Up @@ -214,11 +229,11 @@
- add money formatter for multiple currencies ([#13](https://github.com/kumparan/kumnats/issues/13))


<a name="v1.8.0"></a>
## [v1.8.0] - 2020-12-10

<a name="v1.7.1"></a>
## [v1.7.1] - 2020-12-10

<a name="v1.8.0"></a>
## [v1.8.0] - 2020-12-10
### New Features
- add formatter for indonesian money and date

Expand Down Expand Up @@ -283,7 +298,10 @@
- init go-utils


[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.34.0...HEAD
[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.37.0...HEAD
[v1.37.0]: https://github.com/kumparan/kumnats/compare/v1.36.0...v1.37.0
[v1.36.0]: https://github.com/kumparan/kumnats/compare/v1.35.0...v1.36.0
[v1.35.0]: https://github.com/kumparan/kumnats/compare/v1.34.0...v1.35.0
[v1.34.0]: https://github.com/kumparan/kumnats/compare/v1.33.0...v1.34.0
[v1.33.0]: https://github.com/kumparan/kumnats/compare/v1.32.1...v1.33.0
[v1.32.1]: https://github.com/kumparan/kumnats/compare/v1.32.0...v1.32.1
Expand All @@ -301,8 +319,7 @@
[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.1]: https://github.com/kumparan/kumnats/compare/v1.20.0...v1.20.1
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v1.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
Expand All @@ -319,9 +336,9 @@
[v1.12.0]: https://github.com/kumparan/kumnats/compare/v1.11.0...v1.12.0
[v1.11.0]: https://github.com/kumparan/kumnats/compare/v1.10.0...v1.11.0
[v1.10.0]: https://github.com/kumparan/kumnats/compare/v1.9.0...v1.10.0
[v1.9.0]: https://github.com/kumparan/kumnats/compare/v1.8.0...v1.9.0
[v1.8.0]: https://github.com/kumparan/kumnats/compare/v1.7.1...v1.8.0
[v1.7.1]: https://github.com/kumparan/kumnats/compare/v1.7.0...v1.7.1
[v1.9.0]: https://github.com/kumparan/kumnats/compare/v1.7.1...v1.9.0
[v1.7.1]: https://github.com/kumparan/kumnats/compare/v1.8.0...v1.7.1
[v1.8.0]: https://github.com/kumparan/kumnats/compare/v1.7.0...v1.8.0
[v1.7.0]: https://github.com/kumparan/kumnats/compare/v1.6.0...v1.7.0
[v1.6.0]: https://github.com/kumparan/kumnats/compare/v1.5.0...v1.6.0
[v1.5.0]: https://github.com/kumparan/kumnats/compare/v1.4.0...v1.5.0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kumparan/go-utils

go 1.18
go 1.20

require (
github.com/99designs/gqlgen v0.14.0
Expand Down
13 changes: 13 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,16 @@ func FindDifferencesFromSlices[T comparable](slices ...[]T) []T {

return result
}

// IsUniqueSliceItem :nodoc:
func IsUniqueSliceItem[T comparable](data []T) bool {
mapData := make(map[T]bool, len(data))
for _, d := range data {
if _, ok := mapData[d]; ok {
return false
}
mapData[d] = true
}

return true
}
54 changes: 52 additions & 2 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Test_FindDifferencesFromSlices(t *testing.T) {
result []string
}

var testCases = []tc{
testCases := []tc{
{
slices: [][]string{
{"a", "b", "c"},
Expand Down Expand Up @@ -138,7 +138,57 @@ func Test_FindDifferencesFromSlices(t *testing.T) {
res := FindDifferencesFromSlices(tc.slices...)
for _, it := range tc.result {
assert.Contains(t, res, it)

}
}
}

func Test_IsUniqueSliceItem(t *testing.T) {
type tc[T comparable] struct {
slice []T
result bool
}

testCasesString := []tc[string]{
{
slice: []string{
"haha", "hehe",
},
result: true,
},
{
slice: []string{
"HAHA", "haha",
},
result: true,
},
{
slice: []string{
"haha", "haha",
},
result: false,
},
}

for _, tc := range testCasesString {
assert.Equal(t, tc.result, IsUniqueSliceItem(tc.slice))
}

testCasesInt := []tc[int]{
{
slice: []int{
1, 2,
},
result: true,
},
{
slice: []int{
1, 1,
},
result: false,
},
}

for _, tc := range testCasesInt {
assert.Equal(t, tc.result, IsUniqueSliceItem(tc.slice))
}
}
Loading