lazysupport

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: BSD-3-Clause Imports: 22 Imported by: 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultCache = MemCache{}
View Source
var Month = Year / 12
View Source
var ShouldCache = false

ShouldCache set if the inflector should (or not) cache the inflections

View Source
var Year = day * (365 + 365 + 365 + 366) / 4

Functions

func Cache

func Cache(fn func() ([]byte, error), key ...any) ([]byte, error)

Cache caches the result of fn in the cache.

func Camelize

func Camelize(term string) string

Camelize converts strings to UpperCamelCase.

Example
fmt.Println(Camelize("my_account"))
fmt.Println(Camelize("user-profile"))
fmt.Println(Camelize("ssl_error"))
fmt.Println(Camelize("http_connection_timeout"))
fmt.Println(Camelize("restful_controller"))
fmt.Println(Camelize("multiple_http_calls"))
Output:

MyAccount
UserProfile
SSLError
HTTPConnectionTimeout
RESTfulController
MultipleHTTPCalls

func ClearCache

func ClearCache()

ClearCache clear the inflection cache. Both for singulars and plurals.

func Dasherize

func Dasherize(term string) string

Dasherize converts strings to dashed, lowercase form.

Example
fmt.Println(Dasherize("MyAccount"))
fmt.Println(Dasherize("user_profile"))
Output:

my-account
user-profile

func DomID

func DomID(s any) (string, error)

func ForeignKey

func ForeignKey(term string) string

ForeignKey creates a foreign key name from an ORM model name.

Example
fmt.Println(ForeignKey("Message"))
fmt.Println(ForeignKey("AdminPost"))
Output:

message_id
admin_post_id

func Html2Markdown

func Html2Markdown(input any) string

func Humanize

func Humanize(term string) string

func IDFor

func IDFor(s any) (string, error)

func MeasureText

func MeasureText[T any](name string, fn func() T) T

func Must

func Must[T any](obj T) T

func NameOf

func NameOf(obj any) string

func NameOfWithPackage

func NameOfWithPackage(obj any) string

func Ordinal

func Ordinal(number int64) string

Ordinal returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Example
fmt.Println(Ordinal(1))
fmt.Println(Ordinal(2))
fmt.Println(Ordinal(14))
fmt.Println(Ordinal(1002))
fmt.Println(Ordinal(1003))
fmt.Println(Ordinal(-11))
fmt.Println(Ordinal(-1021))
Output:

st
nd
th
nd
rd
th
st

func Ordinalize

func Ordinalize(number int64) string

Ordinalize turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Example
fmt.Println(Ordinalize(1))
fmt.Println(Ordinalize(2))
fmt.Println(Ordinalize(14))
fmt.Println(Ordinalize(1002))
fmt.Println(Ordinalize(1003))
fmt.Println(Ordinalize(-11))
fmt.Println(Ordinalize(-1021))
Output:

1st
2nd
14th
1002nd
1003rd
-11th
-1021st

func Pluralize

func Pluralize(singular string) string

Pluralize returns the plural form of the word in the string.

Example
fmt.Println(Pluralize("post"))
fmt.Println(Pluralize("octopus"))
fmt.Println(Pluralize("sheep"))
fmt.Println(Pluralize("words"))
fmt.Println(Pluralize("CamelOctopus"))
Output:

posts
octopi
sheep
words
CamelOctopi

func RandomString

func RandomString(n int) string

func Sanitize

func Sanitize(input string) string

Sanitize sanitizes the input string using bluemonday.UGCPolicy

func ShortDuration

func ShortDuration(d time.Duration) string

ShortDuration returns a short string representation of the duration regardless of the unit.

For numbers below to one minute the rules are as follows:

  • If the integer part is bigger than 100, it will hide the fractional part. For example: 120ms
  • If the integer part is bigger than 10, it will show at max 1 decimal. For example: 12.3s
  • If the integer part is bigger than 1, it will show at max 2 decimals. For example: 1.23s
  • If the integer part is 0, it will use the next unit.
  • If duration is 0 it is return as 0s.

For numbers above or equal to one minute it will include at maximum two units. For example:

  • 1y 2d
  • 2m 3d
  • 1h 30m
  • 3m 2s

If the second unit is 0 that part is omitted.

The valid units are

  • days (d)
  • hours (h)
  • minutes (m)
  • seconds (s)
  • milliseconds (ms)
  • microseconds (µs)
  • nanoseconds (ns)

For months and years, it is used the average number of days in a month and year respectively: DaysInYear and DaysInMonth.

It is advise to inform the user that the duration is approximate.

func SimpleFormat

func SimpleFormat(s any) string

func Singularize

func Singularize(plural string) string

Singularize returns the singular form of a word in a string.

Example
fmt.Println(Singularize("posts"))
fmt.Println(Singularize("octopi"))
fmt.Println(Singularize("sheep"))
fmt.Println(Singularize("word"))
fmt.Println(Singularize("CamelOctopi"))
Output:

post
octopus
sheep
word
CamelOctopus

func StripTags

func StripTags(input string) string

StripTags strips all tags from the input string using bluemonday.StrictPolicy

func Tableize

func Tableize(term string) string

Tableize creates the name of a table for an ORM model.

Example
fmt.Println(Tableize("RawScaledScorer"))
fmt.Println(Tableize("ham_and_egg"))
fmt.Println(Tableize("fancyCategory"))
Output:

raw_scaled_scorers
ham_and_eggs
fancy_categories

func TimeAgoInWords

