Module:CommonFunctions: Difference between revisions

From Elwiki
No edit summary
No edit summary
(29 intermediate revisions by the same user not shown)
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
 
function inArrayHas(key, array)
    for k, v in pairs(array) do
        if string.find(k, key) then
            return true
        end
    end
end
 
function inArrayHasValue(value, array)
    for k, v in pairs(array) do
        if string.find(v, value) then
            return true
        end
    end
end
 
function indexOf(value, array)
    for i, v in ipairs(array) do
        if v == value then
            return i
        end
    end
    return nil
end
end


Line 15: Line 42:
function string.starts(String, Start)
function string.starts(String, Start)
     return string.sub(String, 1, string.len(Start)) == Start
     return string.sub(String, 1, string.len(Start)) == Start
end
function string.ends(str, ending)
    return ending == "" or str:sub(-#ending) == ending
end
end


Line 20: Line 51:
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 33: Line 66:
-- 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
 
function indexTableMerge(first_table, second_table)
    for k, v in spairs(second_table) do
        table.insert(first_table, v)
    end
end
end


Line 41: Line 82:
         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 47: Line 88:
-- 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


-- Implement splitting string to a table.
-- Implement splitting string to a table.
function split(s, delimiter)
function split(s, delimiter, skip_empty)
     if delimiter == nil then delimiter = ',' end
    if not s then
        return {}
    end
    local i = 1
     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, match);
         if (skip_empty == true and trim(match) ~= '') or skip_empty == nil then
            table.insert(result, i, trim(match));
            i = i + 1
        end
     end
     end
     return result;
     return result;
Line 61: Line 111:


-- 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
 
function table.containsKey(tbl, key)
    return tbl[key] ~= nil
end
 
function table.containsValue(tbl, val)
    local contains = false
    for k, v in pairs(tbl) do
        if (v == val) then
            contains = true
            break
        end
    end
    return contains
end
 
function table.matches(tbl, val)
    local matches = 0
    for k, v in pairs(tbl) do
        if (type(v) == 'table') then
            matches = matches + table.matches(v, val)
        else
            if (v == val) then
                matches = matches + 1
            end
        end
    end
    return matches
end
 
function dump(o)
    if type(o) == 'table' then
        local s = '{ <br>'
        for k, v in pairs(o) do
            if type(k) ~= 'number' then
                k = '"' .. k .. '"'
            end
            s = s .. '[' .. k .. '] = ' .. dump(v) .. ',<br>'
        end
        return s .. '}'
    else
        return tostring(o)
    end
end
 
function table.not_empty(tbl)
    return not next(tbl)
end
 
function math.round(num, decimals)
    decimals = math.pow(10, decimals or 0)
    num = num * decimals
    if num >= 0 then
        num = math.floor(num + 0.5)
    else
        num = math.ceil(num - 0.5)
    end
    return num / decimals
end
 
function formatnum(amount)
    local formatted = amount
    while true do
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
        if (k == 0) then
            break
        end
    end
    return formatted
end
end

Revision as of 11:24, 17 October 2022

Documentation for this module may be created at Module:CommonFunctions/doc

-- in Array
function inArray(key, array)
    if array[key] ~= nil then
        return true
    end
end

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

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

function inArrayHasValue(value, array)
    for k, v in pairs(array) do
        if string.find(v, value) then
            return true
        end
    end
end

function indexOf(value, array)
    for i, v in ipairs(array) do
        if v == value then
            return i
        end
    end
    return nil
end

-- String starts
function string.starts(String, Start)
    return string.sub(String, 1, string.len(Start)) == Start
end

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

-- Implement sorted loop through array.
function spairs(t)
    local keys = {}
    for k in pairs(t) do
        keys[#keys + 1] = k
    end
    table.sort(keys)
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

-- Implement merging tables
function tableMerge(first_table, second_table)
    for k, v in spairs(second_table) do
        first_table[k] = v
    end
end

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

-- Implement rounding.
function round(num, numDecimalPlaces)
    if numDecimalPlaces == nil then
        numDecimalPlaces = 2
    end
    local mult = 10 ^ (numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end

-- Implement string trim.
function trim(s)
    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

-- Implement splitting string to a table.
function split(s, delimiter, skip_empty)
    if not s then
        return {}
    end
    local i = 1
    if delimiter == nil then
        delimiter = ','
    end
    result = {};
    for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
        if (skip_empty == true and trim(match) ~= '') or skip_empty == nil then
            table.insert(result, i, trim(match));
            i = i + 1
        end
    end
    return result;
end

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

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

function table.containsKey(tbl, key)
    return tbl[key] ~= nil
end

function table.containsValue(tbl, val)
    local contains = false
    for k, v in pairs(tbl) do
        if (v == val) then
            contains = true
            break
        end
    end
    return contains
end

function table.matches(tbl, val)
    local matches = 0
    for k, v in pairs(tbl) do
        if (type(v) == 'table') then
            matches = matches + table.matches(v, val)
        else
            if (v == val) then
                matches = matches + 1
            end
        end
    end
    return matches
end

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

function table.not_empty(tbl)
    return not next(tbl)
end

function math.round(num, decimals)
    decimals = math.pow(10, decimals or 0)
    num = num * decimals
    if num >= 0 then
        num = math.floor(num + 0.5)
    else
        num = math.ceil(num - 0.5)
    end
    return num / decimals
end

function formatnum(amount)
    local formatted = amount
    while true do
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
        if (k == 0) then
            break
        end
    end
    return formatted
end