diff --git a/libs/table.lua b/libs/table.lua index 1d45ce5..87b17d7 100644 --- a/libs/table.lua +++ b/libs/table.lua @@ -241,7 +241,7 @@ end ---at. ---@param tbl table ---@param value any ----@return any|nil +---@return any function ext_table.search(tbl, value) for k, v in pairs(tbl) do if v == value then @@ -313,29 +313,39 @@ function ext_table.shuffle(tbl) return tbl end ----Returns a random key, value index from an array. +---Returns a random (key, value) pair selected from the sequence portion of a table, or `nil` on an empty array. --- ---This function only works on the sequence portion of the table. Its behavior is undefined if the table has holes. ---@param tbl table ---@return any, any function ext_table.randomipair(tbl) + if #tbl == 0 then + return + end local i = math.random(#tbl) return i, tbl[i] end ----Returns a random key, value index from an array. +---Returns a random value selected from a table, or `nil` on an empty table. ---@param tbl table ---@return any, any function ext_table.randomvalue(tbl) local values = ext_table.values(tbl) + if #values == 0 then + return + end return values[math.random(#values)] end ----Returns a random key, value index from a table. +---Returns a random (key, value) pair selected from a table, or `nil` on an empty table. ---@param tbl table ---@return any, any function ext_table.randompair(tbl) - local rand = math.random(ext_table.count(tbl)) + local count = ext_table.count(tbl) + if count == 0 then + return + end + local rand = math.random(count) local n = 0 for k, v in pairs(tbl) do n = n + 1