Slay the Spire Wiki
Advertisement

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

--Shared module invoked by several other Module files

local p ={}

-- iterator sorted by keys
-- For example, if you had a table that looked something like
-- data = {["Cat"] = 5,
--         ["Bat"] = 4,
--         ["Hat"] = 7}
-- You could do
--  for k, v in skpairs(data) do...
-- And your loop would start with k="Bat", v=4 then go to k="Cat", v=5, 
--         and finally to k="Hat", v=7
function p.skpairs(t)
    local keys = {}
    for k in pairs(t) do keys[#keys + 1] = k end
    table.sort(keys)
 
    local i = 0
    local iterator = function()
        i = i + 1
        local key = keys[i]
        if key then
            return key, t[key]
        else
            return nil
        end
    end
    return iterator
end

--Checks if a list contains an item
function p.contains(List, Item, IgnoreCase)
    if(List == nil or Item == nil) then return false end
    if(IgnoreCase == nil) then IgnoreCase = false end
 
    if(type(List) == "table") then
        for i, listI in pairs(List) do
            if(listI == Item) then
                return true
            elseif(IgnoreCase and string.upper(listI) == string.upper(Item)) then
                return true
            end
        end
    else
        local start = string.find(List, Item)
        return start ~= nil
    end
    return false
end

--Returns the number of rows in a table 
function p.tableCount(t)
    local count = 0
    for _ in pairs(t) do count = count + 1 end
    return count
end

return p
Advertisement