Module:Trunc

MyWikiBiz, Author Your Legacy — Sunday December 01, 2024
Revision as of 07:42, 16 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Documentation for this module may be created at Module:Trunc/doc

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