Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
Replace GCD function with an implementation of the Euclidean algorithm, so that fewer iterations occur when numbers differ greatly in magnitude
(Added timeString function)
(Replace GCD function with an implementation of the Euclidean algorithm, so that fewer iterations occur when numbers differ greatly in magnitude)
(4 intermediate revisions by one other user not shown)
Line 178: Line 178:
   
   
         if(maxDigits ~= nil and decimals > maxDigits) then
         if(maxDigits ~= nil and decimals > maxDigits) then
             result = tonumber(string.format("%."..maxDigits.."f", result))
             result = string.format("%."..minDigits.."f", tonumber(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)
Line 270: Line 270:
end
end


-- Euclid's Greatest Common Divisor algorithm
-- Euclidean Greatest Common Divisor algorithm
function p.gcd(a, b)
function p.gcd(a, b)
   if(a == b) then
   if b ~= 0 then
     return a
     return p.gcd(b, a % b)
   else
   else
     if(a > b) then
     return math.abs(a)
      if b == 0 then
        return a
      else
        return p.gcd(a - b, b)
      end
    else
      if a == 0 then
        return b
      else
        return p.gcd(a, b - a)
      end
    end
   end
   end
end
end
Line 297: Line 285:
end
end


function p.timeString(timeInSeconds)
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 pieces = {}
   local pieces = {}
Line 306: Line 295:
     days = math.floor(remain / 86400)
     days = math.floor(remain / 86400)
     remain = remain - days * 86400
     remain = remain - days * 86400
     if days > 1 then
     if isShort then
      table.insert(pieces, days..'d')
    elseif days > 1 then
       table.insert(pieces, days..' days')
       table.insert(pieces, days..' days')
     else
     else
Line 315: Line 306:
     hours = math.floor(remain / 3600)
     hours = math.floor(remain / 3600)
     remain = remain - hours * 3600
     remain = remain - hours * 3600
     if hours > 1 then
     if isShort then
      table.insert(pieces, hours..'h')
    elseif hours > 1 then
       table.insert(pieces, hours..' hours')
       table.insert(pieces, hours..' hours')
     else
     else
Line 324: Line 317:
     minutes = math.floor(remain / 60)
     minutes = math.floor(remain / 60)
     remain = remain - minutes * 60
     remain = remain - minutes * 60
     if minutes > 1 then
     if isShort then
      table.insert(pieces, minutes..'m')
    elseif minutes > 1 then
       table.insert(pieces, minutes..' minutes')
       table.insert(pieces, minutes..' minutes')
     else
     else
Line 331: Line 326:
   end
   end
   if remain > 0 then
   if remain > 0 then
     if remain > 1 then
     if isShort then
      table.insert(pieces, remain..'s')
    elseif remain > 1 then
       table.insert(pieces, remain..' seconds')
       table.insert(pieces, remain..' seconds')
     else
     else
Line 339: Line 336:
   return table.concat(pieces, ', ')
   return table.concat(pieces, ', ')
end
end
 
function p.fixPagename(pageName)
  local result = pageName
  result = string.gsub(result, "%%27", "'")
  result = string.gsub(result, "&#39;", "'")
  return result
end
 
function p.tablesEqual(t1, t2)
  if p.tableCount(t1) ~= p.tableCount(t2) then return false end
  for i, val in p.skpairs(t1) do
    if type(val) ~= type(t2[i]) then
      return false
    elseif type(val) == 'table' then
      if not p.tablesEqual(val, t2[i]) then return false end
    elseif t2[i] ~= val then
      return false
    end
  end
  return true
end
 
return p
return p