Modul:Arguments: Unterschied zwischen den Versionen
Die Seite wurde neu angelegt: „local M = {} function set_deep(deep_key, obj, value) local key_end = mw.ustring.find(deep_key, ".", 0, true) if key_end == nil then obj[deep_key] = value return end local head = mw.ustring.sub(deep_key, 1, key_end - 1) local tail = mw.ustring.sub(deep_key, key_end + 1) if obj[head] == nil then obj[head] = {} end if type(obj[head]) ~= "table" then return end set_deep(tail, obj[head], value) end function M.parse_deep(args) local deep_args =…“ |
KKeine Bearbeitungszusammenfassung |
||
| (Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
local M = {} | local M = {} | ||
function parse_key(key) | |||
local as_number = tonumber(key) | |||
if as_number ~= nil then | |||
return as_number | |||
end | |||
return key | |||
end | |||
function set_deep(deep_key, obj, value) | function set_deep(deep_key, obj, value) | ||
local key_end = mw.ustring.find(deep_key, ".", 0, true) | local key_end = mw.ustring.find(deep_key, ".", 0, true) | ||
if key_end == nil then | if key_end == nil then | ||
obj[deep_key] = value | obj[parse_key(deep_key)] = value | ||
return | return | ||
end | end | ||
local head = mw.ustring.sub(deep_key, 1, key_end - 1) | local head = parse_key(mw.ustring.sub(deep_key, 1, key_end - 1)) | ||
local tail = mw.ustring.sub(deep_key, key_end + 1) | local tail = mw.ustring.sub(deep_key, key_end + 1) | ||
if obj[head] == nil then obj[head] = {} end | if obj[head] == nil then obj[head] = {} end | ||
Aktuelle Version vom 17. September 2024, 21:38 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Arguments/Doku erstellt werden
local M = {}
function parse_key(key)
local as_number = tonumber(key)
if as_number ~= nil then
return as_number
end
return key
end
function set_deep(deep_key, obj, value)
local key_end = mw.ustring.find(deep_key, ".", 0, true)
if key_end == nil then
obj[parse_key(deep_key)] = value
return
end
local head = parse_key(mw.ustring.sub(deep_key, 1, key_end - 1))
local tail = mw.ustring.sub(deep_key, key_end + 1)
if obj[head] == nil then obj[head] = {} end
if type(obj[head]) ~= "table" then return end
set_deep(tail, obj[head], value)
end
function M.parse_deep(args)
local deep_args = {}
for deep_key, value in pairs(args) do
set_deep(deep_key, deep_args, value)
end
return deep_args
end
return M