Module:Shared: Difference between revisions

1,345 bytes removed ,  4 January 2022
Remove joinList: Exists within Scribunto provided libraries as mw.text.listToText; Indent with tabs instead of spaces
(round: Amend such that result remains with maxDigits dp when decimals > maxDigits)
(Remove joinList: Exists within Scribunto provided libraries as mw.text.listToText; Indent with tabs instead of spaces)
Line 25: Line 25:
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
function p.skpairs(t, revert)
function p.skpairs(t, revert)
    local keys = {}
local keys = {}
    for k in pairs(t) do keys[#keys + 1] = k end
for k in pairs(t) do keys[#keys + 1] = k end
    if revert ~= nil then
if revert ~= nil then
        table.sort(keys, function(a, b) return a > b end)
table.sort(keys, function(a, b) return a > b end)
    else
else
        table.sort(keys)
table.sort(keys)
    end
end
   
   
    local i = 0
local i = 0
    local iterator = function()
local iterator = function()
        i = i + 1
i = i + 1
        local key = keys[i]
local key = keys[i]
        if key then
if key then
            return key, t[key]
return key, t[key]
        else
else
            return nil
return nil
        end
end
    end
end
    return iterator
return iterator
end
end


Line 50: Line 50:
--Taken from https://stackoverflow.com/questions/15706270/sort-a-table-in-lua
--Taken from https://stackoverflow.com/questions/15706270/sort-a-table-in-lua
function p.spairs(t, order)
function p.spairs(t, order)
    -- collect the keys
-- collect the keys
    local keys = {}
local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end
for k in pairs(t) do keys[#keys+1] = k end


    -- if order function given, sort by it by passing the table and keys a, b,
-- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys  
-- otherwise just sort the keys  
    if order then
if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
table.sort(keys, function(a,b) return order(t, a, b) end)
    else
else
        table.sort(keys)
table.sort(keys)
    end
end


    -- return the iterator function
-- return the iterator function
    local i = 0
local i = 0
    return function()
return function()
        i = i + 1
i = i + 1
        if keys[i] then
if keys[i] then
            return keys[i], t[keys[i]]
return keys[i], t[keys[i]]
        end
end
    end
end
end
end
   
   
Line 76: Line 76:
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
function p.titleCase(head, tail)
function p.titleCase(head, tail)
    if tail == nil then
if tail == nil then
        --Split into two lines because don't want the other return from gsub
--Split into two lines because don't want the other return from gsub
        local result = string.gsub(head, "(%a)([%w_']*)", p.titleCase)
local result = string.gsub(head, "(%a)([%w_']*)", p.titleCase)
        return result
return result
    else
else
        return string.upper(head) .. string.lower(tail)
return string.upper(head) .. string.lower(tail)
    end
end
end
end
   
   
Line 97: Line 97:
--      if table is not of type 'table' then return nil
--      if table is not of type 'table' then return nil
function p.tableCount(table)
function p.tableCount(table)
    if (type(table) == 'table') then
if (type(table) == 'table') then
        local count = 0
local count = 0
        for _ in pairs(table) do count = count + 1 end
for _ in pairs(table) do count = count + 1 end
        return count
return count
    else
else
        return nil
return nil
    end
end
end
end
   
   
Line 111: Line 111:
--      if table is not of type 'table' then return nil
--      if table is not of type 'table' then return nil
function p.indexCount(table)
function p.indexCount(table)
    if (type(table) == 'table') then
if (type(table) == 'table') then
        local count = 0
local count = 0
        for _ in ipairs(table) do count = count + 1 end
for _ in ipairs(table) do count = count + 1 end
        return count
return count
    else
else
        return nil
return nil
    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 new  function sorter(r1, r2)
                    if(ascend) then
if(ascend) then
                        return r1[sortCol] < r2[sortCol]
return r1[sortCol] < r2[sortCol]
                    else
else
                        return r1[sortCol] > r2[sortCol]
return r1[sortCol] > r2[sortCol]
                    end
end
                end
end
    table.sort(theTable, sorter)
table.sort(theTable, sorter)
end
end
   
   
Line 135: Line 135:
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"}
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"}
function p.splitString(inputstr, sep)
function p.splitString(inputstr, sep)
        if sep == nil then
if sep == nil then
                sep = "%s"
sep = "%s"
        end
end
        local t={}
local t = {}
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
table.insert(t, str)
        end
end
        return t
return t
end
end
   
   
Line 148: Line 148:
--For example calling startsWith ("Lith V1 Relic", "Lith") would return true
--For example calling startsWith ("Lith V1 Relic", "Lith") would return true
function p.startsWith(string1, start)
function p.startsWith(string1, start)
    return string.sub(string1, 1, string.len(start)) == start
return string.sub(string1, 1, string.len(start)) == start
end
end
   
   
Line 154: Line 154:
--Adds commas
--Adds commas
function p.formatnum(number)
function p.formatnum(number)
  local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
   
   
  -- reverse the int-string and append a comma to all blocks of 3 digits
-- reverse the int-string and append a comma to all blocks of 3 digits
  int = int:reverse():gsub("(%d%d%d)", "%1,")
int = int:reverse():gsub("(%d%d%d)", "%1,")
   
   
  -- reverse the int-string back remove an optional comma and put the  
-- reverse the int-string back remove an optional comma and put the  
  -- optional minus and fractional part back
-- optional minus and fractional part back
  return minus .. int:reverse():gsub("^,", "") .. fraction
return minus .. int:reverse():gsub("^,", "") .. fraction
end
end


Line 170: Line 170:


function p.round(val, maxDigits, minDigits)
function p.round(val, maxDigits, minDigits)
    if(val == nil) then
if val == nil then
        return nil
return nil
    else
else
        if(type(maxDigits) == "table") then
if type(maxDigits) == "table" then
            minDigits = maxDigits[2]
minDigits = maxDigits[2]
            maxDigits = maxDigits[1]
maxDigits = maxDigits[1]
        end
end
   
   
        local result = val..""
local result = val..""
        local decimals = string.find(result, "%.")
local decimals = string.find(result, "%.")
        if(decimals ~= nil ) then decimals = string.len(result) - decimals else decimals = 0 end
if decimals ~= nil then
decimals = string.len(result) - decimals
else
decimals = 0
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)
        elseif(minDigits ~= nil and decimals < minDigits) then
elseif minDigits ~= nil and decimals < minDigits then
            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)
    local mult = 10^(numDecimalPlaces or 0)
local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
return math.floor(num * mult + 0.5) / mult
end
end
   
   
Line 203: Line 207:
-- post: returns a boolean; true if element exists in List, false otherwise
-- post: returns a boolean; true if element exists in List, false otherwise
function p.contains(List, Item, IgnoreCase)
function p.contains(List, Item, IgnoreCase)
    if (List == nil or Item == nil) then  
if List == nil or Item == nil then  
        return false  
return false  
    end
end
    if(IgnoreCase == nil) then  
if IgnoreCase == nil then  
        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
            if (value == Item) then
if value == Item then
                return true, key
return true, key
            elseif (IgnoreCase and string.upper(value) == string.upper(Item)) then
elseif IgnoreCase and string.upper(value) == string.upper(Item) then
                return true, key
return true, key
            end
end
        end
end
    else
else
        local start = string.find(List, Item)
local start = string.find(List, Item)
        return start ~= nil
return start ~= nil
    end
end
    return false
return false
end
end
   
   
Line 239: Line 243:
--User:Giga Martin
--User:Giga Martin
function p.trim(str)
function p.trim(str)
  return (str:gsub("^%s*(.-)%s*$", "%1"))
return (str:gsub("^%s*(.-)%s*$", "%1"))
end
end
   
   
Line 251: Line 255:
--      if key contains a nil value
--      if key contains a nil value
function p.hasKey(table, key, length)
function p.hasKey(table, key, length)
    if (length == nil) then
if (length == nil) then
        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
        local elem = table[i]  -- storing one of inner tables into a variable
local elem = table[i]  -- storing one of inner tables into a variable
        if (elem[key] ~= nil) then
if (elem[key] ~= nil) then
            return true
return true
        end
end
    end
end
    return false
return false
end
end
   
   
Line 269: Line 273:
-- Stolen from https://gist.github.com/tylerneylon/81333721109155b2d244
-- Stolen from https://gist.github.com/tylerneylon/81333721109155b2d244
function p.clone(obj)
function p.clone(obj)
    if type(obj) ~= 'table' then return obj end
if type(obj) ~= 'table' then return obj end
    local res = {}
local res = {}
    for k, v in pairs(obj) do res[p.clone(k)] = p.clone(v) end
for k, v in pairs(obj) do res[p.clone(k)] = p.clone(v) end
    return res
return res
end
end


-- Euclidean Greatest Common Divisor algorithm
-- Euclidean Greatest Common Divisor algorithm
function p.gcd(a, b)
function p.gcd(a, b)
  if b ~= 0 then
if b ~= 0 then
    return p.gcd(b, a % b)
return p.gcd(b, a % b)
  else
else
    return math.abs(a)
return math.abs(a)
  end
end
end
end


--Formats a pair of numbers as a reduced fraction
--Formats a pair of numbers as a reduced fraction
function p.fraction(n, d)
function p.fraction(n, d)
  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
end


function p.timeString(timeInSeconds, shorten)
function p.timeString(timeInSeconds, shorten)
  local remain = timeInSeconds
local remain = timeInSeconds
  local days, hours, minutes = 0, 0, 0
local days, hours, minutes = 0, 0, 0
  local isShort = shorten
local isShort = shorten
 
  local pieces = {}


  if remain >= 86400 then
local pieces = {}
    days = math.floor(remain / 86400)
 
    remain = remain - days * 86400
if remain >= 86400 then
    if isShort then
days = math.floor(remain / 86400)
      table.insert(pieces, days..'d')
remain = remain - days * 86400
    elseif days > 1 then
if isShort then
      table.insert(pieces, days..' days')
table.insert(pieces, days..'d')
    else
elseif days > 1 then
      table.insert(pieces, days..' day')
table.insert(pieces, days..' days')
    end
else
  end
table.insert(pieces, days..' day')
  if remain >= 3600 then
end
    hours = math.floor(remain / 3600)
end
    remain = remain - hours * 3600
if remain >= 3600 then
    if isShort then
hours = math.floor(remain / 3600)
      table.insert(pieces, hours..'h')
remain = remain - hours * 3600
    elseif hours > 1 then
if isShort then
      table.insert(pieces, hours..' hours')
table.insert(pieces, hours..'h')
    else
elseif hours > 1 then
      table.insert(pieces, hours..' hour')
table.insert(pieces, hours..' hours')
    end
else
  end
table.insert(pieces, hours..' hour')
  if remain >= 60 then
end
    minutes = math.floor(remain / 60)
end
    remain = remain - minutes * 60
if remain >= 60 then
    if isShort then
minutes = math.floor(remain / 60)
      table.insert(pieces, minutes..'m')
remain = remain - minutes * 60
    elseif minutes > 1 then
if isShort then
      table.insert(pieces, minutes..' minutes')
table.insert(pieces, minutes..'m')
    else
elseif minutes > 1 then
      table.insert(pieces, minutes..' minutes')
table.insert(pieces, minutes..' minutes')
    end
else
  end
table.insert(pieces, minutes..' minutes')
  if remain > 0 then
end
    if isShort then
end
      table.insert(pieces, remain..'s')
if remain > 0 then
    elseif remain > 1 then
if isShort then
      table.insert(pieces, remain..' seconds')
table.insert(pieces, remain..'s')
    else
elseif remain > 1 then
      table.insert(pieces, remain..' second')
table.insert(pieces, remain..' seconds')
    end
else
  end
table.insert(pieces, remain..' second')
  return table.concat(pieces, ', ')
end
end
return table.concat(pieces, ', ')
end
end


function p.fixPagename(pageName)
function p.fixPagename(pageName)
  local result = pageName
local result = pageName
  result = string.gsub(result, "%%27", "'")
result = string.gsub(result, "%%27", "'")
  result = string.gsub(result, "&#39;", "'")
result = string.gsub(result, "&#39;", "'")
  return result
return result
end
end


--Checks if two tables contain the same value with the same indices
--Checks if two tables contain the same value with the same indices
function p.tablesEqual(t1, t2)
function p.tablesEqual(t1, t2)
  if p.tableCount(t1) ~= p.tableCount(t2) then return false end
if p.tableCount(t1) ~= p.tableCount(t2) then return false end
  for i, val in p.skpairs(t1) do
for i, val in p.skpairs(t1) do
    if type(val) ~= type(t2[i]) then
if type(val) ~= type(t2[i]) then
      return false
return false
    elseif type(val) == 'table' then
elseif type(val) == 'table' then
      if not p.tablesEqual(val, t2[i]) then return false end
if not p.tablesEqual(val, t2[i]) then return false end
    elseif t2[i] ~= val then  
elseif t2[i] ~= val then  
      return false  
return false  
    end
end
  end
end
  return true
return true
end
end


--Returns a number including the sign, even if positive
--Returns a number including the sign, even if positive
function p.numStrWithSign(number)
function p.numStrWithSign(number)
  if number >= 0 then
if number >= 0 then
    return '+'..p.formatnum(number)
return '+'..p.formatnum(number)
  else
else
    return p.formatnum(number)
return p.formatnum(number)
  end
end
end
 
-- Takes a table input with elements of type string, returning a string formed from
-- each element separated by the specified delimiter. Optionally uses a different delimiter
-- for the last two elements
function p.joinList(list, sep, lastSep)
  if lastSep ~= nil and sep ~= lastSep and #list > 1 then
    return string.sub(table.concat(list, sep), 1, -(1 + string.len(sep) + string.len(list[#list]))) .. lastSep .. list[#list]
  else
    return table.concat(list, sep)
  end
end
end


return p
return p