Documentation ¶
Index ¶
- Variables
- func Cache(fn func() ([]byte, error), key ...any) ([]byte, error)
- func Camelize(term string) string
- func ClearCache()
- func Dasherize(term string) string
- func DomID(s any) (string, error)
- func ForeignKey(term string) string
- func Html2Markdown(input any) string
- func Humanize(term string) string
- func IDFor(s any) (string, error)
- func MeasureText[T any](name string, fn func() T) T
- func Must[T any](obj T) T
- func NameOf(obj any) string
- func NameOfWithPackage(obj any) string
- func Ordinal(number int64) string
- func Ordinalize(number int64) string
- func Pluralize(singular string) string
- func RandomString(n int) string
- func Sanitize(input string) string
- func ShortDuration(d time.Duration) string
- func SimpleFormat(s any) string
- func Singularize(plural string) string
- func StripTags(input string) string
- func Tableize(term string) string
- func TimeAgoInWords(t time.Time) string
- func TimeDistanceInWords(d time.Duration) string
- func ToSentence(last_join string, parts ...string) string
- func ToSnakeCase(str string) string
- func Truncate(text string, length int) string
- func Underscorize(term string) string
- type DomIDer
- type Duration
- type ErrorWithStack
- type MemCache
- type Panic
- type Set
- type StackLine
- type Stacktrace
- type Strings
- type Table
- type ToParamer
- type Value
- func (v Value) Base32() Value
- func (v Value) Base58() Value
- func (v Value) Base64() Value
- func (v Value) Bytes() []byte
- func (v Value) Decode64() Value
- func (v Value) Int() int
- func (v Value) IsNil() bool
- func (v Value) IsOk() bool
- func (v Value) JSON() *Value
- func (v Value) SHA256() Value
- func (v Value) String() string
- func (v Value) Uint() uint
- type Void
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultCache = MemCache{}
var Month = Year / 12
var ShouldCache = false
ShouldCache set if the inflector should (or not) cache the inflections
var Year = day * (365 + 365 + 365 + 366) / 4
Functions ¶
func Camelize ¶
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 ¶
Dasherize converts strings to dashed, lowercase form.
Example ¶
fmt.Println(Dasherize("MyAccount")) fmt.Println(Dasherize("user_profile"))
Output: my-account user-profile
func ForeignKey ¶
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 MeasureText ¶
func NameOfWithPackage ¶
func Ordinal ¶
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 ¶
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 ¶
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 Sanitize ¶
Sanitize sanitizes the input string using bluemonday.UGCPolicy
func ShortDuration ¶
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 Singularize ¶
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 ¶
StripTags strips all tags from the input string using bluemonday.StrictPolicy
func Tableize ¶
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 TimeDistanceInWords ¶
func ToSentence ¶
ToSentence convert a slice of strings into a sentence with the last_join string between the last two parts.
func ToSnakeCase ¶
func Underscorize ¶
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 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 (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 Panic ¶
func NewPanic ¶
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) StackFrames ¶
type Set ¶
type Set[T comparable] map[T]Void
func NewSet ¶
func NewSet[T comparable](values ...T) Set[T]
type StackLine ¶
func StackDecode ¶
type Stacktrace ¶
type Stacktrace struct { StackLines []StackLine // contains filtered or unexported fields }
func (Stacktrace) String ¶
func (s Stacktrace) String() string
type Table ¶
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