Skip to content

Commit

Permalink
feat(tableHelper): add getEntryInTableByValue
Browse files Browse the repository at this point in the history
  • Loading branch information
HotaruBlaze committed Oct 22, 2023
1 parent d930680 commit e879d59
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/lua/tableHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,18 @@ function tableHelper.isEmpty(inputTable)
return false
end

-- Function to get a table entry by its associated value
-- Note: This function is designed to handle cases where the loaded value may not be in the expected format due to "cjson" behavior.
-- Specifically, "cjson" may load a saved string containing only numbers as a number instead of a string.
function tableHelper.getEntryInTableByValue(inputTable, searchValue)
for key, value in pairs(inputTable) do
if key == searchValue or tostring(key) == tostring(searchValue) then
return value -- Return the table entry with all associated data
end
end
return nil
end

-- Check whether the table is an array with only consecutive numerical keys,
-- i.e. without any gaps between keys
-- Based on http://stackoverflow.com/a/6080274
Expand Down

0 comments on commit e879d59

Please sign in to comment.