Module:DidYouKnow

From Cyraxx Wiki - lolcow.city
Jump to navigation Jump to search

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

--made by Lew Easy 

local p = {}

function p.shuffle(tbl)
    local size = #tbl
    for i = size, 2, -1 do
        local rand = math.random(i)
        tbl[i], tbl[rand] = tbl[rand], tbl[i]
    end
    return tbl
end

function p.getDidYouKnow(frame)
    math.randomseed(os.time())

    local facts = mw.text.split(frame.args.facts, "\n")

    local nonEmptyFacts = {}
    for _, fact in ipairs(facts) do
        if fact ~= "" then
            table.insert(nonEmptyFacts, fact)
        end
    end

    local count = tonumber(frame.args.count) or 2

    p.shuffle(nonEmptyFacts)

    local selectedFacts = {}
    for i = 1, count do
        if nonEmptyFacts[i] then
            table.insert(selectedFacts, "* " .. nonEmptyFacts[i])
        end
    end

    return table.concat(selectedFacts, "\n")
end

return p