Module:ControllerUtilities: Difference between revisions

From Against the Storm Official Wiki
(Created to centralize a few methods)
 
m (Fixed some error validation)
Line 21: Line 21:
--region Public methods
--region Public methods


---expandIDList
--- 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 and values of true, false if the list should be an array
---@param commaSeparatedStringOfIDs string the list of IDs, separated by commas
---@return table the ids now as a table
---@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)
function ControllerUtilities.expandCommaSeparatedStringsIntoTable(commaSeparatedStringOfIDs, needsKeyValueFlags)
    if not commaSeparatedStringOfIDs or "" == commaSeparatedStringOfIDs then
        return {}
    end


     local list = {}
     local list = {}
     for eachID in string.gmatch(commaSeparatedStringOfIDs, PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES) do
     for eachID in string.gmatch(commaSeparatedStringOfIDs, PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES) do
         if needsKeyValueFlags then
         if needsKeyValueFlags then

Revision as of 01:42, 5 May 2024

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