Module:Sandbox/Nux
Appearance
![]() | This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
Usage
[edit]Can be inv9oked without params in a template and will still work: {{#invoke:Sandbox/Nux|parseEnumParams}}
local p = {}
function p.parseEnumParams(frame)
local args = frame:getParent().args
local result = {}
local i = 1
while args["label" .. i] and args["value" .. i] and args["color" .. i] do
table.insert(result, {
label = mw.ustring.gsub(args["label" .. i] or "", "vv", "$v"),
value = tonumber(args["value" .. i]) or 0,
color = args["color" .. i] or ""
})
i = i + 1
end
local jsonString = p.tableToNiceJson(result)
return '<pre>' .. jsonString .. '</pre>'
end
-- table/array to line-by-line JSON
function p.tableToNiceJson(tbl)
local jsonString = "[\n"
for i, entry in ipairs(tbl) do
jsonString = jsonString .. mw.text.jsonEncode(entry) .. ",\n"
end
jsonString = jsonString .. "]"
mw.log("JSON Lines Output:\n" .. jsonString)
return jsonString
end
-- Debug/test function
function p.test_tableToNiceJson()
local testTable = {
{ label = "ciastka: $v", value = 2, color = "goldenrod" },
{ label = "słodycze: $v", value = 4, color = "darkred" },
{ label = "napoje: $v", value = 1, color = "lightblue" },
}
--mw.log("Test Table Input:", mw.text.jsonEncode(testTable, mw.text.JSON_PRETTY))
mw.logObject(testTable)
return p.tableToNiceJson(testTable)
end
return p