diff --git a/README.md b/README.md index e9277df..3e7da15 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,67 @@ -# A library for parsing a human-readable format for recording data dimensions. -This library parses a human-readable format for recording data dimensions in the number of bytes. - +# Humansize +This library parses a human-readable format for writing data measurements in byte counts, or turns a byte count into a data measurement format string. ## Installation ``` go get github.com/MyBlackJay/humansize ``` +## Example +[Usage](https://go.dev/play/p/pux9fwKARrG) +```go +package main + +import ( + "fmt" + "github.com/MyBlackJay/humansize" +) + +func formatAndPrintKB() { + size := "100KB" + + if parsing, err := humansize.Compile(size); err == nil { + fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64()) + } +} + +func formatAndPrintMiB() { + size := "1MiB" + + if parsing, err := humansize.Compile(size); err == nil { + fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64()) + } +} + +func MustCompileMiWithError() { + defer func() { + if err := recover(); err != nil { + fmt.Println(err) + } + }() + + size := "1MR" + + parsing := humansize.MustCompile(size) + fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64()) +} + +func validateMeasureAndPrint() { + measure := "EiR" + fmt.Println(humansize.ValidateMeasure(measure)) +} + +func TurnBytesIntoToSizeAndPrint() { + size := 2.596 * float64(1<<60) + if res, err := humansize.BytesToSize(size, 10); err == nil { + fmt.Println(res) + } +} + +func main() { + formatAndPrintKB() // 100KB 1024 102400 + formatAndPrintMiB() // 1MiB 1048576 1048576 + MustCompileMiWithError() // unsupported data size format + validateMeasureAndPrint() // false + TurnBytesIntoToSizeAndPrint() // 2.5960000000EB +} +``` +