aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2020-06-03 20:16:00 +0200
committerKim Alvefur <zash@zash.se>2020-06-03 20:16:00 +0200
commitbed3607b09feff29c48bc543a5ff5d93deffda5f (patch)
treecb4cce911aaf54886475a90661f3eaa219463e3c /util
parent3a3d0bad1c2dfe2437488c1dfa12ca98f9a1b5b5 (diff)
downloadprosody-bed3607b09feff29c48bc543a5ff5d93deffda5f.tar.gz
prosody-bed3607b09feff29c48bc543a5ff5d93deffda5f.zip
util.human.units: Put math functions into locals
Primarily because the next commit will deal with math.log behaving differently on Lua 5.1 and that's eaiser with locals.
Diffstat (limited to 'util')
-rw-r--r--util/human/units.lua12
1 files changed, 9 insertions, 3 deletions
diff --git a/util/human/units.lua b/util/human/units.lua
index 91d6f0d5..471c82a0 100644
--- a/util/human/units.lua
+++ b/util/human/units.lua
@@ -1,3 +1,9 @@
+local math_abs = math.abs;
+local math_ceil = math.ceil;
+local math_floor = math.floor;
+local math_log = math.log;
+local math_max = math.max;
+local math_min = math.min;
local unpack = table.unpack or unpack; --luacheck: ignore 113
local large = {
@@ -36,7 +42,7 @@ local binary = {
-- unit: string, the base unit
-- b: optional enum 'b', thousands base
local function format(n, unit, b) --> string
- local round = math.floor;
+ local round = math_floor;
local prefixes = large;
local logbase = 1000;
local fmt = "%.3g %s%s";
@@ -48,9 +54,9 @@ local function format(n, unit, b) --> string
logbase = 1024;
elseif n < 1 then
prefixes = small;
- round = math.ceil;
+ round = math_ceil;
end
- local m = math.max(0, math.min(8, round(math.abs(math.log(math.abs(n), logbase)))));
+ local m = math_max(0, math_min(8, round(math_abs(math_log(math_abs(n), logbase)))));
local prefix, multiplier = unpack(prefixes, m * 2-1, m*2);
return fmt:format(n / (multiplier or 1), prefix or "", unit);
end