模組:NumberUtil
外观
local p={}
local lib_arg={};
function p.foreachNumber(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_article = args[1] or args['1'] or ''
local input_calc = args[2] or args['2'] or '$1'
local pattern = '#expr:' .. mw.ustring.gsub(input_calc,'%%(%d+)','%%%%%1') .. '}}'
if mw.isSubsting() then pattern = 'safesubst:' .. pattern end
pattern = '{{' .. pattern
pattern = mw.ustring.gsub(pattern,'%$','(%%1)')
local result = mw.ustring.gsub(input_article,'(%-?[%d%.]+)',pattern)
return working_frame:preprocess( result )
end
function p.combineNumber(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {
parentFirst=true,
trim = false,
removeBlanks = false
})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_article = args[1] or args['1'] or ''
local spliter = {}
input_spliter = mw.ustring.gsub(input_article, '(%d)', '%1%1%1')
mw.ustring.gsub(input_spliter,'[%d%.]([^%d%.]+)[%d%.]',function(str)
spliter[str] =(spliter[str] or 0)+1
return str
end)
local max_count = 0
local spliter_str = args[2] or args['2'] or ','
for value,v_count in pairs(spliter) do
if v_count > max_count then
max_count = v_count
spliter_str = value
end
end
if (args[2] or args['2']) ~= nil and (args[2] or args['2']) ~= '' then spliter_str = args[2] or args['2'] end
if mw.text.trim(spliter_str or '') =='' then spliter_str = ' ' end
local counting_sort = {}
mw.ustring.gsub(input_article,'[%d%.]+',function(str)
local n_first, n_last = mw.ustring.find(str,'%d+')
if n_first then
local num = tonumber(mw.ustring.sub(str, n_first, n_last))
if num then counting_sort[num] = counting_sort[num] or str end
end
return str
end)
local quick_sort = {}
for value,v_count in pairs(counting_sort) do
quick_sort[#quick_sort + 1] = {value,v_count}
end
table.sort( quick_sort, function(left,fight) return left[1] < fight[1] end )
local write_number = ''
local output_arr = {}
local trmp_str = ''
for i=1,#quick_sort do
if type(write_number) ~= type(0) then
write_number = quick_sort[i][1]
trmp_str = '' .. quick_sort[i][2]
end
if quick_sort[i][1] + 1 ~= (quick_sort[i+1]or{quick_sort[i][1]-1})[1] then
if write_number ~= quick_sort[i][1] then
trmp_str = trmp_str .. "-"..quick_sort[i][2]
end
output_arr[#output_arr + 1] = trmp_str
write_number = ''
end
end
return table.concat( output_arr, spliter_str )
end
return p