Module:DidYouKnow

From Foodie Beauty Wiki - lolcow.city
Revision as of 19:51, 13 July 2024 by Aviv (talk | contribs) (Created page with "--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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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