Module:ISO 3166/sandbox: Difference between revisions

Content deleted Content added
Fall back on not splitting by hyphen when getting code from name
Some optimizations for luacode(): Made Hyphenation fallback lazy with more common case first; only call getArgs() once
Line 100:
 
function p.luacode(args, skipHyphenSplit)
local country, subdivision = country, subdivision
if not skipHyphenSplit then
if string.find(args[1]country or '',"%-") then
args[1]country, args[2]subdivision = string.match(args[1]country or '',"^([^%-]*)%-(.*)$")
end
end
Line 108 ⟶ 110:
--error(mw.dumpObject(args))
if args[1]country then args[1]country = p.strip(args[1]country) end
if args[2]subdivision then args[2]subdivision = p.strip(args[2]subdivision) end
--error(mw.dumpObject(args))
Line 124 ⟶ 126:
or '[[Category:Wikipedia page with obscure subdivision]]'
if not args[1]country then
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return catnocountry, '<span style="font-size:100%" class="error">"No parameter for the country given"</span>'
end
if not args[2]subdivision then --3166-1 code
for alpha2,cdata in pairs(data) do
if findname(alpha2,cdata,args[1]country) then
if args["codetype"]=="numeric" or args["codetype"]=="alpha3" then
return cdata[args["codetype"]]
Line 143 ⟶ 145:
else --3166-2 code
for alpha2,cdata in pairs(data) do
if findname(alpha2,cdata,args[1]country) then
if mw.ustring.match(alpha2,"GB") then -- For England, Wales etc.
alpha2 = "GB"
Line 152 ⟶ 154:
if type(scdata)=="table" then
empty = false
if findname(scode,scdata,args[2]subdivision) then
return alpha2.."-"..scode
end
Line 164 ⟶ 166:
return catnocountry
end
 
end
 
Line 170 ⟶ 172:
 
function p.code(frame)
local args = getArgs(frame)
-- Prioritize splitting on hyphen (subdivision).
--lucode() mutates its arguments, so we must call getArgs() twice.
local unhyphenateResult = p.luacode(getArgs(frame)args)
keepHyphenResult = p.luacode(getArgs(frame), true)
--return unhyphenateResult .. '_' .. keepHyphenResult
--Prioritize single country name.
if keepHyphenResult ~= nil and keepHyphenResult ~= '' then
return keepHyphenResult
end
if unhyphenateResult ~= nil and unhyphenateResult ~= '' then
return unhyphenateResult
end
-- If there is a hyphen, try again by not splitting.
if string.find(args[1] or '',"%-") then
local keepHyphenResult = p.luacode(getArgs(frame)args, true)
return keepHyphenResult or ''
end
return ''