From ccc5220ca87662449df61f49e66e689e36d04deb Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Tue, 31 May 2022 15:55:40 -0400 Subject: [PATCH] util round and trinary --- scada-common/util.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/scada-common/util.lua b/scada-common/util.lua index 7073d38..5bbe2b8 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -5,6 +5,17 @@ ---@class util local util = {} +-- OPERATORS -- + +-- trinary operator +---@param cond boolean condition +---@param a any return if true +---@param b any return if false +---@return any value +util.trinary = function (cond, a, b) + if cond then return a else return b end +end + -- PRINT -- -- print @@ -56,6 +67,9 @@ util.concat = function (...) return str end +-- alias +util.c = util.concat + -- sprintf implementation ---@param format string ---@vararg any @@ -63,6 +77,14 @@ util.sprintf = function (format, ...) return string.format(format, table.unpack(arg)) end +-- MATH -- + +-- round a number to an integer +---@return integer rounded +util.round = function (x) + return math.floor(x + 0.5) +end + -- TIME -- -- current time