Module:String2: Difference between revisions

fix
m (1 revision imported)
(fix)
Line 12: Line 12:
end
end


p.ucfirst = function (frame )
p.ucfirst = function (frame)
local s = frame.args[1];
local s = frame.args[1];
if not s or '' == s or s:match ('^%s+$') then -- when <s> is nil, empty, or only whitespace
if not s or '' == s or s:match ('^%s+$') then -- when <s> is nil, empty, or only whitespace
Line 87: Line 87:




p.title = function (frame )
p.title = function (frame)
-- http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html
-- http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html
-- recommended by The U.S. Government Printing Office Style Manual:
-- recommended by The U.S. Government Printing Office Style Manual:
Line 389: Line 389:
end
end
return boolean and 0 or ''
return boolean and 0 or ''
end
-- Checks if a value in a group of numbers is not an interger.
-- Allows usage of an |empty= parameter to allow empty values to be skipped.
function p.isInteger(frame)
local values = frame.args or frame:getParent().args
local allow_empty = frame.args.empty or frame:getParent().args.empty
for _, value in ipairs(values) do
-- Trim spaces
value = value and value:gsub("^%s*(.-)%s*$", "%1")
if value == "" or value == nil then
if not allow_empty then
return false  -- Empty values are not allowed
end
else
value = tonumber(value)
if not (type(value) == "number" and value == math.floor(value)) then
return false
end
end
end
return true
end
-- Returns an error found in a string.
function p.getError(frame)
local text = frame.args[1] or frame:getParent().args[1]
local error_message = text:match('(<strong class="error">.-</strong>)')
return error_message or nil
end
end


return p
return p
Anonymous user