Annotate

util/human/units.lua @ 10888:42a0d9089de9

util.human.units: Handle location of unpack() in Lua 5.1
author Kim Alvefur <zash@zash.se>
date Wed, 03 Jun 2020 19:46:17 +0200
parent 10886:994c4a333199
child 10889:25e0ec11b4e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10888
42a0d9089de9 util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 10886
diff changeset
1 local unpack = table.unpack or unpack; --luacheck: ignore 113
42a0d9089de9 util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 10886
diff changeset
2
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local large = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 "k", 1000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 "M", 1000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 "G", 1000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 "T", 1000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 "P", 1000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 "E", 1000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 "Z", 1000000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 "Y", 1000000000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local small = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 "m", 0.001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 "μ", 0.000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 "n", 0.000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 "p", 0.000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 "f", 0.000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 "a", 0.000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 "z", 0.000000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 "y", 0.000000000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local binary = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 "Ki", 2^10,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 "Mi", 2^20,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 "Gi", 2^30,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 "Ti", 2^40,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 "Pi", 2^50,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 "Ei", 2^60,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 "Zi", 2^70,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 "Yi", 2^80,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- n: number, the number to format
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- unit: string, the base unit
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 -- b: optional enum 'b', thousands base
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local function format(n, unit, b) --> string
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local round = math.floor;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local prefixes = large;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local logbase = 1000;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local fmt = "%.3g %s%s";
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if n == 0 then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return fmt:format(n, "", unit);
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if b == 'b' then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 prefixes = binary;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 logbase = 1024;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 elseif n < 1 then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 prefixes = small;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 round = math.ceil;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 local m = math.max(0, math.min(8, round(math.abs(math.log(math.abs(n), logbase)))));
10888
42a0d9089de9 util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 10886
diff changeset
54 local prefix, multiplier = unpack(prefixes, m * 2-1, m*2);
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return fmt:format(n / (multiplier or 1), prefix or "", unit);
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 return {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 format = format;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 };