Module:Number table sorting/sandbox: Difference between revisions

Content deleted Content added
Created page with 'local lang = mw.language.getContentLanguage() -- constants -------------------------------------------------------------------------------- -- Nts class -------...'
 
sync
Line 1:
local lang = mw.language.getContentLanguage()
local Math = require('Module:Math')
local SortKey = require('Module:Sortkey')
-- constants
local INF = math.huge
local NEGINF = -math.huge
local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)
 
--------------------------------------------------------------------------------
Line 10 ⟶ 15:
 
Nts.formats = {
formatted = true,
no = true,
hideyes = true,
}
 
function Nts.new(args)
mw.logObject(args)
local self = setmetatable({}, Nts)
 
Line 22 ⟶ 25:
 
-- Set the format string
ifself.format = args.format thenor 'yes'
self.format = args.format
else
self.format = 'formatted'
end
if not Nts.formats[self.format] then
error(string.format(
Line 37 ⟶ 36:
-- {{nts|123456789.00123|prefix=approx. }} → approx. 123,456,789.00123
self.prefix = args.prefix or ''
-- debug info
self.debug = args.debug or 'no'
self.quiet = args.quiet or 'no'
 
return self
Line 44 ⟶ 47:
-- Parse the entered number
function Nts:parseNumber(s)
-- sanitize
s = s or '';
s = mw.ustringstring.gsub(s,'−','-')
s = mw.ustringstring.gsub(s, '−'MINUS, '-')
self.rawNumberString = s
-- fractions. was somewhat but completely broken at some point
self.isFraction = (string.find(s, '/') ~= nil)
if self.isFraction then
error(string.format(
"Fractions are not supported",
tostring(s)
), 0)
end
-- format detection
self.isScientificNotation = (string.find(s, 'e') ~= nil)
-- parse with language options
self.number = lang:parseFormattedNumber(s)
-- parse with fallback
if not self.number then
self.number = tonumber(s)
Line 58 ⟶ 78:
tostring(s)
), 0)
end
if self.number < 0 then
self.sign = MINUS
else
self.sign = ''
end
 
self.absNumber = math.abs(self.number)
if self.absNumber ~= INF then
self.magnitude = math.floor(math.log10(self.absNumber))
self.significand = self.number / 10^self.magnitude
self.precision = Math._precision(self.rawNumberString)
self.integer = math.floor(self.absNumber)
self.fractional = math.abs(self.number - self.integer)
end
end
 
function Nts:makeDisplay()
local ret ={}
if self.format == 'hide' then
 
if self.quiet == 'yes' then
return ''
end
 
if self.format == 'formatted' then
ret[#ret + 1] = self.prefix
return self.prefix .. lang:formatNum(self.number)
local sciNotation = string.find(tostring(self.number),'e')
if self.absNnumber == INF or isNaN(self.number) or self.magnitude ==nil or math.abs(self.magnitude) == INF then
ret[#ret + 1] = string.gsub(self.rawNumberString, '-', MINUS)
elseif sciNotation ~= nil or math.abs(self.magnitude) >= 9 then
ret[#ret + 1] = self.sign
if self.format == 'yes' then
ret[#ret + 1] = lang:formatNum(math.abs(self.number * 10^-self.magnitude))
else
ret[#ret + 1] = math.abs(self.number * 10^-self.magnitude)
end
ret[#ret + 1] = '<span style="margin-left:0.2em">×<span style="margin-left:0.1em">10</span></span><s style="display:none">^</s><sup>'
if self.magnitude<0 then
ret[#ret + 1] = MINUS .. (-self.magnitude)
else
ret[#ret + 1] = self.magnitude
end
ret[#ret + 1] = '</sup>'
else
ret[#ret + 1] = self.sign
if self.format == 'yes' then
ret[#ret + 1] = Math._precision_format(self.absNumber, self.precision)
else
local newPrecision = Math._precision(self.absNumber)
ret[#ret + 1] = tostring(self.absNumber)
if newPrecision < self.precision then
if self.integer == self.absNumber then
ret[#ret + 1] = '.'
end
ret[#ret + 1] = string.rep('0', math.min(12, self.precision - newPrecision) )
end
end
end
return table.concat(ret)
return self.prefix .. self.number
end
 
function Nts:makeSortKey()
ifreturn SortKey._sortKeyForNumber(self.number) == 0.. then'♠'
end
return '5000000000000000000'
 
elseif self.number == tonumber('inf') then
function ifNaNThen(n,p)
return '9000000000000000000'
elseifif self.number == tonumberisNaN('-inf'n) then
return '1000000000000000000'p
else
return ''
end
return n
end
 
function isNaN(n)
return n ~= n
end
 
Line 95 ⟶ 166:
local span = root:tag('span')
:attr('data-sort-value', self:makeSortKey())
 
if self.debug == 'yes' then
span:tag('span')
:css('border', '1px solid')
:wikitext(self:makeSortKey())
elseif self.quiet ~= 'no' then
span:css('display', 'none')
end
 
-- Display
if self.formatquiet ~== 'hideno' then
span:wikitext(self:makeDisplay())
else
-- tidy removes empty spans. Keep nbsp content till remexhtml is deployed
span:css('display', 'none')
:wikitext('&nbsp;')
end
 
Line 145 ⟶ 220:
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = { 'Template:Nts',Number 'Template:Ntshtable sorting' },
})
return p._main(args)