go-humanizer

module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2016 License: MIT

README

Humanizer

Software License Build Status GoDoc codecov.io Go Report Card

Humanize values to make them easier to read.

Installation

go get github.com/sbani/go-humanizer

Usage

Strings
Humanize:
import "github.com/sbani/go-humanizer/strings"

Humanize("news_count", true) // "News count"
Humanize("User", false) // "user"
Humanize("news_id", true) // "News"
Truncate:

Truncate string but never cut within a word.

import "github.com/sbani/go-humanizer/strings"

textShort := "Short text"
Truncate(textShort, 1) // Short
Truncate(textShort, 6) // Short
Truncate(textShort, 7) // Short text
Binary Suffix
import "github.com/sbani/go-humanizer/strings"

s := BinarySuffix(0) // "0 bytes"
s := BinarySuffix(1536) // "1.5 kB"
s := BinarySuffix(1048576 * 5) // "5.00 MB"
s := BinarySuffix(1073741824 * 2) // "2.00 GB"
Numbers
Ordinalize:
import "github.com/sbani/go-humanizer/numbers"

Ordinalize(0) // "0th"
Ordinalize(1) // "1st"
Ordinalize(2) // "2nd"
Ordinalize(23) // "23rd"
Ordinalize(1002) // "1002nd"
Ordinalize(-111) // "-111th"
Ordinal:
import "github.com/sbani/go-humanizer/numbers"

Ordinal(0) // "th"
Ordinal(1) // "st"
Ordinal(2) // "nd"
Ordinal(23) // "rd"
Ordinal(1002) // "nd"
Ordinal(-111) // "th"
Roman:
import "github.com/sbani/go-humanizer/numbers"

s, err := ToRoman(1) // "I"
s, err := ToRomanToRoman(5) // "V"
s, err := ToRomanToRoman(1300) // "MCCC"

i, err := ToRomanFromRoman("MMMCMXCIX") // 3999
i, err := ToRomanFromRoman("V") // 5
i, err := ToRomanFromRoman("CXXV") // 125
Collection
Oxford
import "github.com/sbani/go-humanizer/collection"

Oxford([]string{"Albert"}, -1) // "Albert"
Oxford([]string{"Albert", "Norbert"}, -1) // "Albert and Norbert"
Oxford([]string{"Albert", "Norbert", "Michael", "Kevin"}, -1) // "Albert, Norbert, Michael and Kevin"
Oxford([]string{"Albert", "Norbert", "Michael", "Kevin"}, 2)) // Albert, Norbert and 2 more

License

MIT License. See LICENSE file for more informations.

Credits

A special WOW goes to PHP Humanizer. (This lib is just a port)

Contributions

Contributions are very welcome! Feel free to contact me, send a PR or open an issue.

Roadmap

Things that are missing:

  • Strings: Humanize
  • Strings: Truncate
  • Numbers: Roman
  • Numbers: Ordinal
  • Collection: Oxford
  • Numbers: Binary Suffix
  • Numbers: Metric Suffix
  • Date time: Difference
  • Date time: Precise difference
  • Translations

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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