Module:CommonFunctions: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1: Line 1:
-- in Array
-- in Array
function inArray(key, array)
function inArray(key, array)
  if array[key] ~= nil then
    if array[key] ~= nil then
      return true
        return true
  end
    end
end
end


function inArrayStarts(key, array)
function inArrayStarts(key, array)
  for k, v in pairs(array) do
    for k, v in pairs(array) do
      if string.starts(k, key) then return true end
        if string.starts(k, key) then
  end
            return true
        end
    end
end
end


Line 18: Line 20:


function string.ends(str, ending)
function string.ends(str, ending)
  return ending == "" or str:sub(-#ending) == ending
    return ending == "" or str:sub(-#ending) == ending
end
end


Line 24: Line 26:
function spairs(t)
function spairs(t)
     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
     table.sort(keys)
     table.sort(keys)
     local i = 0
     local i = 0
Line 37: Line 41:
-- Implement merging tables
-- Implement merging tables
function tableMerge(first_table, second_table)
function tableMerge(first_table, second_table)
     for k,v in spairs(second_table) do first_table[k] = v end
     for k, v in spairs(second_table) do
        first_table[k] = v
    end
end
end


function indexTableMerge(first_table, second_table)
function indexTableMerge(first_table, second_table)
  for k,v in spairs(second_table) do table.insert(first_table, v) end
    for k, v in spairs(second_table) do
        table.insert(first_table, v)
    end
end
end


Line 49: Line 57:
         numDecimalPlaces = 2
         numDecimalPlaces = 2
     end
     end
     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 55: Line 63:
-- Implement string trim.
-- Implement string trim.
function trim(s)
function trim(s)
  return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
end


Line 61: Line 69:
function split(s, delimiter)
function split(s, delimiter)
     local i = 1
     local i = 1
     if delimiter == nil then delimiter = ',' end
     if delimiter == nil then
        delimiter = ','
    end
     result = {};
     result = {};
     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
         table.insert(result, i, match);
         table.insert(result, i, match);
         i = i + 1
         i = i + 1
Line 71: Line 81:


-- Title case
-- Title case
function titleCaseFunc( first, rest )
function titleCaseFunc(first, rest)
  return first:upper()..rest:lower()
    return first:upper() .. rest:lower()
end
end


function titleCase(str)
function titleCase(str)
  return string.gsub(str, "(%a)([%w_']*)", titleCaseFunc)
    return string.gsub(str, "(%a)([%w_']*)", titleCaseFunc)
end
end


Line 85: Line 95:
function table.containsValue(tbl, val)
function table.containsValue(tbl, val)
     local contains = false
     local contains = false
     for k,v in pairs(tbl) do
     for k, v in pairs(tbl) do
      if (v == val) then
        if (v == val) then
          contains = true
            contains = true
          break
            break
      end
        end
     end
     end
     return contains
     return contains
Line 96: Line 106:
function table.matches(tbl, val)
function table.matches(tbl, val)
     local matches = 0
     local matches = 0
     for k,v in pairs(tbl) do
     for k, v in pairs(tbl) do
      if (v == val) then
        if (type(v) == 'table') then
          matches = matches + 1
            matches = matches + table.matches(v, val)
      end
        else
            if (v == val) then
                matches = matches + 1
            end
        end
     end
     end
     return matches
     return matches
end
end


    function dump(o)
function dump(o)
        if type(o) == 'table' then
    if type(o) == 'table' then
            local s = '{ <br>'
        local s = '{ <br>'
            for k, v in pairs(o) do
        for k, v in pairs(o) do
                if type(k) ~= 'number' then
            if type(k) ~= 'number' then
                    k = '"' .. k .. '"'
                k = '"' .. k .. '"'
                end
                s = s .. '[' .. k .. '] = ' .. dump(v) .. ',<br>'
             end
             end
             return s .. '}'
             s = s .. '[' .. k .. '] = ' .. dump(v) .. ',<br>'
        else
            return tostring(o)
         end
         end
        return s .. '}'
    else
        return tostring(o)
     end
     end
end
ElEditors, Administrators
70,763

edits