Compare commits
4 Commits
6bfa97e6aa
...
v0.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
| e5b35f3fcc | |||
| e6a94c487e | |||
| 0f903cc8ec | |||
| 19fcdb4a7d |
@@ -1,31 +1,31 @@
|
|||||||
package ucontainer
|
package ucontainer
|
||||||
|
|
||||||
// Optional is an optional value.
|
// Optional can either hold a value, or nothing.
|
||||||
type Optional[T any] struct {
|
type Optional[T any] struct {
|
||||||
value T
|
value T
|
||||||
exists bool
|
exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the value and true if the value exists. If not, it returns the
|
// O creates a new optional with the specified value.
|
||||||
// last set value and false.
|
func O[T any] (value T) Optional[T] {
|
||||||
func (this *Optional[T]) Value () (T, bool) {
|
return Optional[T] {
|
||||||
return this.value, this.exists
|
value: value,
|
||||||
|
exists: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value.
|
// Void returns an optional with no value.
|
||||||
func (this *Optional[T]) Set (value T) {
|
func Void[T any] () Optional[T] {
|
||||||
this.value = value
|
return Optional[T] { }
|
||||||
this.exists = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unset unsets the value.
|
|
||||||
func (this *Optional[T]) Unset () {
|
|
||||||
var zero T
|
|
||||||
this.value = zero
|
|
||||||
this.exists = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exists returns if the value is currently set.
|
// Exists returns if the value is currently set.
|
||||||
func (this *Optional[T]) Exists () bool {
|
func (optional Optional[T]) Exists () bool {
|
||||||
return this.exists
|
return optional.exists
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the value and true if the value exists. If not, it returns the
|
||||||
|
// zero value and false.
|
||||||
|
func (optional Optional[T]) Value () (T, bool) {
|
||||||
|
return optional.value, optional.exists
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,15 @@ package ucontainer
|
|||||||
// Set is a set of unique items, built on top of map.
|
// Set is a set of unique items, built on top of map.
|
||||||
type Set[T comparable] map[T] struct { }
|
type Set[T comparable] map[T] struct { }
|
||||||
|
|
||||||
|
// NewSet creates a new set that contains all specified items.
|
||||||
|
func NewSet[T comparable] (items ...T) Set[T] {
|
||||||
|
set := make(Set[T])
|
||||||
|
for _, item := range items {
|
||||||
|
set.Add(item)
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
// Empty returns true if there are no items in the set.
|
// Empty returns true if there are no items in the set.
|
||||||
func (set Set[T]) Empty () bool {
|
func (set Set[T]) Empty () bool {
|
||||||
return set == nil || len(set) == 0
|
return set == nil || len(set) == 0
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module git.tebibyte.media/sashakoshka/goutil
|
module git.tebibyte.media/sashakoshka/goutil
|
||||||
|
|
||||||
go 1.20.0
|
go 1.22.0
|
||||||
|
|||||||
@@ -1,6 +1,23 @@
|
|||||||
package ucolor
|
package ucolor
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
import "image/color"
|
||||||
|
|
||||||
|
func TestTransparent (test *testing.T) {
|
||||||
|
if Transparent(color.NRGBA { A: 255 }) {
|
||||||
|
test.Fatal("false positive")
|
||||||
|
}
|
||||||
|
if !Transparent(color.NRGBA { A: 0 }) {
|
||||||
|
test.Fatal("false negative")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToRGBA (test *testing.T) {
|
||||||
|
rgba := ToRGBA(color.NRGBA { R: 123, G: 100, B: 23, A: 230 })
|
||||||
|
if rgba != (color.RGBA { R: 111, G: 90, B: 20, A: 230 }) {
|
||||||
|
test.Fatalf("wrong value: %v", rgba)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPremultiply (test *testing.T) {
|
func TestPremultiply (test *testing.T) {
|
||||||
r, g, b, a := Premultiply(0xFFFF, 0xFFFF, 0xFFFF, 0x8888)
|
r, g, b, a := Premultiply(0xFFFF, 0xFFFF, 0xFFFF, 0x8888)
|
||||||
|
|||||||
Reference in New Issue
Block a user