humanize

package module
v0.0.0-...-142c3fe Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 11 Imported by: 1

README

humanize GoDoc Go Report Card Build Status codecov

Human readable formatting and input parsing for Go, with i18n support. Easily extendable with new languages.

Supported languages
  • English
  • Polish
Table of contents

Features

Decode duration from human input
duration, _ := humanizer.ParseDuration("2 days, 5 hours and 40 seconds")
fmt.Println(duration) 
// Prints: 53h0m40s
Humanize date difference
firstDate := time.Date(2017, 3, 21, 12, 30, 15, 0, time.UTC)
secondDate := time.Date(2017, 6, 21, 0, 0, 0, 0, time.UTC)

Approximate mode:

fmt.Println(humanizer.TimeDiff(firstDate, secondDate, false))
// Prints: in 3 months

Precise mode:

fmt.Println(humanizer.TimeDiff(secondDate, firstDate, true))
// Prints: 3 months, 1 day, 11 hours, 29 minutes and 45 seconds ago
Pretty print timestamps
fmt.Println(humanizer.SecondsToTimeString(67))
// Prints: 01:07
Add decimal separators to numbers

Uses x/text/number and is locale aware.

fmt.Println(humanizer.HumanizeNumber(1234.567, 2))
// Prints: 1,234.57
Decode value from human input with a prefix
value, _ := humanizer.ParsePrefix("1.5k")
fmt.Println(value)
// Prints: 1500

Bit prefixes are recognized as well:

value, _ := humanizer.ParsePrefix("1.5Ki")
fmt.Println(value)
// Prints: 1536

NOTE: ParsePrefix will return a precise value (big.Float), so you might get fractions where you wouldn't expect them (e.g. bytes). It's up to you to handle that.

Humanize big numbers with prefixes

Quick usage:

fmt.Println(humanizer.SiPrefixFast(174512))
// Prints: 174.5k

Controlled usage:

fmt.Println(humanizer.SiPrefix(1440000, 2, 1000, false))
// Prints: 1.44 mega

Using bit prefixes instead of metric:

fmt.Println(humanizer.BitPrefixFast(1509949))
// Prints: 1.44Mi
Humanize parts of one

Avoid leading zeroes:

fmt.Println(humanizer.HumanizeParts(0.25, 0))
// Prints: 25%
fmt.Println(humanizer.HumanizeParts(0.0023, 0))
// Prints: 2‰

Allow leading zeroes to avoid jumping to a smaller unit:

fmt.Println(humanizer.HumanizeParts(0.0023, 1))
// Prints: 0.23%

TODO

  • Smarter imprecise mode for time durations.
  • More features?

Documentation

Overview

Package humanize provides methods for formatting and parsing values in human readable form.

Index

Constants

View Source
const (
	Second   = 1
	Minute   = 60
	Hour     = 60 * Minute
	Day      = 24 * Hour
	Week     = 7 * Day
	Month    = 30 * Day
	Year     = 12 * Month
	LongTime = 35 * Year
)

Time constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Humanizer

type Humanizer struct {
	// contains filtered or unexported fields
}

Humanizer is the main struct that provides the public methods.

func New

func New(langName string) (*Humanizer, error)

New creates a new humanizer for a given language.

func (*Humanizer) BitPrefix

func (humanizer *Humanizer) BitPrefix(value float64, decimals int, threshold int64, short bool) string

BitPrefix appends a bit prefix to the value and converts it accordingly. Arguments:

value - value to be converted (should be in bytes).
decimals - decimal precision for the converted value.
threshold - upper bound of the value range to be ignored. Lower bound is 1/threshold.
short - whether to use short or long prefix.

func (*Humanizer) BitPrefixFast

func (humanizer *Humanizer) BitPrefixFast(value float64) string

BitPrefixFast is a convenience wrapper over BitPrefix. Precision is 2 decimal place. Will not prefix values smaller than 1024 and will append only the short prefix.

func (*Humanizer) HumanizeNumber

func (humanizer *Humanizer) HumanizeNumber(value float64, digits int) string

HumanizeNumber makes the number easily readable by adding decimal separators. Arguments:

value - the value to be formatted
digits - number of (max) fraction digits to be shown

func (*Humanizer) HumanizeParts

func (humanizer *Humanizer) HumanizeParts(value float64, allowedZeroes int) string

HumanizeParts makes part of one fractions human readable, e.g.: 0.02 -> 2%, 0.007 -> 7‰. Arguments:

value - the value to be formatted
allowedZeroes - number of leading zeroes allowed in the fraction before moving to a smaller unit

func (*Humanizer) ParseDuration

func (humanizer *Humanizer) ParseDuration(input string) (time.Duration, error)

ParseDuration will return time duration as parsed from input string.

func (*Humanizer) ParsePrefix

func (humanizer *Humanizer) ParsePrefix(input string) (*big.Float, error)

ParsePrefix will return a number as parsed from input string.

func (*Humanizer) SecondsToTimeString

func (humanizer *Humanizer) SecondsToTimeString(duration int64) string

SecondsToTimeString converts the time in seconds into a human readable timestamp, eg.:

76 -> 01:16
3620 -> 1:00:20

func (*Humanizer) SiPrefix

func (humanizer *Humanizer) SiPrefix(value float64, decimals int, threshold int64, short bool) string

SiPrefix appends a SI prefix to the value and converts it accordingly. Arguments:

value - value to be converted.
decimals - decimal precision for the converted value.
threshold - upper bound of the value range to be ignored. Lower bound is 1/threshold.
short - whether to use short or long prefix.

func (*Humanizer) SiPrefixFast

func (humanizer *Humanizer) SiPrefixFast(value float64) string

SiPrefixFast is a convenience function for easy prefixing with a SI prefix. Precision is 1 decimal place. Will not prefix values in range 0.01 - 1000 and will append only the short prefix.

func (*Humanizer) TimeDiff

func (humanizer *Humanizer) TimeDiff(startDate, endDate time.Time, precise bool) string

TimeDiff will return the humanized time difference between the given dates. Precise setting determines whether a rough approximation or exact description should be returned, e.g.:

precise=false -> "3 months"
precise=true  -> "2 months and 10 days"

func (*Humanizer) TimeDiffNow

func (humanizer *Humanizer) TimeDiffNow(date time.Time, precise bool) string

TimeDiffNow is a convenience method returning humanized time from now till date.

Jump to

Keyboard shortcuts

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