Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
Update for v1.1
(tableIsEmpty: Initial implementation)
(Update for v1.1)
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 117: Line 117:
end
end
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 131: 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 143: 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 156: 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 162: Line 162:
return string.sub(string1, 1, string.len(start)) == start
return string.sub(string1, 1, string.len(start)) == start
end
end
 
--Adds commas
--Adds commas
function p.formatnum(number)
function p.formatnum(number)
Line 171: Line 171:
while true do
while true do
-- Format in blocks of 3 digits at a time until formatting is complete
-- Format in blocks of 3 digits at a time until formatting is complete
            local k
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
result, k = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then
if k == 0 then
Line 181: Line 182:


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 193: 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 201: 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 207: 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 217: 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 229: 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 274: 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 284: 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 397: Line 398:
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