Help us shape the future of Go! Take the 2026 Go Developer Survey

humanize

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

README

humanize GoDoc

Usage

local humanize = require("humanize")
local time = require("time")

-- humanize.parse_bytes
local size, err = humanize.parse_bytes("1.3GiB")
if err then error(err) end
if not(size == 1395864371) then error("size: "..tostring(size)) end

-- humanize.ibytes
local size_string = humanize.ibytes(1395864371)
if not(size_string == "1.3 GiB") then error("ibytes: "..size_string) end

-- humanize.time
local t = time.unix() - 2
local time_string = humanize.time(t)
if not(time_string == "2 seconds ago") then error("time: "..time_string) end

-- humanize.si
local si_result = humanize.si(0.212121, "m")
if not(si_result == "212.121 mm") then error("si: "..tostring(si_result)) end

Documentation

Overview

Port dustin/go-humanize for gopher-lua

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IBytes

func IBytes(L *lua.LState) int

IBytes lua humanize.ibytes(number) return string

Example

humanize.ibytes(number)

state := lua.NewState()
Preload(state)
source := `
local humanize = require("humanize")
print(humanize.ibytes(1395864371))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
1.3 GiB

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func ParseBytes

func ParseBytes(L *lua.LState) int

ParseBytes lua humanize.parse_bytes(string) returns (number, err)

Example

humanize.parse_bytes(string)

state := lua.NewState()
Preload(state)
source := `
local humanize = require("humanize")
print(humanize.parse_bytes("1.3GiB"))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
1395864371

func Preload

func Preload(L *lua.LState)

Preload adds humanize to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local humanize = require("humanize")

func SI

func SI(L *lua.LState) int

SI lua humanize.si(number, string) return string

Example

humanize.si(input, unit)

state := lua.NewState()
Preload(state)
source := `
local humanize = require("humanize")
print(humanize.si(0.212121, "m"))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
212.121 mm

func Time

func Time(L *lua.LState) int

Time lua humanize.time(number) return string

Example

humanize.time(number)

state := lua.NewState()
Preload(state)
time.Preload(state)
source := `
local humanize = require("humanize")
local time = require("time")
print(humanize.time(time.unix() - 61))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
1 minute ago

Types

This section is empty.

Jump to

Keyboard shortcuts

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