Regel 20: Regel 20:
  
 
function p.main( frame )
 
function p.main( frame )
str = mw.text.unstrip(frame.args[1]) or mw.text.unstrip(frame.str)
+
str = mw.text.unstrip(frame.args[1]) or mw.text.unstrip(frame.args.str)
padlength = mw.text.unstrip(frame.args[2]) or mw.text.unstrip(frame.padlength)
+
padlength = mw.text.unstrip(frame.args[2]) or mw.text.unstrip(frame.args.padlength)
 
regpattern = "!\d+!"
 
regpattern = "!\d+!"
 
 

Versie van 10 aug 2023 10:05

Module:Pad numbers

Summary
Pad numbers in string.



local p = {}

p.padleft = function( str, len, char )
	if char == nil
		then char = '0'
		end
	char = '0'
	res = string.rep( char, len - #str ) .. str
	return res
end

p.padright = function( str, len, char )
	if char == nil
		then char = '0'
		end
	char = '0'
	res = str .. string.rep( char, len - #str )
	return res
end

function p.main( frame )
	str = mw.text.unstrip(frame.args[1]) or mw.text.unstrip(frame.args.str)
	padlength = mw.text.unstrip(frame.args[2]) or mw.text.unstrip(frame.args.padlength)
	regpattern = "!\d+!"
	
	matches = string.match( str, regpattern )
	if matches == nil
	then str = ""
	else
		for k, v in matches[0] do
	        padded = p.padright( v, 5, '0' )
	        str:gsub( v, padded )
		end
	end
	--
	return str
end

return p