utils

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: GPL-3.0 Imports: 21 Imported by: 2

Documentation

Index

Constants

View Source
const Fractions = "¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞"

Variables

This section is empty.

Functions

func AppendUnique

func AppendUnique(s []string, v string) []string

func BaseUrl

func BaseUrl(urlStr string) string

func Cleanup

func Cleanup(s string) string

func CleanupInline

func CleanupInline(s string) string

func CleanupLang

func CleanupLang(lang string) string

func Deduplicate

func Deduplicate[E comparable](s []E) []E

func DomainZone

func DomainZone(urlStr string) string

func FindFloat

func FindFloat(val any) float64

func FindInt

func FindInt(val any) int

func FindNumber

func FindNumber(str string) *float64

func FirstMetaContent added in v0.17.0

func FirstMetaContent(el *goquery.Selection, names ...string) (string, bool)

FirstMetaContent returns the content attribute of the first matching meta-element from a list of identifiers.

func FormatFraction

func FormatFraction(f float64) string

func GetMetaContent added in v0.17.0

func GetMetaContent(el *goquery.Selection, key, name string) (string, bool)

GetMetaContent retrieves the "content" attribute of a meta tag matching the specified key and name in the selection.

func HostAlias

func HostAlias(urlStr string) string

func Hostname

func Hostname(urlStr string) string

func IsAbsolute

func IsAbsolute(urlStr string) bool

func LanguageByDomain

func LanguageByDomain(tld string) (string, bool)

func Parse8601Duration added in v0.3.0

func Parse8601Duration(str string) time.Duration

func ParseDate added in v0.19.0

func ParseDate(str string) (*time.Time, bool)

func ParseDuration

func ParseDuration(str string) (time.Duration, bool)

func ParseFloat

func ParseFloat(str string) (float64, error)

func ParseFraction

func ParseFraction(str string) (float64, error)

func ParseInt

func ParseInt(str string) (int, error)

func ParseKilocalories added in v0.20.0

func ParseKilocalories(str string) *float64

ParseKilocalories parses str as an energy value and converts it to kilocalories. Returns nil if str has a recognized unit that isn't energy (e.g. "12 g").

func ParseMass added in v0.20.0

func ParseMass(str string, target Unit) *float64

ParseMass parses str as a mass and converts it to target (e.g. UnitGram, UnitMilligram). Returns nil if str has a recognized unit that isn't a mass (e.g. "12 kcal") - wrong scale entirely, so there's no sensible number to fall back on.

func RecoverPanic added in v0.20.0

func RecoverPanic(label string, errp *error)

RecoverPanic recovers a panic in the deferring function and converts it into an error assigned to *errp, logging the stack trace under label.

Must be used as `defer utils.RecoverPanic(label, &err)` in a function with a named error return — recover() only stops a panic when called directly by the deferred function itself, so wrapping this call in another closure (e.g. `defer func() { utils.RecoverPanic(...) }()`) will not catch anything.

func RemoveDoubleSpace

func RemoveDoubleSpace(str string) string

func RemoveNewLines

func RemoveNewLines(s string) string

func RemoveSpaces

func RemoveSpaces(s string) string

func RemoveTrailingSlash

func RemoveTrailingSlash(urlStr string) string

func SplitParagraphs

func SplitParagraphs(s string) []string

func SplitTitle

func SplitTitle(title string) []string

func ToAbsoluteUrl

func ToAbsoluteUrl(base *url.URL, urlStr string) string

func TrimZeroWidthSpaces

func TrimZeroWidthSpaces(s string) string

func Unquote

func Unquote(s string) string

func UrlMatchesPathPattern added in v0.17.0

func UrlMatchesPathPattern(rawUrl, pattern string, targetHost ...string) bool

UrlMatchesPathPattern checks if rawUrl's path starts with pattern. If targetHost is supplied, absolute URLs with a non-matching host are rejected.

Types

type RequestConfig added in v0.16.0

type RequestConfig struct {
	Method  string
	URL     string
	Body    io.Reader
	Headers http.Header
}

type RequestResult added in v0.20.0

type RequestResult struct {
	Body        []byte
	ContentType string
	URL         *url.URL
}

func ExecuteRequest added in v0.16.0

func ExecuteRequest(config RequestConfig, options model.RequestOptions) (*RequestResult, error)

type Unit added in v0.20.0

type Unit string

Unit is a recognized mass or energy unit found alongside a number.

Only SI/scientific abbreviations are recognized (g, mg, kcal, kJ, г, мг, ...), not translated unit words, since these are used verbatim across most languages' nutrition labels. Scripts/words not listed here are left unrecognized and fall back to a bare number.

const (
	UnitMicrogram   Unit = "mcg"
	UnitMilligram   Unit = "mg"
	UnitGram        Unit = "g"
	UnitKilogram    Unit = "kg"
	UnitKilocalorie Unit = "kcal"
	UnitKilojoule   Unit = "kj"
)

func FindNumberWithUnit added in v0.20.0

func FindNumberWithUnit(str string) (value *float64, unit Unit)

FindNumberWithUnit extracts the first number in str and its unit, if recognized. unit is "" when none is found, meaning the caller should assume its own default.

Jump to

Keyboard shortcuts

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