Regel 27: | Regel 27: | ||
function p.main( frame ) | function p.main( frame ) | ||
− | local str = mw.text.unstrip( frame.args[1] | + | local str = mw.text.unstrip( frame.args[1] ) |
− | local padlength = mw.text.unstrip( frame.args[2] or | + | local padlength = mw.text.unstrip( frame.args[2] ) or 5 |
+ | if str == nil | ||
+ | then res = "" | ||
+ | end | ||
local res = string.gsub( | local res = string.gsub( | ||
str, | str, | ||
"(%d+)", | "(%d+)", | ||
− | function(d) return p.padright( d, | + | function(d) return p.padright( d, padlength, '0' ) end |
) | ) | ||
return res | return res |
Versie van 10 aug 2023 12:21
--[[ Method for replacing numerical sequences in string with zero-padded equivalents ]]-- 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 --[[ Not currently used ]]-- 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 ) local str = mw.text.unstrip( frame.args[1] ) local padlength = mw.text.unstrip( frame.args[2] ) or 5 if str == nil then res = "" end local res = string.gsub( str, "(%d+)", function(d) return p.padright( d, padlength, '0' ) end ) return res end return p