Module:Damage: Difference between revisions

From Elwiki
No edit summary
No edit summary
Line 4: Line 4:
     return string.sub(String,1,string.len(Start))==Start
     return string.sub(String,1,string.len(Start))==Start
end
end
function spairs(t, order)
 
    -- collect the keys
function spairs(t)
     local keys = {}
     local keys = {}
     for k in pairs(t) do keys[#keys+1] = k end
     for k in pairs(t) do keys[#keys+1] = k end
 
     table.sort(keys)
     -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys
    if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
    else
        table.sort(keys)
    end
 
    -- return the iterator function
     local i = 0
     local i = 0
     return function()
     return function()

Revision as of 21:05, 23 March 2022

Documentation for this module may be created at Module:Damage/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}
function string.starts(String,Start)
    return string.sub(String,1,string.len(Start))==Start
end

function spairs(t)
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end
    table.sort(keys)
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

function forargs(frame, prefix)
    local args = getArgs(frame)
    local tab = ''
    for k,v in spairs(args) do
        tab = tab .. ', ' .. k .. '=' .. v
    end
    -- for k,v in frame:argumentPairs() do
    --     if string.starts(k, prefix) then
    --         if i == 1 then
    --             tab = k .. '=' .. v
    --         else
    --             tab = tab .. ', ' .. k .. '=' .. v
    --         end
    --         i = i + 1
    --     end
    -- end
    return tab
end

function p.main(frame)
    local args = getArgs(frame)
    return forargs(frame, 'hits')
end

return p