Module:Item

local p = {}

local items = mw.loadData('Module:Item/data') local UNKNOWN_ITEM = "Some Unknown item"

local function isempty(s) return s == nil or s == '' end

local function trim(s) local r, _ = s:gsub("^%s*(.-)%s*$", "%1") return r end

local function paramstring(s) if s == nil then return "" end return trim(tostring(s)) end

local function lookup(frame) return items[p.name(frame)] end

local function get_attr(frame, key, fallback) local e = lookup(frame) if e == nil then return fallback end return tostring(e[key]) end

function p.name(frame) local s = paramstring(frame.args[1]) if not isempty(s) then return s   end local pf = mw.getCurrentFrame:getParent if not isempty(pf.name) then return pf.name end return mw.title.getCurrentTitle.text end

function p.sell_value(frame) return get_attr(frame, "coin", "") end function p.desc(frame) return get_attr(frame, "desc", "") end

function p.sortable_table local keys = {} for k in pairs(items) do       table.insert(keys, k)    end table.sort(keys)

local s = {} table.insert(s, '{| class="wikitable sortable"') table.insert(s, '! class="unsortable"|Icon !! Name !! Description') for _, key in ipairs(keys) do       local c = items[key] table.insert(s, '|-') local icon = string.format(' ', key, key) table.insert(s, string.format('| %s || %s || %s', icon, key, c.name, c.desc)) end table.insert(s, '|}') return table.concat(s, "\n") end

function p._test_sortable_table if string.len(p.sortable_table) <= 500 then mw.log("sortable table is too short") end end

-- Run this before saving function p._test local testcases = { {name="Un Coin", sell_value="1", desc="Used to make purchases at the Un Store."}, {name=UNKNOWN_ITEM, sell_value="", desc=""}, }   for _, tc in ipairs(testcases) do        for key, val in pairs(tc) do            local x = p[key]{args={tc.name}} if x ~= val then mw.log(string.format("%s %s is %s; expected %s", tc.name, key, x, val)) end end end p._test_sortable_table end

return p