From b6d2b6645a083ec677506c8e1eeaf6e89d10ee44 Mon Sep 17 00:00:00 2001 From: Phoenix / Hotaru Date: Mon, 23 Oct 2023 11:35:41 +0100 Subject: [PATCH 1/2] feat(tableHelper): add getEntryKeyByValue --- lib/lua/tableHelper.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/lua/tableHelper.lua b/lib/lua/tableHelper.lua index 20dbae8f..2f18fa86 100644 --- a/lib/lua/tableHelper.lua +++ b/lib/lua/tableHelper.lua @@ -400,6 +400,21 @@ function tableHelper.isEmpty(inputTable) return false end +--- Retrieve a key associated with a specific value within a table. +--- +--- @param inputTable table The table to search within. +--- @param searchValue any The value to search for within the table. +--- @return any|false The key associated with the searchValue if found, or false if not found. +--- Note: This function is intended to handle situations where the loaded key may not align with the expected key due to the behavior of "cjson." +function tableHelper.getEntryKeyByValue(inputTable, searchValue) + for key, value in pairs(inputTable) do + if key == searchValue or tostring(key) == tostring(searchValue) then + return key + end + end + return false +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 From f2981c5c0f05ae9365bf48de94eec7c8a61ca34f Mon Sep 17 00:00:00 2001 From: Phoenix / Hotaru Date: Mon, 23 Oct 2023 11:41:42 +0100 Subject: [PATCH 2/2] Update lib/lua/tableHelper.lua Co-authored-by: Yvan Cywan --- lib/lua/tableHelper.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lua/tableHelper.lua b/lib/lua/tableHelper.lua index 2f18fa86..5e01b6c9 100644 --- a/lib/lua/tableHelper.lua +++ b/lib/lua/tableHelper.lua @@ -403,8 +403,8 @@ end --- Retrieve a key associated with a specific value within a table. --- --- @param inputTable table The table to search within. ---- @param searchValue any The value to search for within the table. ---- @return any|false The key associated with the searchValue if found, or false if not found. +--- @param searchValue string|number The value to search for within the table. +--- @return string|number|false The key associated with the searchValue if found, or false if not found. --- Note: This function is intended to handle situations where the loaded key may not align with the expected key due to the behavior of "cjson." function tableHelper.getEntryKeyByValue(inputTable, searchValue) for key, value in pairs(inputTable) do