(17 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
 +
--[[
 +
Method for replacing numerical sequences in string with zero-padded equivalents
 +
]]--
 +
 
local p = {}
 
local p = {}
  
p.padleft = function( str, len, char )
+
p.padleft = function( str, length, char )
 
if char == nil
 
if char == nil
 
then char = '0'
 
then char = '0'
 
end
 
end
 
char = '0'
 
char = '0'
res = string.rep( char, len - #str ) .. str
+
res = string.rep( char, length - #str ) .. str
 
return res
 
return res
 
end
 
end
  
 +
--[[
 +
Not currently used
 +
]]--
 
p.padright = function( str, len, char )
 
p.padright = function( str, len, char )
 
if char == nil
 
if char == nil
Regel 16: Regel 23:
 
char = '0'
 
char = '0'
 
res = str .. string.rep( char, len - #str )
 
res = str .. string.rep( char, len - #str )
 +
return res
 +
end
 +
 +
p.replaceNumbers = function( str, padlength )
 +
if not str or str == "" then return "" end
 +
local res = string.gsub(
 +
str,
 +
"(%d+)",
 +
function(d) return p.padleft( d, padlength, '0' ) end
 +
)
 
return res
 
return res
 
end
 
end
  
 
function p.main( frame )
 
function p.main( frame )
str = mw.text.unstrip(frame.args[1])
+
if frame.args[1] == nil then
padlength = mw.text.unstrip(frame.args[2])
+
return ""
regpattern = "!\d+!"
+
end
 +
str = mw.text.unstrip( frame.args[1] )
 +
if frame.args[2] == nil then
 +
padlength = 5
 +
else
 +
padlength = mw.text.unstrip( frame.args[2] )
 +
end
 
 
matches = string.match( str, regpattern )
+
local res = p.replaceNumbers( str, padlength )
for k, v in matches do
+
return res
        padded = p.padright( v, 5, '0' )
 
        str:gsub( v, padded )
 
end
 
--
 
return str
 
 
end
 
end
  
 
return p
 
return p

Huidige versie van 10 aug 2023 om 14:38

Module:Pad numbers

Summary
Pad numbers in string. The numbers are 0000001 and 0005343



--[[
Method for replacing numerical sequences in string with zero-padded equivalents
]]--

local p = {}

p.padleft = function( str, length, char )
	if char == nil
		then char = '0'
		end
	char = '0'
	res = string.rep( char, length - #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

p.replaceNumbers = function( str, padlength )
	if not str or str == "" then return "" end
	local res = string.gsub(
		str,
		"(%d+)",
		function(d) return p.padleft( d, padlength, '0' ) end
	)
	return res
end

function p.main( frame )
	if frame.args[1] == nil then 
		return ""
	end
	str = mw.text.unstrip( frame.args[1] )
	if frame.args[2] == nil then
		padlength = 5
	else
		padlength = mw.text.unstrip( frame.args[2] )
	end
	
	local res = p.replaceNumbers( str, padlength )
	return res
end

return p