Skip to content

Commit

Permalink
helper functions: enum(t), dict(t), tinvert(t), switch(case), printd(…
Browse files Browse the repository at this point in the history
…...), printf(f,...), dump(t, startkey)
  • Loading branch information
speedwaystar committed Jan 9, 2023
1 parent 19c6b50 commit ebbee7f
Showing 1 changed file with 128 additions and 25 deletions.
153 changes: 128 additions & 25 deletions SmartBuff.globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,137 @@ SMARTBUFF_CONST_AUTOSOUND = "Deathbind Sound";
--SMARTBUFF_CONST_AUTOSOUND = "GLUECREATECHARACTERBUTTON";

--[[
SystemFont
GameFontNormal
GameFontNormalSmall
GameFontNormalLarge
GameFontHighlight
GameFontHighlightSmall
GameFontHighlightSmallOutline
GameFontHighlightLarge
GameFontDisable
GameFontDisableSmall
GameFontDisableLarge
GameFontGreen
GameFontGreenSmall
GameFontGreenLarge
GameFontRed
GameFontRedSmall
GameFontRedLarge
GameFontWhite
GameFontDarkGraySmall
NumberFontNormalYellow
NumberFontNormalSmallGray
QuestFontNormalSmall
SystemFont
GameFontNormal
GameFontNormalSmall
GameFontNormalLarge
GameFontHighlight
GameFontHighlightSmall
GameFontHighlightSmallOutline
GameFontHighlightLarge
GameFontDisable
GameFontDisableSmall
GameFontDisableLarge
GameFontGreen
GameFontGreenSmall
GameFontGreenLarge
GameFontRed
GameFontRedSmall
GameFontRedLarge
GameFontWhite
GameFontDarkGraySmall
NumberFontNormalYellow
NumberFontNormalSmallGray
QuestFontNormalSmall
DialogButtonHighlightText
ErrorFont
TextStatusBarText
CombatLogFont
ErrorFont
TextStatusBarText
CombatLogFont
NumberFontNormalLarge
NumberFontNormalHuge
]]--

----------------------------------------------------------------------------

-- Creates a table `t` indexed by both sequentially numbered `keys` _and_ `values`,
-- thus supporting reverse lookup. Assumes numeric 'keys' and alphanumeric 'valuee'.
-- ## Example
-- ```lua
-- t = enum( "foo", "bar" );
-- print(t.hello) -- prints the integer 1
-- print(t[1]) -- prints the string "foo"
-- ```
---@param t table
---@return table
function enum(t)
for i = 1, #t do
local v = t[i]
--t[i] = nil
t[v] = i
end
return t
end

-- Creates a table `t` of self-indexed values
-- ## Example
-- ```lua
-- t = dict( "foo", "bar")
-- print(t.foo) -- prints the string "foo"
-- ```
---@param list table
---@return table
function dict(list)
for k, v in pairs(list) do
list[k] = nil
list[v] = v
end
return list
end

-- Returns a copy of `list` with `keys` and `values` inverted
-- ## Example
---```
---t = { "foo" = 1, "bar" = 2};
---s = tinvert(t);
---print(t.foo); -- prints the number 1
---print(s[1]); -- prints the string "foo";
---```
---@generic T
---@param list T[]
---@return T out
function table.invert(list)
local out = {}
for k, v in pairs(list) do
out[v] = k
end
return out
end

local Default, Nil = {}, function () end -- for uniqueness
---@param case any
---@return any
-- Implements a `switch` statement in Lua.
-- ## Example
-- ```
-- switch(case) = {
-- [1] = function() print("one") end,
-- [2] = print,
-- [3] = math.sin,
-- default = function() print("default") end,
-- }
-- ```
function switch (case)
return setmetatable({ case }, {
__call = function (t, cases)
local item = #t == 0 and Nil or t[1]
return (cases[item] or cases[Default] or Nil)
end
})
end

-- Prints debuggin information using a formatted version of its variable
-- number of arguments following the description given in its first argument.
---
---[View documents](command:extension.lua.doc?["en-us/51/manual.html/pdf-string.format"])
---@param s any
---@param ... any
function printf(s, ...)
print(" ",addonName,"::",string.format(s, ...))
end

-- Prints debug information to `stdout`. Receives any number of arguments,
-- converting each argument to a string following the same rules of
-- [tostring](command:extension.lua.doc?["en-us/51/manual.html/pdf-tostring"]).
---
--- [View documents](command:extension.lua.doc?["en-us/51/manual.html/pdf-print"])
---
function printd(...)
print(" ",addonName,"::",...)
end

--- Prints the value of any global variable, table value, frame, function result, or any valid Lua expression. Output is color coded for easier reading. Tables display up to 30 values, the rest are skipped and a message is shown.
---@param t any
---@param startkey? any
function dump(t, startkey)
DevTools_Dump(t, startkey)
end

0 comments on commit ebbee7f

Please sign in to comment.