Skip to content

Commit

Permalink
Merge pull request #970 from Blizzard/table-fix-indexof
Browse files Browse the repository at this point in the history
Table fix indexof
  • Loading branch information
tvandijck committed Dec 12, 2017
2 parents 056e539 + 177c2f1 commit 08c768d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/base/rule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@

-- enum?
if prop.values then
local i = table.indexof(prop.values, value)
local i = table.findKeyByValue(prop.values, value)
if i ~= nil then
return tostring(i)
else
Expand Down Expand Up @@ -185,7 +185,7 @@

-- enum?
if prop.values then
local i = table.indexof(prop.values, value)
local i = table.findKeyByValue(prop.values, value)
if i ~= nil then
return prop.switch .. tostring(i)
else
Expand Down
15 changes: 14 additions & 1 deletion src/base/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,27 @@
--

function table.indexof(tbl, obj)
for k, v in pairs(tbl) do
for k, v in ipairs(tbl) do
if v == obj then
return k
end
end
end


--
-- Looks for an object within a table. Returns the key if found,
-- or nil if the object could not be found.
--

function table.findKeyByValue(tbl, obj)
for k, v in pairs(tbl) do
if v == obj then
return k
end
end
end


---
-- Insert a new value into a table in the position after the specified
Expand Down

0 comments on commit 08c768d

Please sign in to comment.