Module:Addcommas: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m invocation support added
Sylphoid (talk | contribs)
Undo revision 13730 by Sur (talk) helper functions should not be invoked directly. update access point
 
Line 10: Line 10:
--
--
function p._add(arg, sep)
function p._add(arg, sep)
-- If called internally (direct Lua call), args will be passed directly.
    -- If called from wikitext, `arg` will be the `frame` object, and we get args from `frame.args`.
    -- Handle the case where it's called via #invoke (i.e., from wikitext)
    if type(arg) == "table" and arg.args then
        local frame = arg
        arg = frame.args[1]
        sep = frame.args[2]
    end
   
libraryUtil.checkTypeMulti('Module:Addcommas._add', 1, arg, {'number', 'string'})
libraryUtil.checkTypeMulti('Module:Addcommas._add', 1, arg, {'number', 'string'})
libraryUtil.checkType('"Module:Addcommas"._add', 2, sep, "string", true);
libraryUtil.checkType('"Module:Addcommas"._add', 2, sep, "string", true);
Line 57: Line 47:
function p._strip(str)
function p._strip(str)
libraryUtil.checkType('Module:Addcommas._strip', 1, str, 'string')
libraryUtil.checkType('Module:Addcommas._strip', 1, str, 'string')
    return str:gsub(',', '')
return str:gsub(',', '')
end
end


--[=[
 
function p.commas(frame)
function p.commas(frame)
local args = require("Module:Arguments").getArgs(frame);
--local args = require("Module:Arguments").getArgs(frame); -- Module:Arguments does not yet exist
    return p._add(args[1], args.sep);
local str = frame.args[1]
local delimiter = frame.args.sep
return p._add(str, delimiter);
end
end
--]=]
return p
return p

Latest revision as of 22:55, 4 November 2024

Documentation for this module may be created at Module:Addcommas/doc

--
-- @Attribution https://runescape.wiki/w/Module:Addcommas
--
local libraryUtil = require('libraryUtil')
local p = {}

--
-- main function
-- keep public so it can be used in other modules
--
function p._add(arg, sep)
	libraryUtil.checkTypeMulti('Module:Addcommas._add', 1, arg, {'number', 'string'})
	libraryUtil.checkType('"Module:Addcommas"._add', 2, sep, "string", true);
    local lang = mw.language.getContentLanguage()
    
    local x;
    if type(arg) == 'number' then
    	x = lang:formatNum(arg)
    else
	    local z = tostring(arg)
	    
	    -- strip any existing commas
	    z = z:gsub(',', '')
	    
	    local y = mw.text.split(z, '[%-–]')

	    -- handle ranges as separate numbers
	    -- required by [[Module:Exchange]] (p._table)
	    if y[1] ~= '' and y[2] then
	        y[1] = tonumber(y[1])
	        y[2] = tonumber(y[2])
	        x = lang:formatNum(y[1]) .. '–' .. lang:formatNum(y[2])
	    else
	        x = lang:formatNum(tonumber(z))
	    end
    end

	if (sep) then
		x = mw.ustring.gsub(x, ",", sep);
	end
	return x;
end

--
-- strips any existing commas in the input
--
function p._strip(str)
	libraryUtil.checkType('Module:Addcommas._strip', 1, str, 'string')
	return str:gsub(',', '')
end


function p.commas(frame)
	--local args = require("Module:Arguments").getArgs(frame); -- Module:Arguments does not yet exist
	local str = frame.args[1]
	local delimiter = frame.args.sep
	return p._add(str, delimiter);
end
return p