Module:IsValidMonthName

MyWikiBiz, Author Your Legacy — Friday November 15, 2024
Revision as of 23:24, 15 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Template:Module rating A simple module to check whether a supplied string is a valid name of a month in the Julian or Gregorian calendars

Operation

If the first parameter is the name of month, then it returns the parameter. In all other cases, it returns an empty string. (This return of a null string for a non-match allows the module to be used in templates simply by checking that the result is not an empty string.

It has three callable functions:

isFullMonthName
Checks whether the parameter is the full name of a month, e.g. July or October
isShortMonthName
Checks whether the parameter is the three-letter short name of a month, e.g. Jul or Oct
isMonthName
Checks whether the parameter is either:
a) the full name of a month, e.g. July or October
b) the three-letter short name of a month, e.g. Jul or Oct

By default, the checks are case-sensitive. However, with function, case-insensitivity can be specified by supplying a second parameter: Template:Param

Usage

For clarity of code, this module is best called through one of the three wrapper templates: {{Is valid full month name}}, {{Is valid short month name}}, and {{Is valid month name}}

isFullMonthName
  • {{#invoke:IsValidMonthName|isFullMonthName|mytext}}
  • {{#invoke:IsValidMonthName|isFullMonthName|mytext|ignorecase}}
isShortMonthName
  • {{#invoke:IsValidMonthName|isShortMonthName|mytext}}
  • {{#invoke:IsValidMonthName|isShortMonthName|mytext|ignorecase}}
isMonthName
  • {{#invoke:IsValidMonthName|isMonthName|mytext}}
  • {{#invoke:IsValidMonthName|isMonthName|mytext|ignorecase}}

See also



--[[
    A simple module to check whether a supplied string is a valid name of a month
    in the Julian or Gregorian calendars.
--]]

local IsValidMonthName = {}

local FullMonthNames = {
    ["January"] = true,
    ["February"] = true,
    ["March"] = true,
    ["April"] = true,
    ["May"] = true,
    ["June"] = true,
    ["July"] = true,
    ["August"] = true,
    ["September"] = true,
    ["October"] = true,
    ["November"] = true,
    ["December"] = true
}

local FullMonthNamesLowerCase = {
    ["january"] = true,
    ["february"] = true,
    ["march"] = true,
    ["april"] = true,
    ["may"] = true,
    ["june"] = true,
    ["july"] = true,
    ["august"] = true,
    ["september"] = true,
    ["october"] = true,
    ["november"] = true,
    ["december"] = true
}

local ShortMonthNames = {
    ["Jan"] = true,
    ["Feb"] = true,
    ["Mar"] = true,
    ["Apr"] = true,
    ["May"] = true,
    ["Jun"] = true,
    ["Jul"] = true,
    ["Aug"] = true,
    ["Sep"] = true,
    ["Oct"] = true,
    ["Nov"] = true,
    ["Dec"] = true
}

local ShortMonthNamesLowerCase = {
    ["jan"] = true,
    ["feb"] = true,
    ["mar"] = true,
    ["apr"] = true,
    ["may"] = true,
    ["jun"] = true,
    ["jul"] = true,
    ["aug"] = true,
    ["sep"] = true,
    ["oct"] = true,
    ["nov"] = true,
    ["dec"] = true
}



-- ############### Publicly accessible functions #######################

-- if the parameter is a valid FULL name of a month, return "%validmonthname%"
-- Otherwise just return an empty string
function IsValidMonthName.isFullMonthName(frame)
	return doNameCheck(frame.args[1], frame.args[2], true, false)
end


-- if the parameter is a valid SHORT name of a month, return "%validmonthname%"
-- Otherwise just return an empty string
function IsValidMonthName.isShortMonthName(frame)
	return doNameCheck(frame.args[1], frame.args[2], false, true)
end


-- if the parameter is a valid FULL OR SHORT name of a month, return "%validmonthname%"
-- Otherwise just return an empty string
function IsValidMonthName.isMonthName(frame)
	return doNameCheck(frame.args[1], frame.args[2], true, true)
end

-- ############### Private functions #######################


function doNameCheck(s, caseArg, checkFull, checkShort)
	local ignoreCase = false

	-- check for missing parameter
	if (s == nil) then
		return ""
	end
	
	-- check for empty parameter
	s = mw.text.trim(s)
	if (s == "") then
		return ""
	end
		
	if (caseArg ~= nil) then
		if (string.lower(caseArg) == "ignorecase") then
			ignoreCase = true
		end
	end
	
	if checkFull then
		if ((FullMonthNames[s] == true) and (not ignoreCase))
			or
		((FullMonthNamesLowerCase[string.lower(s)] == true) and (ignoreCase))
		then
			return s
		end
	end
	if checkShort then
		if ((ShortMonthNames[s] == true) and (not ignoreCase))
			or
		((ShortMonthNamesLowerCase[string.lower(s)] == true) and (ignoreCase))
		then
			return s
		end
	end
	
	return ""
end

return IsValidMonthName