compare

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: Apache-2.0 Imports: 3 Imported by: 5

README

Compare

GoDoc Build Status Codecov Go Report Card Version

Easy Comparisions in Go

This library provides a number of helper functions that compare values of different or unknown data types. It works around some of Go's more annoying issues with generics, and will probably be simplified or eliminated once the final generic proposals land in a stable release.

Interface(value1, value2) int

Pass any two values into this function and it will attempt to convert them into comparable data types.

If value1 < value2, it returns -1 If value1 == value2, it returns 0 If value1 > value2, it returns 1

IntXX Functions

This is a series of functions Int() Int8() Int16() Int32() Int64() that compare similar values of their corresponding types.

FloatXX Functions

This is a series of functions Float32() Float64() that compare similar values of their corresponding types.

String Functions

There are several string comparison functions as well.

String() compares two string values with the same signatures as above

BeginsWith() returns true if a string begins with a certain value Contains() returns true if a string contains a certain value EndsWith() returns true if a string ends with a certain value

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 📚

Documentation

Index

Constants

View Source
const OperatorBeginsWith = "BEGINS"

OperatorBeginsWith represents a "begins with" comparison , when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorContainedBy = "CONTAINED BY"

OperatorContainedBy represents a "contains" comparison in reverse, when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorContains = "CONTAINS"

OperatorContains represents a "contains" comparison, when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorEndsWith = "ENDS"

OperatorEndsWith represents a "ends with" comparison, when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorEqual = "="

OperatorEqual represents an "equals" comparison, when used in Predicates and Criteria

View Source
const OperatorGreaterOrEqual = ">="

OperatorGreaterOrEqual represents an "greater or equal" comparison, when used in Predicates and Criteria

View Source
const OperatorGreaterThan = ">"

OperatorGreaterThan represents an "greater than" comparison, when used in Predicates and Criteria

View Source
const OperatorLessOrEqual = "<="

OperatorLessOrEqual represents an "less or equal" comparison, when used in Predicates and Criteria

View Source
const OperatorLessThan = "<"

OperatorLessThan represents a "less than" comparison, when used in Predicates and Criteria

View Source
const OperatorNotEqual = "!="

OperatorNotEqual represents a "not equals" comparison, when used in Predicates and Criteria

Variables

This section is empty.

Functions

func BeginsWith

func BeginsWith(value1 any, value2 any) bool

BeginsWith is a simple "generic-safe" function for string comparison. It returns TRUE if value1 begins with value2

func Contains

func Contains(value1 any, value2 any) bool

Contains is a simple "generic-safe" function for string comparison. It returns TRUE if value1 contains value2

func EndsWith

func EndsWith(value1 any, value2 any) bool

EndsWith is a simple "generic-safe" function for string comparison. It returns TRUE if value1 ends with value2

func Equal

func Equal(value1 any, value2 any) bool

Equal is a simplified version of Compare. It ONLY returns true if the two provided values are EQUAL. In all other cases (including errors) it returns FALSE

func Float32

func Float32(value1 float32, value2 float32) int

Float32 compares two float32 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func Float64

func Float64(value1 float64, value2 float64) int

Float64 compares two float64 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func GreaterThan

func GreaterThan(value1 any, value2 any) bool

GreaterThan is a simplified version of Compare. It ONLY returns true if value1 is verifiably GREATER THAN value2. In all other cases (including errors) it returns FALSE

func Int

func Int(value1 int, value2 int) int

Int compares two int values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func Int16

func Int16(value1 int16, value2 int16) int

Int16 compares two int16 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func Int32

func Int32(value1 int32, value2 int32) int

Int32 compares two int32 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func Int64

func Int64(value1 int64, value2 int64) int

Int64 compares two int64 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func Int8

func Int8(value1 int8, value2 int8) int

Int8 compares two int8 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func Interface

func Interface(value1 any, value2 any) (int, error)

Interface tries its best to muscle value2 and value2 into compatable types so that they can be compared. If value1 is LESS THAN value2, it returns -1, nil If value1 is EQUAL TO value2, it returns 0, nil If value1 is GREATER THAN value2, it returns 1, nil If the two values are not compatable, then it returns 0, [DERP] with an explanation of the error. Currently, this function ONLLY compares identical numeric or string types. In the future, it *may* be expanded to perform simple type converstions between similar types.

func LessThan

func LessThan(value1 any, value2 any) bool

LessThan is a simplified version of Compare. It ONLY returns true if value1 is verifiably LESS THAN value2. In all other cases (including errors) it returns FALSE

func String

func String(value1 string, value2 string) int

String compares two string values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func UInt

func UInt(value1 uint, value2 uint) int

UInt compares two uint values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func UInt16

func UInt16(value1 uint16, value2 uint16) int

UInt16 compares two uint16 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func UInt32

func UInt32(value1 uint32, value2 uint32) int

UInt32 compares two uint32 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func UInt64

func UInt64(value1 uint64, value2 uint64) int

UInt64 compares two uint64 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func UInt8

func UInt8(value1 uint8, value2 uint8) int

UInt8 compares two uint8 values. It returns -1 if value1 is LESS THAN value2. It returns 0 if value1 is EQUAL TO value2. It returns 1 if value1 is GREATER THAN value2.

func WithOperator

func WithOperator(value1 any, operator string, value2 any) (bool, error)

WithOperator uses an operator to compare two values, and returns TRUE or FALSE

Types

type Stringer

type Stringer interface {
	String() string
}

Stringer is an interface for types that can be converted to String

Jump to

Keyboard shortcuts

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