Difference between revisions of "Module:Common"

Jump to navigation Jump to search
684 bytes added ,  18:59, 7 February 2024
no edit summary
(Created page with "--[[ Additional functions that are common to other modules. As they cannot be outside of modules there is no invoke level function calls. ]] local function trim(object) -...")
 
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[
local a = require('Module:Arguments')
Additional functions that are common to other modules. As they cannot be
local com = {}
outside of modules there is no invoke level function calls.
]]


local function trim(object)
function com.trim(object)
--Use of while instead of if incase multiple whitespaces are present.
--Use of while instead of if incase multiple whitespaces are present.
while string.sub(object, 1, 1) == ' ' do  
if com.exists(object) then
object = string.sub(object, 2)
while string.sub(object, 1, 1) == ' ' do  
end
object = string.sub(object, 2)
end
while string.sub(object, string.len(object)) == ' ' do
while string.sub(object, string.len(object)) == ' ' do
object = string.sub(object, 1, string.len(object) - 1)
object = string.sub(object, 1, string.len(object) - 1)
end
end
end
Line 17: Line 17:
end
end


function exists(object)
function com.exists(object)
if object == nil or object == '' then
if object == nil or object == '' then
return false
return false
Line 24: Line 24:
end
end
end
end
function com.modulePagename(title)
if not title then
title = mw.title.getCurrentTitle().text
end
mw.log(title)
local disambig = string.find(title, '%(')
if disambig then
title = title:gsub('(.*)%s%(.-%)', '%1')
end
return title
end
function com.pagename(frame)
local args = a.getArgs(frame)
return com.trim(com.modulePagename(args['title']))
end
function com.fUpper(text)
local words = mw.text.split(text, '%s')
for k, v in ipairs(words) do
words[k] = v:sub(1,1):upper() .. (v:sub(2) or '')
end
return table.concat(words, ' ')
end
function com.firstUpper(frame)
local args = a.getArgs(frame)
return string.gsub(args[1], string.sub(args[1],1,1), string.upper(
string.sub(args[1],1,1)))
end
return com

Navigation menu