From 83ea38bd065b34d47319c3b18887c0c3e79f7479 Mon Sep 17 00:00:00 2001 From: Bilal Bassam <38175840+Bilal2453@users.noreply.github.com> Date: Fri, 14 Jul 2023 02:00:20 +0300 Subject: [PATCH 1/4] standardize behavior on math.random(0) --- libs/table.lua | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libs/table.lua b/libs/table.lua index 1d45ce5..625928b 100644 --- a/libs/table.lua +++ b/libs/table.lua @@ -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 and the value associated with it from an array, 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 +---@return any|nil, any|nil 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 key and the value associated with it from an array, or `nil` on an empty array. ---@param tbl table ----@return any, any +---@return any|nil, any|nil 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 and the value associated with it from a table, or `nil` on an empty table. ---@param tbl table ----@return any, any +---@return any|nil, any|nil 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 From 5d39370ba76a97e3eb51c97b3b5eb91772435c5b Mon Sep 17 00:00:00 2001 From: Bilal Bassam <38175840+Bilal2453@users.noreply.github.com> Date: Fri, 14 Jul 2023 03:28:45 +0300 Subject: [PATCH 2/4] table: fixing description of randomvalue --- libs/table.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/table.lua b/libs/table.lua index 625928b..2ec2ffe 100644 --- a/libs/table.lua +++ b/libs/table.lua @@ -313,7 +313,7 @@ function ext_table.shuffle(tbl) return tbl end ----Returns a random key and the value associated with it from an array, or `nil` on an empty 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 @@ -326,7 +326,7 @@ function ext_table.randomipair(tbl) return i, tbl[i] end ----Returns a random key and the value associated with it from an array, or `nil` on an empty array. +---Returns a random value selected from a table, or `nil` on an empty table. ---@param tbl table ---@return any|nil, any|nil function ext_table.randomvalue(tbl) @@ -337,7 +337,7 @@ function ext_table.randomvalue(tbl) return values[math.random(#values)] end ----Returns a random key and the value associated with it from a table, or `nil` on an empty table. +---Returns a random (key, value) pair selected from a table, or `nil` on an empty table. ---@param tbl table ---@return any|nil, any|nil function ext_table.randompair(tbl) From 608400564b1ad60f9e5a7c834087e53e2d5de938 Mon Sep 17 00:00:00 2001 From: Bilal2453 Date: Sun, 17 Dec 2023 02:34:42 +0300 Subject: [PATCH 3/4] remove implied nil type --- libs/table.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/table.lua b/libs/table.lua index 2ec2ffe..20bada5 100644 --- a/libs/table.lua +++ b/libs/table.lua @@ -317,7 +317,7 @@ end --- ---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|nil, any|nil +---@return any, any function ext_table.randomipair(tbl) if #tbl == 0 then return From 01815dc7ca5d57f5552c80edffda018f2cb8522d Mon Sep 17 00:00:00 2001 From: Bilal Bassam Date: Sun, 17 Dec 2023 02:49:16 +0300 Subject: [PATCH 4/4] remove implied nil type --- libs/table.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/table.lua b/libs/table.lua index 20bada5..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 @@ -328,7 +328,7 @@ end ---Returns a random value selected from a table, or `nil` on an empty table. ---@param tbl table ----@return any|nil, any|nil +---@return any, any function ext_table.randomvalue(tbl) local values = ext_table.values(tbl) if #values == 0 then @@ -339,7 +339,7 @@ end ---Returns a random (key, value) pair selected from a table, or `nil` on an empty table. ---@param tbl table ----@return any|nil, any|nil +---@return any, any function ext_table.randompair(tbl) local count = ext_table.count(tbl) if count == 0 then