Module:String2/sandbox: Difference between revisions

Content deleted Content added
Remove upper and lower functions and make sentence and title strip-marker safe.
implement Template:trunc in Lua
Line 1:
local getArgs = require('Module:Arguments').getArgs
local p = {}
 
Line 289 ⟶ 290:
end
end
end
 
--[[
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:String2|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