Import std.text.humanize
std.text.humanize
Numbers, sizes, times, and names written as people say them, in English.
Plurals ("3 files"), how long ago or from now a time is ("3 minutes ago"), ordinals ("22nd"), numbers in words ("forty-two"), and a name in each case style a program uses: snake_case, camelCase, PascalCase, and kebab-case. FormatBytes and FormatDuration say a size ("1.2 MB") and a Duration ("1 hour and 30 minutes").
std/text/humanize.fb
Types
Humanize
class
Public Class Humanize
Humanize's operations.
Examples
Expect(Humanize.Plural(3, "box") == "3 boxes")
Expect(Humanize.Ordinal(22) == "22nd")
Expect(Humanize.Words(42) == "forty-two")
Expect(Humanize.SnakeCase("HttpServerName") == "http_server_name")
Expect(Humanize.Relative(-180) == "3 minutes ago")-
word's plural: "city" is "cities", "box" "boxes", "leaf" "leaves", "person" "people", and "sheep" "sheep". Of several words, the last one's. A word keeps its capitals, as "TreeNode" does in "TreeNodes", and one in capitals has its ending in them too. -
countandword, thewordin its plural unlesscountis 1 or -1: "3 files", "1 file", and "0 files". -
How long ago a time
secondsfrom now was, whensecondsis negative, "3 minutes ago", or how far off it is, "in 3 minutes", rounded to the unit people would say: "just now" under 45 seconds either way, then a minute, minutes, an hour, hours, a day, days, a month, months, a year, and years. -
How long ago a time
dbefore now was, whendis before 0, or how far off one d from now is, as Relative says it ofd's whole seconds: "3 minutes ago" and "in 2 hours". -
nwith its ordinal's ending: "1st", "2nd", "3rd", "4th", "11th", "22nd", and "113th". -
nin words: "zero", "forty-two", "one thousand two hundred thirty-four", and "minus seven". -
text's words in snake_case: "Hello World", "helloWorld", and "HTTPServer2" are "hello_world", "hello_world", and "http_server2". Words are split at spaces, punctuation, and capitals that start one, and a run of capitals is one word. -
text's words in kebab-case: "hello-world". -
text's words in camelCase: "helloWorld", "httpServer2". -
text's words in PascalCase: "HelloWorld", "HttpServer2".
Functions
FormatBytes
function
Public Function FormatBytes(count: Int64, binary: Bool = False): String
count bytes as people say them, in the largest unit they make at least 1 of, to a tenth,
rounded half up: "1.2 MB", "999 B", and "-3.4 GB". The units are 1000 apart, or with binary,
1024: "1.2 MiB".
Examples
Expect(FormatBytes(999) == "999 B")
Expect(FormatBytes(1_234_567) == "1.2 MB")
Expect(FormatBytes(1536, True) == "1.5 KiB")
FormatDuration
function
Public Function FormatDuration(d: Duration): String
d in words: each of its days, hours, minutes, and seconds that isn't 0, the seconds with their
fraction, as "1 hour, 2 minutes, and 3.5 seconds" and "2 days and 1 minute". Under a second,
it's in its largest unit: "250 milliseconds", "1.5 microseconds", and "80 nanoseconds". It's
"0 seconds", and one before 0 is "minus" one after.
Examples
Expect(FormatDuration(90.Seconds) == "1 minute and 30 seconds")Report an issue with this page or with FutureBlitz on GitHub.