basiccheck

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2024 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basiccheck"
)

func main() {
	input1 := []string{"foo", "bar"}
	input2 := []string{"bar", "foo"}
	if basiccheck.SimilarSlice(input1, input2) {
		fmt.Printf("%v =~ %v", input1, input2)

	} else {
		fmt.Printf("%v != %v", input1, input2)
	}
}
Output:

[foo bar] =~ [bar foo]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllInSliceWith added in v0.5.0

func AllInSliceWith[T any](list []T, valid func(T) bool) bool

AllInSliceWith check if all elements in a slice return true with the function 'valid' passed in arguments.

If 'valid' is nil, return true.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/jeremmfr/go-utils/basiccheck"
)

func main() {
	input := []string{"boo", "bar", "baz"}
	if basiccheck.AllInSliceWith(input, func(s string) bool {
		return strings.HasPrefix(s, "b")
	}) {
		fmt.Printf("all of %v has prefix 'b'", input)

	} else {
		fmt.Printf("one of %v has not prefix 'b'", input)
	}
}
Output:

all of [boo bar baz] has prefix 'b'

func EqualSlice deprecated added in v0.5.0

func EqualSlice[T comparable](a, b []T) bool

EqualSlice check if two slice is Equal: same length, same element in same order.

Deprecated: use slices.Equal() from the standard 'slices' library instead.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basiccheck"
)

func main() {
	a := []string{"foo", "bar", "baz"}
	b := []string{"foo", "bar", "baz"}
	if basiccheck.EqualSlice(a, b) {
		fmt.Print("a == b")

	} else {
		fmt.Print("a != b")
	}
}
Output:

a == b

func InSlice deprecated added in v0.5.0

func InSlice[T comparable](elem T, list []T) bool

InSlice check if an element is present in a slice.

Deprecated: use slices.Contains() from the standard 'slices' library instead.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basiccheck"
)

func main() {
	input := []string{"foo", "bar"}
	if basiccheck.InSlice("bar", input) {
		fmt.Printf("bar found in %v", input)

	} else {
		fmt.Printf("bar not found in %v", input)
	}
}
Output:

bar found in [foo bar]

func OneInSliceWith deprecated added in v0.5.0

func OneInSliceWith[T any](list []T, find func(T) bool) bool

OneInSliceWith check if at least one element in a slice returns true with the function 'find' passed in arguments.

If 'find' is nil, return false.

Deprecated: use slices.ContainsFunc() from the standard 'slices' library instead.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/jeremmfr/go-utils/basiccheck"
)

func main() {
	input := []string{"foo", "bar", "baz"}
	if basiccheck.OneInSliceWith(input, func(s string) bool {
		return strings.HasPrefix(s, "b")
	}) {
		fmt.Printf("one of %v has prefix 'b'", input)

	} else {
		fmt.Printf("no one of %v has prefix 'b'", input)
	}
}
Output:

one of [foo bar baz] has prefix 'b'

func SimilarSlice added in v0.11.0

func SimilarSlice[T comparable](a, b []T) bool

SimilarSlice check if two slice is Similar: same length, same element (not necessarily in same order).

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basiccheck"
)

func main() {
	a := []string{"foo", "bar", "baz"}
	b := []string{"baz", "foo", "bar"}
	if basiccheck.SimilarSlice(a, b) {
		fmt.Print("a == b")

	} else {
		fmt.Print("a != b")
	}
}
Output:

a == b

func StringHasOneOfPrefixes added in v0.2.0

func StringHasOneOfPrefixes(s string, list []string) bool

StringHasOneOfPrefixes check if string has one of prefix list.

func StringHasOneOfSuffixes added in v0.10.0

func StringHasOneOfSuffixes(s string, list []string) bool

StringHasOneOfSuffixes check if string has one of suffix list.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL