Moodul:Bananas: erinevus redaktsioonide vahel

Allikas: Vikipeedia
Eemaldatud sisu Lisatud sisu
Resümee puudub
Resümee puudub
 
9. rida: 9. rida:


function p.rep( frame )
function p.rep( frame )
local new_args = p._getParameters( frame.args, { 's', 'i' } );
local new_args = p._getParameters( frame.args, { 's', 'n' } );
local s = new_args['s'] or '';
local s = new_args['s'] or '';
local i = tonumber( new_args['i'] ) or 1;
local n = tonumber( new_args['n'] ) or 1;
return string.rep(s, n)
return string.rep(s, n)
end
end

Viimane redaktsioon: 7. aprill 2013, kell 21:08

Selle mooduli dokumentatsiooni saab kirjutada asukohta Moodul:Bananas/doc.

-- For unit tests, see [[Module:Bananas/tests]]
-- Test moodul
local p = {}
 
function p.hello()
    return "Hello, world!"
end
 

function p.rep( frame )
    local new_args = p._getParameters( frame.args, { 's', 'n' } );
    local s = new_args['s'] or '';
    local n = tonumber( new_args['n'] ) or 1;
    return string.rep(s, n)
end

--[[
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters.  This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
function p._getParameters( frame_args, arg_list )
    local new_args = {};
    local index = 1;
    local value;
 
    for i,arg in ipairs( arg_list ) do
        value = frame_args[arg]
        if value == nil then
            value = frame_args[index];
            index = index + 1;
        end
        new_args[arg] = value;
    end
 
    return new_args;
end 

return p