Skip to content

Commit

Permalink
updated enumeration utility functions
Browse files Browse the repository at this point in the history
Signed-off-by: speedwaystar <[email protected]>
  • Loading branch information
speedwaystar committed Jan 13, 2023
1 parent a6b941c commit e95be75
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions SmartBuff.globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,49 @@ 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
-- Returns an unumerated table.
---## Example
---```
---Enum.Animals = Enum.MakeEnum ( "Dog", "Cat", "Rabbit" )
---print( Enum.Animals.Cat ) -- prints "Cat"
---```
---@param ... ...
---@return table
function enum(t)
for i = 1, #t do
local v = t[i]
--t[i] = nil
t[v] = i
function Enum.MakeEnum(...)
return tInvert({...})
-- for i = 1, #t do
-- local v = t[i]
-- --t[i] = nil
-- t[v] = i
-- end
-- return t
end
return t

-- Returns an unumerated table from an existing table.
---## Example
---```
---Fish = { "Loach", "Pike", "Herring" }
---Enum.Fish = Enum.MakeEnumFromTable(Fish)
---print(Enum.Fish.Herring) -- prints "Herring"
---```
function Enum.MakeEnumFromTable(t)
return tInvert(t)
end

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

-- Returns a copy of `list` with `keys` and `values` inverted
Expand All @@ -96,12 +106,11 @@ end
---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)
---@param tbl table
---@return table out
function table.invert(tbl)
local out = {}
for k, v in pairs(list) do
for k, v in pairs(tbl) do
out[v] = k
end
return out
Expand Down

0 comments on commit e95be75

Please sign in to comment.