Module:Detect singular
Appearance
![]() | This Lua module is used on approximately 1,870,000 pages, or roughly 3% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
Lua to determine whether a string is singular or plural. Designed for use in infoboxes, to determine whether labels should be singular or plural based on the corresponding data, e.g.
{{#invoke:Detect singular|pluralize|Wynken, Blynken, and Nod||singular|plural}}
→ Script error: The function "pluralize" does not exist.
- See Template:Detect singular for main() usage. Test cases here.
- See Template:Pluralize from text for pluralize() usage (a wrapper around main()). Test cases here.
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')
local function plainFind(s, sub)
return mw.ustring.find(s, sub, 1, true)
end
local function plainCount(s, sub)
local _, count = mw.ustring.gsub(s, sub, '')
return count
end
function p._main(args)
local checkComma = not yesNo(args.nocomma,true)
local checkBullets = yesNo(args.bullets,true)
local s = args[1] -- the input string
if not s then
return nil -- empty input returns nil
end
s = tostring(s)
if plainFind(s,'forcedetectsingular') then -- magic data string to return true
return true
end
if plainFind(s,'forcedetectplural') then -- magic data string to return false
return false
end
local hasComma = checkComma and plainFind(s, ',')
local hasList = plainFind(s,'list')
local hasBreak = mw.ustring.find(s,'<%s*br')
local hasBullets = checkBullets and plainCount(s,'*') > 1
return not (hasComma or hasList or hasBreak or hasBullets)
end
function p.main(frame)
local args = getArgs(frame)
if p._main(args) then
return 1
end
return ""
end
return p