From 163f4fc0fdd37f6018a2a03ea29b81d1534546c4 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 17 Sep 2024 22:51:04 +0100 Subject: [PATCH] weac.table*: fix documentation, table.find --- worldeditadditions_core/utils/table/table_apply.lua | 4 ++-- worldeditadditions_core/utils/table/table_contains.lua | 4 ++-- worldeditadditions_core/utils/table/table_filter.lua | 4 ++-- worldeditadditions_core/utils/table/table_find.lua | 7 ++----- worldeditadditions_core/utils/table/table_get_last.lua | 4 ++-- worldeditadditions_core/utils/table/table_map.lua | 4 ++-- worldeditadditions_core/utils/table/table_reduce.lua | 4 ++-- worldeditadditions_core/utils/table/table_tostring.lua | 4 ++-- worldeditadditions_core/utils/table/table_unique.lua | 4 ++-- worldeditadditions_core/utils/table/table_unpack.lua | 2 ++ 10 files changed, 20 insertions(+), 21 deletions(-) diff --git a/worldeditadditions_core/utils/table/table_apply.lua b/worldeditadditions_core/utils/table/table_apply.lua index 472cea9..97e3e9c 100644 --- a/worldeditadditions_core/utils/table/table_apply.lua +++ b/worldeditadditions_core/utils/table/table_apply.lua @@ -5,10 +5,10 @@ -- Warning: This function mutates `target`! -- @param source table The source to take values from -- @param target table The target to write values to -local function table_apply(source, target) +local function apply(source, target) for key, value in pairs(source) do target[key] = value end end -return table_apply +return apply diff --git a/worldeditadditions_core/utils/table/table_contains.lua b/worldeditadditions_core/utils/table/table_contains.lua index 71ff075..84805bc 100644 --- a/worldeditadditions_core/utils/table/table_contains.lua +++ b/worldeditadditions_core/utils/table/table_contains.lua @@ -5,11 +5,11 @@ -- @param tbl table The table to look in. -- @param target any The target to look for. -- @returns bool Whether the table contains the given target or not. -local function table_contains(tbl, target) +local function contains(tbl, target) for key, value in ipairs(tbl) do if value == target then return true end end return false end -return table_contains +return contains diff --git a/worldeditadditions_core/utils/table/table_filter.lua b/worldeditadditions_core/utils/table/table_filter.lua index 00f746e..b929c83 100644 --- a/worldeditadditions_core/utils/table/table_filter.lua +++ b/worldeditadditions_core/utils/table/table_filter.lua @@ -10,7 +10,7 @@ -- @param tbl table The table of values to filter. -- @param func function:bool The filter function to execute - should return a boolean value indicating whether the item provided as the first argument should be kept -- @returns table A new table containing the values that the given function returned true for. -local function table_filter(tbl, func) +local function filter(tbl, func) local result = {} for i,value in ipairs(tbl) do if func(value, i) then @@ -20,4 +20,4 @@ local function table_filter(tbl, func) return result end -return table_filter +return filter diff --git a/worldeditadditions_core/utils/table/table_find.lua b/worldeditadditions_core/utils/table/table_find.lua index 2298303..fc3b5b2 100644 --- a/worldeditadditions_core/utils/table/table_find.lua +++ b/worldeditadditions_core/utils/table/table_find.lua @@ -12,10 +12,7 @@ -- 2. `i` (number): The index in the table that the value can be found at -- 3. `tbl` (table): The original table. -- @return any|nil The first element in the table that satisfies the predicate, or nil if no such element is found. -function table_find(tbl, func) -end - -function table_find(tbl, func) +function find(tbl, func) for i,value in ipairs(tbl) do if func(value, i, tbl) then return value @@ -23,4 +20,4 @@ function table_find(tbl, func) end end -return table_find \ No newline at end of file +return find \ No newline at end of file diff --git a/worldeditadditions_core/utils/table/table_get_last.lua b/worldeditadditions_core/utils/table/table_get_last.lua index d265897..8de911b 100644 --- a/worldeditadditions_core/utils/table/table_get_last.lua +++ b/worldeditadditions_core/utils/table/table_get_last.lua @@ -9,11 +9,11 @@ local table_unpack = dofile(wea_c.modpath.."/utils/table/table_unpack.lua") -- @param tbl table The table to fetch items from. -- @param count number The number of items to fetch from the end of the table. -- @returns table A table containing the last count items from the given table. -local function table_get_last(tbl, count) +local function get_last(tbl, count) return {table_unpack( tbl, math.max(0, (#tbl) - (count - 1)) )} end -return table_get_last +return get_last diff --git a/worldeditadditions_core/utils/table/table_map.lua b/worldeditadditions_core/utils/table/table_map.lua index c49441b..d0dbae4 100644 --- a/worldeditadditions_core/utils/table/table_map.lua +++ b/worldeditadditions_core/utils/table/table_map.lua @@ -6,7 +6,7 @@ -- @param tbl table The table to operate on. -- @param func function:any|nil The function to execute on every item in the table. -- @returns table A new table containing the return values of the function. -local function table_map(tbl, func) +local function map(tbl, func) local result = {} for i,value in ipairs(tbl) do local newval = func(value, i) @@ -15,4 +15,4 @@ local function table_map(tbl, func) return result end -return table_map +return map diff --git a/worldeditadditions_core/utils/table/table_reduce.lua b/worldeditadditions_core/utils/table/table_reduce.lua index 6499e00..0d95399 100644 --- a/worldeditadditions_core/utils/table/table_reduce.lua +++ b/worldeditadditions_core/utils/table/table_reduce.lua @@ -5,7 +5,7 @@ -- @param tbl The table to iterate over. -- @param func The function to call for every element in tbl. Will be passed the following arguments: accumulator, value, index, table. Of course, the provided function need not take this many arguments. -- @param initial_value The initial value of the accumulator. -local function table_reduce(tbl, func, initial_value) +local function reduce(tbl, func, initial_value) local acc = initial_value for key, value in pairs(tbl) do acc = func(acc, value, key, tbl) @@ -13,4 +13,4 @@ local function table_reduce(tbl, func, initial_value) return acc end -return table_reduce +return reduce diff --git a/worldeditadditions_core/utils/table/table_tostring.lua b/worldeditadditions_core/utils/table/table_tostring.lua index 7ee0bf5..8b9df3a 100644 --- a/worldeditadditions_core/utils/table/table_tostring.lua +++ b/worldeditadditions_core/utils/table/table_tostring.lua @@ -7,7 +7,7 @@ -- @param new_line string key value pair delimiter -- @param max_depth number max recursion depth (optional) -- @return string concatenated table pairs -local function table_tostring(tbl, sep, new_line, max_depth) +local function tostring(tbl, sep, new_line, max_depth) if type(sep) ~= "string" then sep = ": " end if type(new_line) ~= "string" then new_line = ", " end if type(max_depth) == "number" then max_depth = {depth=0,max=max_depth} @@ -28,4 +28,4 @@ end -- Test: -- /lua v1 = { x= 0.335, facing= { axis= "z", sign= -1 } }; print(worldeditadditions.table.tostring(v1)) -return table_tostring +return tostring diff --git a/worldeditadditions_core/utils/table/table_unique.lua b/worldeditadditions_core/utils/table/table_unique.lua index 700f8c9..5e89a95 100644 --- a/worldeditadditions_core/utils/table/table_unique.lua +++ b/worldeditadditions_core/utils/table/table_unique.lua @@ -6,7 +6,7 @@ -- See also `worldeditadditions_core.Set`. -- @param tbl table The table of values to make unique. -- @returns table A new table containing the values of the given table appearing at most once. -local function table_unique(tbl) +local function unique(tbl) local newtbl = {} for i,value in ipairs(tbl) do local seen = false @@ -23,4 +23,4 @@ local function table_unique(tbl) return newtbl end -return table_unique +return unique diff --git a/worldeditadditions_core/utils/table/table_unpack.lua b/worldeditadditions_core/utils/table/table_unpack.lua index 3261b47..f296cd8 100644 --- a/worldeditadditions_core/utils/table/table_unpack.lua +++ b/worldeditadditions_core/utils/table/table_unpack.lua @@ -12,6 +12,8 @@ -- @param [offset=0] number The offset at which to start unpacking. -- @param [count=nil] number The number of items to unpack. Defaults to unpack all remaining items after `offset`. -- @returns any... The selected items unpacked from the table. +-- @example Lua version agnostic unpack +-- print(worldeditadditions_core.table.unpack({1 = "apple", 2 = "orange"})) local function table_unpack(tbl, offset, count) ---@diagnostic disable-next-line: deprecated if type(unpack) == "function" then