Compare commits

...

4 Commits

Author SHA1 Message Date
semantic-release-bot
dd110b65b6 release(rc): v1.2.0-rc.2
## [1.2.0-rc.2](https://git.ext.icikowski.pl/go/generics/compare/v1.2.0-rc.1...v1.2.0-rc.2) (2024-05-27)

### Features

* **pairs:** add `Pairs`-related methods ([057f12e](057f12e602))

### Refactoring

* **comments:** beautify comments ([73328b0](73328b0ef7))
2024-05-27 22:26:30 +00:00
97bf50af45
docs(package): add simple README.md and package description 2024-05-28 00:21:14 +02:00
73328b0ef7
refactor(comments): beautify comments 2024-05-28 00:20:32 +02:00
057f12e602
feat(pairs): add Pairs-related methods 2024-05-28 00:19:20 +02:00
9 changed files with 149 additions and 20 deletions

View File

@ -1,3 +1,15 @@
## [1.2.0-rc.2](https://git.ext.icikowski.pl/go/generics/compare/v1.2.0-rc.1...v1.2.0-rc.2) (2024-05-27)
### Features
* **pairs:** add `Pairs`-related methods ([057f12e](https://git.ext.icikowski.pl/go/generics/commit/057f12e602cda3282bd3aa53590a4bd120789f3e))
### Refactoring
* **comments:** beautify comments ([73328b0](https://git.ext.icikowski.pl/go/generics/commit/73328b0ef77efe3bc873b643e1464a84faf64e54))
## [1.2.0-rc.1](https://git.ext.icikowski.pl/go/generics/compare/v1.1.1-rc.9...v1.2.0-rc.1) (2024-05-21)

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# `generics`
[![Go Report Card](https://goreportcard.com/badge/pkg.icikowski.pl/generics)](https://goreportcard.com/report/pkg.icikowski.pl/generics)
Collection of helpers and functions using generics.

17
maps.go
View File

@ -1,15 +1,6 @@
package generics
// Pair represents key-value pair
type Pair[K comparable, V any] struct {
Key K
Value V
}
// Pairs represents collection of key-value pairs
type Pairs[K comparable, V any] []Pair[K, V]
// MapPairs returns list of map's key-value pairs
// MapPairs returns list of map's key-value pairs.
func MapPairs[K comparable, V any](src map[K]V) Pairs[K, V] {
dst := make([]Pair[K, V], len(src))
idx := 0
@ -23,7 +14,7 @@ func MapPairs[K comparable, V any](src map[K]V) Pairs[K, V] {
return dst
}
// MapKeys returns list of map's keys
// MapKeys returns list of map's keys.
func MapKeys[K comparable, V any](src map[K]V) []K {
dst := make([]K, len(src))
idx := 0
@ -34,7 +25,7 @@ func MapKeys[K comparable, V any](src map[K]V) []K {
return dst
}
// MapValues returns list of map's values
// MapValues returns list of map's values.
func MapValues[K comparable, V any](src map[K]V) []V {
dst := make([]V, len(src))
idx := 0
@ -45,7 +36,7 @@ func MapValues[K comparable, V any](src map[K]V) []V {
return dst
}
// InvertMap returns map with keys and values swapped
// InvertMap returns map with keys and values swapped.
func InvertMap[K, V comparable](src map[K]V) map[V]K {
if src == nil {
return nil

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "generics",
"version": "1.2.0-rc.1",
"version": "1.2.0-rc.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "generics",
"version": "1.2.0-rc.1",
"version": "1.2.0-rc.2",
"devDependencies": {
"@saithodev/semantic-release-gitea": "^2.1.0",
"@semantic-release/changelog": "^6.0.3",

View File

@ -1,4 +1,4 @@
/*
Generics-related functions
Collection of helpers and functions using generics.
*/
package generics

View File

@ -1,7 +1,7 @@
{
"private": true,
"name": "generics",
"version": "1.2.0-rc.1",
"version": "1.2.0-rc.2",
"scripts": {
"release": "./node_modules/.bin/semantic-release"
},

28
pairs.go Normal file
View File

@ -0,0 +1,28 @@
package generics
// Pair represents key-value pair.
type Pair[K comparable, V any] struct {
Key K
Value V
}
// Pairs represents a collection of key-value pairs.
type Pairs[K comparable, V any] []Pair[K, V]
// Keys returns list of key-value pairs' keys.
func (p Pairs[K, V]) Keys() []K {
dst := make([]K, len(p))
for i, pair := range p {
dst[i] = pair.Key
}
return dst
}
// Values returns list of key-value pairs' values.
func (p Pairs[K, V]) Values() []V {
dst := make([]V, len(p))
for i, pair := range p {
dst[i] = pair.Value
}
return dst
}

93
pairs_test.go Normal file
View File

@ -0,0 +1,93 @@
package generics
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPairsKeys(t *testing.T) {
tests := map[string]struct {
src Pairs[string, int]
dst []string
}{
"nil pairs": {
src: nil,
dst: []string{},
},
"empty pairs": {
src: Pairs[string, int]{},
dst: []string{},
},
"filled pairs": {
src: Pairs[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
{
Key: "baz",
Value: 3,
},
},
dst: []string{
"foo",
"bar",
"baz",
},
},
}
for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
dst := tc.src.Keys()
require.ElementsMatch(t, tc.dst, dst)
})
}
}
func TestPairsValues(t *testing.T) {
tests := map[string]struct {
src Pairs[string, int]
dst []int
}{
"nil pairs": {
src: nil,
dst: []int{},
},
"empty pairs": {
src: Pairs[string, int]{},
dst: []int{},
},
"filled pairs": {
src: Pairs[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
{
Key: "baz",
Value: 3,
},
},
dst: []int{1, 2, 3},
},
}
for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
dst := tc.src.Values()
require.ElementsMatch(t, tc.dst, dst)
})
}
}

6
ptr.go
View File

@ -1,11 +1,11 @@
package generics
// Ptr returns pointer for given value
// Ptr returns pointer for given value.
func Ptr[T any](val T) *T {
return &val
}
// Val returns value of given pointer or default value if pointer is nil
// Val returns value of given pointer or default value if pointer is nil.
func Val[T any](ptr *T) T {
var val T
if ptr != nil {
@ -14,7 +14,7 @@ func Val[T any](ptr *T) T {
return val
}
// Fallback returns value of given pointer or fallback value if pointer is nil
// Fallback returns value of given pointer or fallback value if pointer is nil.
func Fallback[T any](ptr *T, fallback T) T {
if ptr != nil {
return *ptr