Anonymous

Module:CommonFunctions: Difference between revisions

From Elwiki
no edit summary
No edit summary
No edit summary
(16 intermediate revisions by the same user not shown)
Line 12: Line 12:
         end
         end
     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 67: Line 92:


-- Implement splitting string to a table.
-- Implement splitting string to a table.
function split(s, delimiter)
function split(s, delimiter, skip_empty)
    if not s then
        return {}
    end
     local i = 1
     local i = 1
     if delimiter == nil then
     if delimiter == nil then
Line 74: Line 102:
     result = {};
     result = {};
     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
         table.insert(result, i, match);
         if (skip_empty == true and trim(match) ~= '') or skip_empty == nil then
        i = i + 1
            table.insert(result, i, trim(match));
            i = i + 1
        end
     end
     end
     return result;
     return result;
Line 135: Line 165:
function table.not_empty(tbl)
function table.not_empty(tbl)
     return not next(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
ElEditors, Administrators
70,706

edits