Table: add table.key_value_list()
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
local tableCopy, tableKeyOf, pairs, ipairs, next, type, assert, ensureArgType, ensureArgNotNil
|
local tableCopy, tableKeyOf, pairs, ipairs, next, type, assert, ensureArgType, ensureArgNotNil, toString
|
||||||
= table.copy, table.keyof, pairs, ipairs, next, type, assert, Ensure.argType, Ensure.argNotNil
|
= table.copy, table.keyof, pairs, ipairs, next, type, assert, Ensure.argType, Ensure.argNotNil, tostring
|
||||||
|
|
||||||
-- While extending Lua's table library, we maintain snake_case naming for standard library
|
-- While extending Lua's table library, we maintain snake_case naming for standard library
|
||||||
-- functions and arguments to match standard library conventions. The code that is not exposed
|
-- functions and arguments to match standard library conventions. The code that is not exposed
|
||||||
@@ -359,4 +359,28 @@ function table.divide_values(t1, t2, empty_value)
|
|||||||
return a / b
|
return a / b
|
||||||
end
|
end
|
||||||
return operateValues(t1, t2, empty_value or 1, operation)
|
return operateValues(t1, t2, empty_value or 1, operation)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Returns a string of the format `'key1=value1,key2=value2...keyN=valueN'`.
|
||||||
|
--- @param t table
|
||||||
|
--- @param separator string? Default: `,`
|
||||||
|
--- @return string
|
||||||
|
function table.key_value_list(t, separator)
|
||||||
|
separator = separator or ','
|
||||||
|
|
||||||
|
local result = ''
|
||||||
|
|
||||||
|
local size = #t
|
||||||
|
local k = 1
|
||||||
|
for key, value in pairs(t) do
|
||||||
|
if size < k then
|
||||||
|
result = result..key..'='..toString(value)..separator
|
||||||
|
else
|
||||||
|
result = result..key..'='..toString(value)
|
||||||
|
end
|
||||||
|
|
||||||
|
k = k + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user