Module:ControllerUtilities

From Against the Storm Official Wiki
Revision as of 01:42, 5 May 2024 by Aeredor (talk | contribs) (Fixed some error validation)

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

---
--- Centralizes some common functionality across controllers
---
---@module ControllerUtilities
ControllerUtilities = {}

--region Public Constants

--endregion



--region Private Constants

local PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES = "[^%s,][^,]+[^%s,]"

--endregion



--region Public methods

--- Converts a string that represents a list of items into an array of those items. This is useful when a template parameter is used as a list and not a single value.
---
---@param commaSeparatedStringOfIDs string the list of IDs, separated by commas
---@param needsKeyValueFlags boolean true if the list should be made of keys (IDs) and values of `true`, false if the list should be an array of IDs
---@return table the IDs now separated into elements in a table
function ControllerUtilities.expandCommaSeparatedStringsIntoTable(commaSeparatedStringOfIDs, needsKeyValueFlags)

    if not commaSeparatedStringOfIDs or "" == commaSeparatedStringOfIDs then
        return {}
    end

    local list = {}
    for eachID in string.gmatch(commaSeparatedStringOfIDs, PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES) do
        if needsKeyValueFlags then
            list[eachID] = true
        else
            table.insert(list, eachID)
        end
    end

    return list
end

--endregion

return ControllerUtilities