utils

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2019 License: MIT Imports: 18 Imported by: 20

README

utils

golang utils I like

Documentation

Index

Constants

View Source
const (
	Day   = time.Hour * 24
	Week  = Day * 7
	Month = Day * 30
	Year  = Month * 12
)

Variables

View Source
var (
	CarriageReturn = []byte{'\r'}
	LineFeed       = []byte{'\n'}
	CRLF           = []byte("\r\n")
	Space          = []byte{' '}
)

Functions

func Abs

func Abs(n int) int

func Abs64

func Abs64(n int64) int64

func BoolPtr

func BoolPtr(v bool) *bool

BoolPtr returns a bool pointer of the given bool value

func BytesToFloat32

func BytesToFloat32(bytes []uint8) float32

BytesToFloat32 takes a slice of 4 bytes and converts it to a float32 value

func BytesToUint64

func BytesToUint64(bytes []uint8) (v uint64)

BytesToUint64 takes a slice of 8 bytes and returns an uint64 value NOTE: bytes must be 8 length here, or else it will panic. it should just return an error value

func CeilInt

func CeilInt(f float64) int

func CheckFlag

func CheckFlag(value, flag uint32) bool

CheckFlag returns true if the given flag is enabled in the value

func ChunkStringSlice

func ChunkStringSlice(in []string, n int) (out [][]string, err error)

arr = ["a", "b", "c", "d", "e"] ChunkStringSlice(arr, 2) ret = [ [ "a", "b"], ["c", "d"], ["e"] ]

func CombineUint32

func CombineUint32(val1, val2 uint32) uint64

CombineUint32 returns the packed uint64 value of two uint32's

func CustomDurationString

func CustomDurationString(diff time.Duration, numParts int, glue string) string

func CustomRelTime

func CustomRelTime(t1, t2 time.Time, numParts int, glue string) string

func DurationString

func DurationString(diff time.Duration) string

func FilterChannelName

func FilterChannelName(username string) string

func FilterUserIDs

func FilterUserIDs(potentialUserIDs []string) (userIDs []string)

func FilterUsername

func FilterUsername(username string) string

func FilterUsernames

func FilterUsernames(potentialUsernames []string) (usernames []string)

func Float32ToString

func Float32ToString(v float32) string

FloatToString converts a float value to a string with sane defaults

func FloorInt

func FloorInt(f float64) int

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomString

func GenerateRandomString(n int) (string, error)

GenerateRandomString returns a securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomStringURLSafe

func GenerateRandomStringURLSafe(n int) (string, error)

GenerateRandomStringURLSafe returns a URL-safe, base64 encoded securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GetEnv

func GetEnv(key, fallback string) string

GetEnv returns an environment key from the system, with an optional value if the environment key isn't set

func GetTriggers

func GetTriggers(message string) []string

GetTriggers returns a list of strings that have been parsed in accordance to the command rules

func GetTriggersKC

func GetTriggersKC(message string) []string

GetTriggersKC returns a list of strings that have been parsed in accordance to the command rules, but keeps the case

func GetTriggersN

func GetTriggersN(message string, n int) []string

GetTriggersN returns a list of strings that have been parsed in accordance to the command rules. Offset by N

func InsertUnique

func InsertUnique(text string, target *[]string)

InsertUnique inserts a string into a target string splice if it doesn't already exist in it

func Int32ToBytes

func Int32ToBytes(value int32) (bytes []byte)

Int32ToBytes converts a int32 value into a slice of its 4 bytes

func IsNotAlphanumeric

func IsNotAlphanumeric(r rune) bool

func IsNotLatinLetter

func IsNotLatinLetter(r rune) bool

func IsValidUserID

func IsValidUserID(userID string) bool

IsValidUserID verifies that the given user id is not empty, and that it only contains valid characters It does not to any length-checks, as there are no clear rules as to how short or long a user ID can be

func IsValidUsername

func IsValidUsername(username string) bool

IsValidUsername verifies that the given user name is not empty, and that it only contains valid characters (0-9 a-z A-Z _) It does not to any length-checks, as there are no unbroken rules as to how short or long a user name can be

func MakeVariations

func MakeVariations(text string, doNormalize bool) (originalVariations, lowercasedVariations []string, err error)

MakeVariations makes normal-case and lowercase variatinos of a string

func MaxInt

func MaxInt(a, b int) int

func MaxInt64

func MaxInt64(a, b int64) int64

func MinInt

func MinInt(a, b int) int

func NewStringPtr

func NewStringPtr(s string) *string

NewStringPtr returns the pointer to the given string

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

func RandIntN

func RandIntN(min, max int) (int, error)

RandIntN generates a number between min and max (inclusive). Only works with positive numbers.

func ReadArg

func ReadArg(reader *bufio.Reader) string

ReadArg reads a string until \n and trims all whitespace

func RelTime

func RelTime(t1, t2 time.Time) string

func RemoveInStringFunc

func RemoveInStringFunc(in string, predicate RemoveFunc) string

func RemoveNewlines

func RemoveNewlines(s string) string

RemoveNewlines replaces all \r and \n with spaces

func Round

func Round(val float64, places int) (newVal float64)

Round returns the rounded value of a float64 up to N places

func SBKey

func SBKey(m map[string]bool) (keys []string)

func SplitUint64

func SplitUint64(val uint64) (p1, p2 uint32)

SplitUint64 spits a uint64 value into two uint32 values

func StringContains

func StringContains(needle string, haystack []string) bool

func Sum

func Sum(s []int) int

Sum returns the sum of the given slice of ints

func TimeSince

func TimeSince(t2 time.Time) string

func Uint32ToBytes

func Uint32ToBytes(value uint32) (bytes []byte)

Uint32ToBytes converts a uint32 value into a slice of its 4 bytes

func Uint64ToBytes

func Uint64ToBytes(value uint64) (bytes []byte)

Uint64ToBytes converts a uint64 value into a slice of its 8 bytes

func UnpingifyUsername

func UnpingifyUsername(username string) string

func WebWrite

func WebWrite(w http.ResponseWriter, data interface{}) error

func WebWriteError

func WebWriteError(w http.ResponseWriter, code int, errorString string) error

Types

type RemoveFunc

type RemoveFunc func(rune) bool

type WebAPIError

type WebAPIError struct {
	ErrorString string `json:"error"`
	ErrorCode   int    `json:"code"`
}

func NewWebAPIError

func NewWebAPIError(code int, errorString string) WebAPIError

Jump to

Keyboard shortcuts

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