func TimeAgoInWords(t time.Time) string

func TimeDistanceInWords

func TimeDistanceInWords(d time.Duration) string

func ToSentence

func ToSentence(last_join string, parts ...string) string

ToSentence convert a slice of strings into a sentence with the last_join string between the last two parts.

func ToSnakeCase

func ToSnakeCase(str string) string

func Truncate

func Truncate(text string, length int) string

func Underscorize

func Underscorize(term string) string

Underscorize converts strings to underscored, lowercase form.

Example
fmt.Println(Underscorize("MyAccount"))
fmt.Println(Underscorize("user-profile"))
fmt.Println(Underscorize("SSLError"))
fmt.Println(Underscorize("HTTPConnectionTimeout"))
fmt.Println(Underscorize("RESTfulController"))
fmt.Println(Underscorize("MultipleHTTPCalls"))
Output:

my_account
user_profile
ssl_error
http_connection_timeout
restful_controller
multiple_http_calls

Types

type DomIDer

type DomIDer interface {
	DomID() string
}

type Duration

type Duration time.Duration

func (Duration) String

func (d Duration) String() string

String will call ShortDuration on the duration.

type ErrorWithStack

type ErrorWithStack struct {
	Err error
	// contains filtered or unexported fields
}

ErrorWithStack is an error with a stacktrace attached

func NewError

func NewError(offset int, err error) *ErrorWithStack

func NewErrorf

func NewErrorf(offset int, format string, data ...any) *ErrorWithStack

func (ErrorWithStack) Error

func (e ErrorWithStack) Error() string

Error calls Error in the underlying error

func (ErrorWithStack) Stacktrace

func (e ErrorWithStack) Stacktrace() *Stacktrace

Trace returns the stacktrace

func (ErrorWithStack) String

func (e ErrorWithStack) String() string

String returns the error message and the stacktrace

func (ErrorWithStack) Unwrap

func (e ErrorWithStack) Unwrap() error

Unwrap calls Unwrap in the underlying error

type MemCache

type MemCache map[any][]byte

MemCache is a simple in-memory cache.

func (MemCache) Cache

func (c MemCache) Cache(fn func() ([]byte, error), key ...any) ([]byte, error)

Cache caches the result of fn in the cache.

type Panic

type Panic struct {
	Err        any
	Stacktrace []StackLine
	Stack      []byte
	PC         []uintptr
}

func NewPanic

func NewPanic(err any, data []byte, skip int) (p Panic)

NewPanic will generate a new panic that can be used as a normal error It is meant to be used with the recover() function

if err := recover() ; err != nil {
    err := NewErrPanic(err, debug.Stack())
    ...
}

func (Panic) Error

func (p Panic) Error() string

func (Panic) StackFrames

func (p Panic) StackFrames() []uintptr

func (Panic) String

func (p Panic) String() string

func (Panic) Unwrap

func (p Panic) Unwrap() error

type Set

type Set[T comparable] map[T]Void

func NewSet

func NewSet[T comparable](values ...T) Set[T]

func (Set[T]) Has

func (s Set[T]) Has(item T) bool

func (Set[T]) Set

func (s Set[T]) Set(item T)

func (Set[T]) Slice

func (s Set[T]) Slice() []T

type StackLine

type StackLine struct {
	L       int
	Package string
	Func    string
	File    string
	Line    string
}

func StackDecode

func StackDecode(data []byte) (sls []StackLine)

func (StackLine) RelFile

func (sl StackLine) RelFile() string

RelFile returns the relative path of the file

func (StackLine) String

func (sl StackLine) String() string

type Stacktrace

type Stacktrace struct {
	StackLines []StackLine
	// contains filtered or unexported fields
}

func (Stacktrace) String

func (s Stacktrace) String() string

type Strings

type Strings Set[string]

func NewStringSet

func NewStringSet(s ...string) Strings

func (Strings) Has

func (s Strings) Has(what string) bool

func (Strings) HasPrefix

func (s Strings) HasPrefix(what string) bool

func (Strings) Set

func (s Strings) Set(what string)

func (Strings) Slice

func (s Strings) Slice() []string

func (Strings) TrimPrefix

func (s Strings) TrimPrefix(what string) (prefix, trimmed string)

type Table

type Table struct {
	Header []string
	Values [][]string
}
Example
table := Table{
	Header: []string{"Title", "Age"},
	Values: [][]string{
		{"The film", "1"},
		{"The super super file"},
		{"", ""},
		{"Other Filem", "123123"},
	},
}

fmt.Println(table.String())
Output:

Title                Age
The film             1
The super super file

Other Filem          123123

func (*Table) String

func (t *Table) String() string

type ToParamer

type ToParamer interface {
	ToParam() string
}

type Value

type Value struct {
	Data []byte
	Obj  any
}

func NewValue

func NewValue(data any) *Value

func (Value) Base32

func (v Value) Base32() Value

func (Value) Base58

func (v Value) Base58() Value

func (Value) Base64

func (v Value) Base64() Value

func (Value) Bytes

func (v Value) Bytes() []byte

func (Value) Decode64

func (v Value) Decode64() Value

func (Value) Int

func (v Value) Int() int

func (Value) IsNil

func (v Value) IsNil() bool

func (Value) IsOk

func (v Value) IsOk() bool

func (Value) JSON

func (v Value) JSON() *Value

func (Value) SHA256

func (v Value) SHA256() Value

func (Value) String

func (v Value) String() string

func (Value) Uint

func (v Value) Uint() uint

type Void

type Void struct{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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