Module:Xswitch
| This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
This module is a pretty haphazard implementation of a discussed Lua request. (yes, you can do better than this) It takes an input number or string (the first numbered parameter) and sorts it back through the thresholds it is given until it finds one that it is greater than - then it spits out that result. The thresholds and results are given either as parameters or as a file. For example:
{{#invoke:xswitch|main
|profile={{some file}}
|type=string
|ant
|astronaut|astronaut
|Cherry|Cherry
|Apple|Apple
|cosmonaut|cosmonaut
|camper|camper
}}
will take the profile from "some file" that you've transcluded, ignoring everything after "ant". But if the profile were omitted, it would return "Apple" because that is the first thing working backwards that it is greater than. If you want smarter behavior than that, sort your parameters. :)
The file to accomplish the same tests would read
|astronaut|astronaut |Cherry|Cherry |Apple|Apple |cosmonaut|cosmonaut |camper|camper
The idea is/was that for a very long set of tests you might want to put them all in a file to edit, so you don't have to pass them all in the template each time and can edit them in one place.
Setting type to anything forces a string comparison - otherwise a numeric comparison is forced.
p = {}
function extractprofile(profile, type)
-- takes a profile like [[Templates:xswitch/test-profile]] and extracts the levels and results, _instead_ of having them passed as parameters
local x = 0
local pattern = {}
local mins = {}
local results = {}
for min,result in mw.ustring.gmatch(profile, "(.-)|(.-)\n") do
if min then
if type then
min = tostring(min)
else
min = tonumber(min)
end -- and I have nofklu WHAT this does to Unicode, sorry! Tostring is supposedly not necessary but I am suspicious.
table.insert(mins, min)
table.insert(results, (result or ""))
end
end
return mins,results
end
function p.main(frame)
local args = frame.args
local parent, pargs, type, profile, input, number
if frame.getparent then
parent = frame:getparent()
pargs = parent.args
type = pargs.type
input = pargs[1]
end -- I have nofklu why this gives me an error this time. Say who what?
type = type or args.type or nil -- sort as number or string. Default to number (nil), think.
profile = profile or args.profile or nil -- File to transclude to obtain the thresholds
input = input or args[1] or ""
if type then input = tostring(input) else input = tonumber(input) end
local mins = {}
local results = {}
if profile then
mins,results = extractprofile(profile, type)
number=table.maxn(mins)
end
if not(mins[1]) then -- (no profile or profile extraction unsuccessful)
mins={}; results={} -- suspicious mind...
number = 0
repeat
number = number + 1
local min
if parent then min = pargs[number * 2]; result = pargs[number * 2 + 1] else min = nil; result = nil end
min = min or args[number * 2] or nil -- numbered params from 2 on alternate as threshold and result
local result = result or args[number*2+1] or ""
if min then
if type then
min = tostring(min)
else
min = tonumber(min)
end
table.insert(mins, min)
table.insert(results, (result or ""))
end
until not(mins[number])
end
local toolow = results[number - 1] or ""
while number > 1 do
number = number-1
if input > mins[number] then
output = results[number];
break
end
end
return (output or toolow or "")
end
return p
