strings

package
v3.24.7 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package duration contains routines to parse standard units of time.

Package strings contains functions to manipulate strings

Index

Constants

View Source
const (
	BYTE = 1 << (10 * iota)
	KILOBYTE
	MEGABYTE
	GIGABYTE
	TERABYTE
	PETABYTE
	EXABYTE
)

Variables

View Source
var (
	Nanosecond  = Duration(time.Nanosecond)
	Microsecond = Duration(time.Microsecond)
	Millisecond = Duration(time.Millisecond)
	Second      = Duration(time.Second)
	Minute      = Duration(time.Minute)
	Hour        = Duration(time.Hour)
	Day         = Hour * 24
	Week        = Day * 7
	Fortnight   = Week * 2
	Month       = Day * 30    // Approximation
	Year        = Day * 365   // Approximation
	Decade      = Year * 10   // Approximation
	Century     = Year * 100  // Approximation
	Millennium  = Year * 1000 // Approximation
)

Standard unit of time.

View Source
var DurationAgeGen = cel.Function("Age",
	cel.Overload("duration.Age",
		[]*cel.Type{cel.AnyType},
		cel.DurationType,
		cel.UnaryBinding(age),
	),
)
View Source
var DurationAgeGen2 = cel.Function("age",
	cel.Overload("duration.age",
		[]*cel.Type{cel.AnyType},
		cel.DurationType,
		cel.UnaryBinding(age),
	),
)
View Source
var Durations = []cel.EnvOption{
	cel.Function("duration",
		cel.Overload("double.duration",
			[]*cel.Type{cel.DoubleType},
			cel.DurationType,
			cel.UnaryBinding(func(value ref.Val) ref.Val {
				return types.Duration{Duration: time.Duration(conv.ToInt64(value.Value()))}
			}),
		),
	),
	cel.Function("Duration",
		cel.Overload("duration.Duration",
			[]*cel.Type{cel.StringType},
			cel.DurationType,
			cel.UnaryBinding(func(value ref.Val) ref.Val {
				a, err := ParseDuration(value.Value().(string))
				if err != nil || a == nil {
					return types.Duration{}
				}
				return types.Duration{Duration: *a}
			}),
		),
	),
}

Functions

func Age added in v3.20.5

func Age(s string) time.Duration

func CamelCase

func CamelCase(in string) string

CamelCase -

func HumanBytes added in v3.20.4

func HumanBytes(size interface{}) string

ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:

E: Exabyte
P: Petabyte
T: Terabyte
G: Gigabyte
M: Megabyte
K: Kilobyte
B: Byte

The unit that results in the smallest number greater than or equal to 1 is always chosen. Input is the size in bytes.

func HumanDuration added in v3.20.4

func HumanDuration(duration interface{}) string
Returns a string representing of a duration in the form "3d1h3m".

Leading zero units are omitted. As a special case, durations less than one second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure that the leading digit is non-zero. Duration more than a day or more than a week lose granularity and are truncated to resp. days-hours-minutes and weeks-days-hours. The zero duration formats as 0s.

func Indent

func Indent(width int, indent, s string) string

Indent - indent each line of the string with the given indent string. Any indent characters are permitted, except for '\n'.

TODO: return an error if the indent string contains '\n' instead of succeeding

func KebabCase

func KebabCase(in string) string

KebabCase -

func ParseDuration added in v3.20.4

func ParseDuration(val string) (*time.Duration, error)

func Semver added in v3.20.4

func Semver(version string) (*sv2.Version, error)

func SemverCompare added in v3.20.4

func SemverCompare(constraint, version string) (bool, error)

func ShellQuote

func ShellQuote(s string) string

ShellQuote - generate a POSIX shell literal evaluating to a string

func SnakeCase

func SnakeCase(in string) string

SnakeCase -

func Sort deprecated

func Sort(list []string) []string

Sort - return an alphanumerically-sorted list of strings

Deprecated: use coll.Sort instead

func Trunc

func Trunc(length int, s string) string

Trunc - truncate a string to the given length

func WordWrap

func WordWrap(in string, opts WordWrapOpts) string

WordWrap - insert line-breaks into the string, before it reaches the given width.

Types

type Duration added in v3.20.4

type Duration time.Duration

Duration is a standard unit of time.

func (Duration) Days added in v3.20.4

func (d Duration) Days() float64

Days returns the duration as a floating point number of days.

func (Duration) Hours added in v3.20.4

func (d Duration) Hours() float64

Hours returns the duration as a floating point number of hours.

func (Duration) Minutes added in v3.20.4

func (d Duration) Minutes() float64

Minutes returns the duration as a floating point number of minutes.

func (Duration) Nanoseconds added in v3.20.4

func (d Duration) Nanoseconds() int64

Nanoseconds returns the duration as an integer nanosecond count.

func (Duration) Seconds added in v3.20.4

func (d Duration) Seconds() float64

Seconds returns the duration as a floating point number of seconds.

func (Duration) String added in v3.20.4

func (d Duration) String() string

String returns a string representing the duration in the form "3d1h3m". Leading zero units are omitted. As a special case, durations less than one second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure that the leading digit is non-zero. Duration more than a day or more than a week lose granularity and are truncated to resp. days-hours-minutes and weeks-days-hours. The zero duration formats as 0s.

func (Duration) Weeks added in v3.20.4

func (d Duration) Weeks() float64

Weeks returns the duration as a floating point number of days.

type InvalidDuration added in v3.20.19

type InvalidDuration struct {
	Duration string
}

func (InvalidDuration) Error added in v3.20.19

func (m InvalidDuration) Error() string

type WordWrapOpts

type WordWrapOpts struct {
	// Line-break sequence to insert (defaults to "\n")
	LBSeq string

	// The desired maximum line length in characters (defaults to 80)
	Width uint
}

WordWrapOpts defines the options to apply to the WordWrap function

Jump to

Keyboard shortcuts

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