util

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB = 1000
	MB = 1000 * KB
	GB = 1000 * MB
	TB = 1000 * GB
	PB = 1000 * TB

	KiB = 1024
	MiB = 1024 * KiB
	GiB = 1024 * MiB
	TiB = 1024 * GiB
	PiB = 1024 * TiB
)

See: http://en.wikipedia.org/wiki/Binary_prefix

Variables

This section is empty.

Functions

func Assign

func Assign(dst any, src any, excludeFieldIndexes []int)

https://stackoverflow.com/questions/23350173 copy non-empty field values from src to dst. dst and src must be pointors of same type of plain struct

func BytesSize

func BytesSize(size float64) string

BytesSize returns a human-readable size in bytes, kibibytes, mebibytes, gibibytes, or tebibytes (e.g. "44kiB", "17MiB").

func BytesSizeAround

func BytesSizeAround(size float64) string

Return at most 6 chars, e.g. "123.1G". It removes trailing zero(s) and dot, e.g. "123.0GiB" => "123G".

func CleanBasename

func CleanBasename(name string) string

Return a cleaned safe base filename (without path). 1. CleanBaseFilenameComponent. 2. Clean trailing dot (".") (Windows does NOT allow dot in the end of filename) 3. TrimSpace 4. Truncate name to at most 240 (UTF-8 string) bytes.

func CleanBasenameComponent

func CleanBasenameComponent(name string) string

Return a cleaned safe base filename component. 1. Replace invalid chars with alternatives (e.g. "?" => "?"). 2. CleanTitle (clean \r, \n and other invisiable chars then TrimSpace).

func CleanFileBasename

func CleanFileBasename(name string) string

Similar to CleanBaseName, but treats name as a filename (base+ext) and tries to preserve ext. It also removes spaces between base and ext.

func CleanFilePath

func CleanFilePath(p string) string

Similar to CleanPath but treat p as a file path (the basename of p contains ext), and try to preserve ext in basename.

func CleanPath

func CleanPath(p string) string

Clean & normaliza & sanitize p path. 1. path.Clean. 2. Replace \ with / . 3. Clean each part of the path, replace invalid chars with alternatives, truncate too long names.

func CountNonZeroVariables

func CountNonZeroVariables(vars ...any) (cnt int)

Return count of variable in vars that fulfil the condition that variable is non-zero value

func CustomSize

func CustomSize(format string, size float64, base float64, _map []string) string

CustomSize returns a human-readable approximation of a size using custom format.

func DomSelectionText

func DomSelectionText(s *goquery.Selection) string

Return deduplicated and combined text of dom elements. That is, if both parent and child node are in the same selection, it will only write once of parent's content to result, ignoring the (duplicate) child node. It will Clean result.

func ExistsFileWithAnySuffix

func ExistsFileWithAnySuffix(name string, suffixes ...string) string

Check whether a file (or dir) with name + <any suffix in list> exists in file system. If exists, return name + suffix. Else, return empty string. If len(suffixes) == 0, return name if itself exists.

func FileExists

func FileExists(name string) bool

Check whether a file (or dir) with name exists in file system

func FilterSlice

func FilterSlice[T any](ss []T, test func(T) bool) (ret []T)

Return filtered ss. The ret is nil if and only if ss is nil.

func First

func First[T any](t T, args ...any) T

Return t unconditionally.

func FirstNonZeroArg

func FirstNonZeroArg[T comparable](args ...T) T

func FormatDate

func FormatDate(t int64) string

func FormatTime

func FormatTime(t int64) string

func FromHumanSize

func FromHumanSize(size string) (int64, error)

FromHumanSize returns an integer from a human-readable specification of a size using SI standard (e.g. "44kB", "17MB").

func GetExtFromType

func GetExtFromType(typ string) string

typ: "Content-Type" header.

func GetFunctionName

func GetFunctionName(i any) string

func HumanSize

func HumanSize(size float64) string

HumanSize returns a human-readable approximation of a size capped at 4 valid numbers (e.g. "2.746 MB", "796 KB").

func HumanSizeWithPrecision

func HumanSizeWithPrecision(size float64, precision int) string

HumanSizeWithPrecision allows the size to be in any precision, instead of 4 digit precision used in units.HumanSize.

func LogAzureHttpRequest

func LogAzureHttpRequest(req *azuretls.Request)

Log if dump-headers flag is set.

func LogAzureHttpResponse

func LogAzureHttpResponse(res *azuretls.Response, err error)

Log if dump-headers flag is set.

func LookPathWithSelfDir

func LookPathWithSelfDir(name string) (string, error)

Similar to exec.LookPath, but also look up for dir of self executable file.

func MakeCleanTmpDir

func MakeCleanTmpDir(tmpdir string) error

Make a new empty temp dir at tmpdir location. If tmpdir already exists, clean it first(remove itself with all contents inside it).

func Map

func Map[T1 any, T2 any](ss []T1, mapper func(T1) T2) (ret []T2)

func Md5

func Md5(input string) string

func OmitemptySlice

func OmitemptySlice[T comparable](slice []T) (list []T)

func ParseInt

func ParseInt[T constraints.Integer](s string, defaultValue T) T

func PrintJson

func PrintJson(output io.Writer, value any) error

func RAMInBytes

func RAMInBytes(size string) (int64, error)

RAMInBytes parses a human-readable string representing an amount of RAM in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and returns the number of bytes, or -1 if the string is unparseable. Units are case-insensitive, and the 'b' suffix is optional. Specially, if size is "-1", return -1,nil

func Sleep

func Sleep(seconds int)

func SplitCsv

func SplitCsv(str string) []string

split a csv like line to values. "a, b, c" => [a,b,c]

func UniqueSlice

func UniqueSlice[T comparable](slice []T) (list []T)

func UnmarshalJson

func UnmarshalJson[T any](source []byte) (T, error)

Unmarshal source as json of type T

func Unwrap

func Unwrap[T any](t T, err error) T

Panic if err is not nil, otherwise return t

func Xlsx2Csv

func Xlsx2Csv(xlsxFile string) error

Convert "abc.xlsx" to "abc.xlsx.csv". Adopted from https://github.com/shenwei356/csvtk/blob/master/csvtk/cmd/xlsx2csv.go .

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