637 bytes added
, 07:42, 16 July 2021
local getArgs = require('Module:Arguments').getArgs
local p = {}
--[[
Truncate a string
Implements Template:Trunc, which is similar to string.sub, but when second
argument is missing or is not a number, return entire string
Usage:
{{#invoke:Trunc|trunc|abc123|3}}
Parameters:
args[1]: string to truncate
args[2]: number of characters to keep
]]
function p.trunc(frame)
local args = getArgs(frame,{parentFirst=true})
if not args[1] then
return ""
end
if not args[2] then
return args[1]
end
length = tonumber(args[2])
if not length then
return args[1]
end
return mw.ustring.sub(args[1],1,length)
end
return p