weac.table*: fix documentation, table.find

This commit is contained in:
Starbeamrainbowlabs 2024-09-17 22:51:04 +01:00
parent da97890e0c
commit 163f4fc0fd
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
10 changed files with 20 additions and 21 deletions

View file

@ -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

View file

@ -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

View file

@ -10,7 +10,7 @@
-- @param tbl table The table of values to filter.
-- @param func function<any, number>: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

View file

@ -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
return find

View file

@ -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

View file

@ -6,7 +6,7 @@
-- @param tbl table The table to operate on.
-- @param func function<any>: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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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