Module:Sortkey: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Johnuniq Module:Arguments defaults to removing blank arguments but gives an error with string.find; for example List of rampage killers (Asia) uses {{hs|}} |
meta>TheDJ No edit summary |
||
Line 15: | Line 15: | ||
local args = getArgs(frame); | local args = getArgs(frame); | ||
return p._encode(args[1] or "") | return p._encode(args[1] or "") | ||
end | |||
function p.sortKeyForNumber(value) | |||
if not valid_number(value) then | |||
if value < 0 then | |||
sortkey = '1000000000000000000' | |||
else | |||
sortkey = '9000000000000000000' | |||
end | |||
elseif value == 0 then | |||
sortkey = '5000000000000000000' | |||
else | |||
local mag = floor(log10(abs(value)) + 1e-14) | |||
local prefix | |||
if value > 0 then | |||
prefix = 7000 + mag | |||
else | |||
prefix = 2999 - mag | |||
value = value + 10^(mag+1) | |||
end | |||
sortkey = format('%d', prefix) .. format('%015.0f', floor(value * 10^(14-mag))) | |||
end | |||
return sortkey; | |||
end | |||
function p.sortKeyForNumber(frame) | |||
local args = getArgs(frame); | |||
return p._sortKeyForNumber(args[1] or "") | |||
end | end | ||
return p | return p |
Revision as of 18:44, 18 September 2018
Documentation for this module may be created at Module:Sortkey/doc
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p._encode(sortkey)
-- Protect against sortkey nesting.
-- Example: {{sort|{{dts|2013|07|07}}|{{dts|1990|12|01}}}}
if string.find(sortkey, "sortkey") or string.find(sortkey, "data-sort-value") then
return "";
end
return mw.text.encode(sortkey)
end
function p.encode(frame)
local args = getArgs(frame);
return p._encode(args[1] or "")
end
function p.sortKeyForNumber(value)
if not valid_number(value) then
if value < 0 then
sortkey = '1000000000000000000'
else
sortkey = '9000000000000000000'
end
elseif value == 0 then
sortkey = '5000000000000000000'
else
local mag = floor(log10(abs(value)) + 1e-14)
local prefix
if value > 0 then
prefix = 7000 + mag
else
prefix = 2999 - mag
value = value + 10^(mag+1)
end
sortkey = format('%d', prefix) .. format('%015.0f', floor(value * 10^(14-mag)))
end
return sortkey;
end
function p.sortKeyForNumber(frame)
local args = getArgs(frame);
return p._sortKeyForNumber(args[1] or "")
end
return p