aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/shared.lua
diff options
context:
space:
mode:
authorHirokazu Hata <h.hata.ai.t@gmail.com>2020-03-02 16:38:43 +0900
committerGitHub <noreply@github.com>2020-03-01 23:38:43 -0800
commite35ff7371f4a61621587744a7620200380abbbe9 (patch)
tree9ea54d157518273c8180ba218bb43bce9d5025cb /runtime/lua/vim/shared.lua
parente2e99b9f81c31458a97787ea66cb8befae7f0787 (diff)
downloadrneovim-e35ff7371f4a61621587744a7620200380abbbe9.tar.gz
rneovim-e35ff7371f4a61621587744a7620200380abbbe9.tar.bz2
rneovim-e35ff7371f4a61621587744a7620200380abbbe9.zip
lua: add vim.tbl_len() #11889
Diffstat (limited to 'runtime/lua/vim/shared.lua')
-rw-r--r--runtime/lua/vim/shared.lua18
1 files changed, 18 insertions, 0 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index 498992aa2e..1bf1c63fd7 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -356,6 +356,24 @@ function vim.tbl_islist(t)
end
end
+--- Counts the number of non-nil values in table `t`.
+---
+--- <pre>
+--- vim.tbl_count({ a=1, b=2 }) => 2
+--- vim.tbl_count({ 1, 2 }) => 2
+--- </pre>
+---
+--@see https://github.com/Tieske/Penlight/blob/master/lua/pl/tablex.lua
+--@param Table
+--@returns Number that is the number of the value in table
+function vim.tbl_count(t)
+ vim.validate{t={t,'t'}}
+
+ local count = 0
+ for _ in pairs(t) do count = count + 1 end
+ return count
+end
+
--- Trim whitespace (Lua pattern "%s") from both sides of a string.
---
--@see https://www.lua.org/pil/20.2.html