Compare commits
No commits in common. "8c8394e5466388e6a7332e37834c2600aec48840" and "8bd63075f40e8126c42274985ec8fff33eda1751" have entirely different histories.
8c8394e546
...
8bd63075f4
@ -5,6 +5,7 @@ when:
|
||||
|
||||
steps:
|
||||
test:
|
||||
group: test
|
||||
image: golang:1.22-alpine
|
||||
commands:
|
||||
- go test -v ./...
|
||||
|
74
CHANGELOG.md
74
CHANGELOG.md
@ -1,77 +1,3 @@
|
||||
## [1.2.0](https://git.ext.icikowski.pl/go/generics/compare/v1.1.0...v1.2.0) (2024-05-27)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **pairs:** add `Pairs`-related methods ([057f12e](https://git.ext.icikowski.pl/go/generics/commit/057f12e602cda3282bd3aa53590a4bd120789f3e))
|
||||
* **types:** add `Pairs` type ([658d8a7](https://git.ext.icikowski.pl/go/generics/commit/658d8a7fd6a032c65531b2a708da5c87417a7caf))
|
||||
|
||||
|
||||
### Refactoring
|
||||
|
||||
* **comments:** beautify comments ([73328b0](https://git.ext.icikowski.pl/go/generics/commit/73328b0ef77efe3bc873b643e1464a84faf64e54))
|
||||
|
||||
|
||||
### Continuous Integrations
|
||||
|
||||
* **deps:** pin `conventional-changelog-conventionalcommits` to version 7.x ([e6fc7d8](https://git.ext.icikowski.pl/go/generics/commit/e6fc7d8d6564a8ccaad0b11e307bae400d3ebe61))
|
||||
* **init:** add initial CI configuration ([edf64a3](https://git.ext.icikowski.pl/go/generics/commit/edf64a344f8102d53b647f3f345ca9cbaed75bd2))
|
||||
* **renovate:** add Renovate bot configuration ([f7324b3](https://git.ext.icikowski.pl/go/generics/commit/f7324b38a1b575949e8d8bf847738ea96c5ee1f3))
|
||||
* **test:** remove `group` directive ([57713b3](https://git.ext.icikowski.pl/go/generics/commit/57713b397fd81cf8856bb573211835df8edad1a1))
|
||||
|
||||
|
||||
### Build system and dependencies
|
||||
|
||||
* **deps:** update all non-major dependencies ([8bd6307](https://git.ext.icikowski.pl/go/generics/commit/8bd63075f40e8126c42274985ec8fff33eda1751))
|
||||
* **deps:** update all non-major dependencies ([c728417](https://git.ext.icikowski.pl/go/generics/commit/c72841795782290d8a8252058de7210c93410d1a))
|
||||
* **deps:** update all non-major dependencies ([6288e9d](https://git.ext.icikowski.pl/go/generics/commit/6288e9d155df422e1507714569de4bd519cb49b8))
|
||||
* **deps:** update dependency @semantic-release/commit-analyzer to v12 ([0be7796](https://git.ext.icikowski.pl/go/generics/commit/0be77969d9a2df9643847d1e98059f1be4b283a4))
|
||||
* **deps:** update dependency @semantic-release/npm to v12 ([abb8e20](https://git.ext.icikowski.pl/go/generics/commit/abb8e20ebfc063448616ae35a793a6afab121bba))
|
||||
* **deps:** update dependency @semantic-release/release-notes-generator to v13 ([280006b](https://git.ext.icikowski.pl/go/generics/commit/280006b47ba3c7a89cb4b7ef26fb9f0c0b735326))
|
||||
* **deps:** update dependency conventional-changelog-conventionalcommits to v8 ([c473983](https://git.ext.icikowski.pl/go/generics/commit/c4739832872f532adfdbaea724a756f6abcb86f7))
|
||||
* **deps:** update dependency semantic-release to v23 ([27e0bd1](https://git.ext.icikowski.pl/go/generics/commit/27e0bd1c5bad1d5cf682717e64c23454b25081a9))
|
||||
* **deps:** update dependency semantic-release to v23.0.3 ([38d67b0](https://git.ext.icikowski.pl/go/generics/commit/38d67b0580902f0d6c3ea120ccfcc6dac2ec5b56))
|
||||
* **deps:** update dependency semantic-release to v23.0.4 ([c0db031](https://git.ext.icikowski.pl/go/generics/commit/c0db0317faedfce3bed1b590919f5a275397f277))
|
||||
* **deps:** update dependency semantic-release to v23.0.5 ([f30de19](https://git.ext.icikowski.pl/go/generics/commit/f30de192cda667443974081f0e8661ddf881eec2))
|
||||
* **deps:** update dependency semantic-release to v23.0.8 ([c139591](https://git.ext.icikowski.pl/go/generics/commit/c13959155745dfbe3b488afc26b9fe234e3a405d))
|
||||
|
||||
## [1.2.0-rc.3](https://git.ext.icikowski.pl/go/generics/compare/v1.2.0-rc.2...v1.2.0-rc.3) (2024-05-27)
|
||||
|
||||
|
||||
### Continuous Integrations
|
||||
|
||||
* **test:** remove `group` directive ([57713b3](https://git.ext.icikowski.pl/go/generics/commit/57713b397fd81cf8856bb573211835df8edad1a1))
|
||||
|
||||
## [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)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **types:** add `Pairs` type ([658d8a7](https://git.ext.icikowski.pl/go/generics/commit/658d8a7fd6a032c65531b2a708da5c87417a7caf))
|
||||
|
||||
|
||||
### Continuous Integrations
|
||||
|
||||
* **deps:** pin `conventional-changelog-conventionalcommits` to version 7.x ([e6fc7d8](https://git.ext.icikowski.pl/go/generics/commit/e6fc7d8d6564a8ccaad0b11e307bae400d3ebe61))
|
||||
|
||||
|
||||
### Build system and dependencies
|
||||
|
||||
* **deps:** update all non-major dependencies ([8bd6307](https://git.ext.icikowski.pl/go/generics/commit/8bd63075f40e8126c42274985ec8fff33eda1751))
|
||||
* **deps:** update dependency conventional-changelog-conventionalcommits to v8 ([c473983](https://git.ext.icikowski.pl/go/generics/commit/c4739832872f532adfdbaea724a756f6abcb86f7))
|
||||
|
||||
## [1.1.1-rc.9](https://git.ext.icikowski.pl/go/generics/compare/v1.1.1-rc.8...v1.1.1-rc.9) (2024-04-12)
|
||||
|
||||
|
||||
|
@ -1,5 +0,0 @@
|
||||
# `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.
|
16
maps.go
16
maps.go
@ -1,7 +1,13 @@
|
||||
package generics
|
||||
|
||||
// MapPairs returns list of map's key-value pairs.
|
||||
func MapPairs[K comparable, V any](src map[K]V) Pairs[K, V] {
|
||||
// Pair represents key-value pair
|
||||
type Pair[K comparable, V any] struct {
|
||||
Key K
|
||||
Value V
|
||||
}
|
||||
|
||||
// MapPairs returns list of map's key-value pairs
|
||||
func MapPairs[K comparable, V any](src map[K]V) []Pair[K, V] {
|
||||
dst := make([]Pair[K, V], len(src))
|
||||
idx := 0
|
||||
for k, v := range src {
|
||||
@ -14,7 +20,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
|
||||
@ -25,7 +31,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
|
||||
@ -36,7 +42,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
|
||||
|
@ -13,11 +13,11 @@ func TestMapPairs(t *testing.T) {
|
||||
}{
|
||||
"nil map": {
|
||||
src: nil,
|
||||
dst: Pairs[string, int]{},
|
||||
dst: []Pair[string, int]{},
|
||||
},
|
||||
"empty map": {
|
||||
src: map[string]int{},
|
||||
dst: Pairs[string, int]{},
|
||||
dst: []Pair[string, int]{},
|
||||
},
|
||||
"filled map": {
|
||||
src: map[string]int{
|
||||
@ -25,7 +25,7 @@ func TestMapPairs(t *testing.T) {
|
||||
"bar": 2,
|
||||
"baz": 3,
|
||||
},
|
||||
dst: Pairs[string, int]{
|
||||
dst: []Pair[string, int]{
|
||||
{
|
||||
Key: "foo",
|
||||
Value: 1,
|
||||
|
2179
package-lock.json
generated
2179
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
||||
/*
|
||||
Collection of helpers and functions using generics.
|
||||
Generics-related functions
|
||||
*/
|
||||
package generics
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "generics",
|
||||
"version": "1.2.0",
|
||||
"version": "1.1.1-rc.9",
|
||||
"scripts": {
|
||||
"release": "./node_modules/.bin/semantic-release"
|
||||
},
|
||||
@ -12,7 +12,7 @@
|
||||
"@semantic-release/git": "^10.0.1",
|
||||
"@semantic-release/npm": "^12.0.0",
|
||||
"@semantic-release/release-notes-generator": "^13.0.0",
|
||||
"conventional-changelog-conventionalcommits": "^7.0.2",
|
||||
"conventional-changelog-conventionalcommits": "^8.0.0",
|
||||
"semantic-release": "^23.0.0"
|
||||
}
|
||||
}
|
||||
|
28
pairs.go
28
pairs.go
@ -1,28 +0,0 @@
|
||||
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
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
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
6
ptr.go
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user