Skip to content

Commit

Permalink
updated docs and added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MyBlackJay committed Dec 27, 2023
1 parent 8b70818 commit 245837b
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}
```

0 comments on commit 245837b

Please sign in to comment.