Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
no edit summary
m (Formatting)
No edit summary
(6 intermediate revisions by 2 users not shown)
Line 13: Line 13:


local p = {}
local p = {}
 
-- iterator sorted by keys
-- iterator sorted by keys
-- For example, if you had a table that looked something like
-- For example, if you had a table that looked something like
Line 32: Line 32:
table.sort(keys)
table.sort(keys)
end
end
 
local i = 0
local i = 0
local iterator = function()
local iterator = function()
Line 71: Line 71:
end
end
end
end
 
-- conveniently shifts BLAH to Blah
-- conveniently shifts BLAH to Blah
-- Handy when formatting data in ALL CAPS or all lower case
-- Handy when formatting data in ALL CAPS or all lower case
Line 84: Line 84:
end
end
end
end
 
-- Returns the number of rows in a table
-- Returns the number of rows in a table
-- Originally snagged this from Module:VoidByReward written by User:NoBrainz
-- Originally snagged this from Module:VoidByReward written by User:NoBrainz
Line 105: Line 105:
end
end
end
end
 
-- Returns true if the table is empty, false otherwise
function p.tableIsEmpty(table)
if type(table) == 'table' then
for k, v in pairs(table) do
return false
end
return true
else
return nil
end
end
 
-- Returns the number of indexed elements in a table
-- Returns the number of indexed elements in a table
-- pre : table is a table with no explicit nil values
-- pre : table is a table with no explicit nil values
Line 119: Line 131:
end
end
end
end
 
--Sorts theTable based on the listed column
--Sorts theTable based on the listed column
function p.tableSort(theTable, sortCol, ascend)
function p.tableSort(theTable, sortCol, ascend)
local new  function sorter(r1, r2)
local sorter = function(r1, r2)
if ascend then
if ascend then
return r1[sortCol] < r2[sortCol]
return r1[sortCol] < r2[sortCol]
Line 131: Line 143:
table.sort(theTable, sorter)
table.sort(theTable, sorter)
end
end
 
--Splits a string based on a sent in separating character
--Splits a string based on a sent in separating character
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"}
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"}
Line 144: Line 156:
return t
return t
end
end
 
--Returns 'true' if a string starts with something
--Returns 'true' if a string starts with something
--For example calling startsWith ("Lith V1 Relic", "Lith") would return true
--For example calling startsWith ("Lith V1 Relic", "Lith") would return true
Line 150: Line 162:
return string.sub(string1, 1, string.len(start)) == start
return string.sub(string1, 1, string.len(start)) == start
end
end
 
--Stolen from Stack Overflow
--Adds commas
--Adds commas
function p.formatnum(number)
function p.formatnum(number)
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
if tonumber(number) == nil then
return number
-- reverse the int-string and append a comma to all blocks of 3 digits
else
int = int:reverse():gsub("(%d%d%d)", "%1,")
local result = number
while true do
-- reverse the int-string back remove an optional comma and put the
-- Format in blocks of 3 digits at a time until formatting is complete
-- optional minus and fractional part back
            local k
return minus .. int:reverse():gsub("^,", "") .. fraction
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then
break
end
end
return result
end
end
end


function p.formatNumber(frame)
function p.formatNumber(frame)
number = frame.args ~= nil and frame.args[1] or frame
local number = frame.args ~= nil and frame.args[1] or frame
return p.formatnum(number)
return p.formatnum(number)
end
end
Line 177: Line 194:
maxDigits = maxDigits[1]
maxDigits = maxDigits[1]
end
end
 
local result = val..""
local result = val..""
local decimals = string.find(result, "%.")
local decimals = string.find(result, "%.")
Line 185: Line 202:
decimals = 0
decimals = 0
end
end
 
if maxDigits ~= nil and decimals > maxDigits then
if maxDigits ~= nil and decimals > maxDigits then
result = string.format("%."..maxDigits.."f", result)
result = string.format("%."..maxDigits.."f", result)
Line 191: Line 208:
result = string.format("%."..minDigits.."f", result)
result = string.format("%."..minDigits.."f", result)
end
end
 
return result
return result
end
end
end
end
 
--From http://lua-users.org/wiki/SimpleRound
--From http://lua-users.org/wiki/SimpleRound
function p.round2(num, numDecimalPlaces)
function p.round2(num, numDecimalPlaces)
Line 201: Line 218:
return math.floor(num * mult + 0.5) / mult
return math.floor(num * mult + 0.5) / mult
end
end
 
-- pre : List is a table or a string
-- pre : List is a table or a string
--      Item is the element that is being searched
--      Item is the element that is being searched
Line 213: Line 230:
IgnoreCase = false  
IgnoreCase = false  
end
end
 
if type(List) == "table" then
if type(List) == "table" then
for key, value in pairs(List) do
for key, value in pairs(List) do
Line 258: Line 275:
length = p.tableCount(table)
length = p.tableCount(table)
end
end
 
-- iterating through outer table
-- iterating through outer table
for i = 1, length, 1 do
for i = 1, length, 1 do
Line 268: Line 285:
return false
return false
end
end
 
-- copies the contents of a variable; handy for when you might want to modify an object taken from a data file
-- copies the contents of a variable; handy for when you might want to modify an object taken from a data file
-- or any other read-only variable
-- or any other read-only variable
Line 292: Line 309:
local gcd = p.gcd(n, d)
local gcd = p.gcd(n, d)
return p.formatnum(n/gcd)..'/'..p.formatnum(d/gcd)
return p.formatnum(n/gcd)..'/'..p.formatnum(d/gcd)
end
--Similar to p.fraction but returns the simplified numerator and denomerator separately without formatting
function p.fractionpair(n, d)
local gcd = p.gcd(n, d)
return n / gcd, d / gcd
end
end


Line 350: Line 373:
result = string.gsub(result, "%%27", "'")
result = string.gsub(result, "%%27", "'")
result = string.gsub(result, "&#39;", "'")
result = string.gsub(result, "&#39;", "'")
result = string.gsub(result, "&#38;", "&")
return result
return result
end
end
Line 375: Line 399:
return p.formatnum(number)
return p.formatnum(number)
end
end
end
-- Takes a description template & template data, returning a description with variables populated
function p.applyTemplateData(descTemplate, templateData)
    local resultDesc = descTemplate
    for k, v in pairs(templateData) do
        local val = v
        if type(v) == 'number' then
            val = p.formatnum(val)
        end
        resultDesc = string.gsub(resultDesc, '${' .. k .. '}', val)
    end
    return resultDesc
end
end


return p
return p