Skip to content

Commit

Permalink
Core: helpers: add table.count() for assoc arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Dec 4, 2024
1 parent dba80e6 commit 1419c77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ read_globals = {
"copy", "indexof", "insert_all", "key_value_swap",
-- our Core/helpers:
-- table:
"contains", "has_value", "has_key", "merge", "merge_values", "is_empty", "overwrite", "keys_of",
"contains", "has_value", "has_key", "merge", "merge_values", "is_empty", "overwrite", "keys_of", "count",
"keys", "except", "keys_has_one_of_values",
"equals", "multiply_each_value", "apply_function_to_every_value"
} },
Expand Down
10 changes: 10 additions & 0 deletions mods/lord/Core/helpers/src/lua_ext/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,13 @@ function table.apply_function_to_every_value(t, func)
end
return result
end

--- Use `#table` for numeric
function table.count(table)
local count = 0
for _, _ in pairs(table) do
count = count + 1
end

return count
end

0 comments on commit 1419c77

Please sign in to comment.