[cmd] ListToString

stable
By Strooth in Miscellaneous Published November 2021 👁 1,076 views 💬 0 comments

Description

Convert List to String for Logging usually Usage - CC_ListToString({['a']={1,2,3,4},'d','g',1,4,8,3,'3','4',{1}, ok="AverageOrderbookSpread",{this={'a','d','g',1,4,8,3,'3','4','m'}}})
HaasScript
DefineCommand('ListToString', 'combines all list values to a single string')
--local default = {['a']={1,2,3,4},'d','g',1,4,8,3,'3','4',{1}, ok="AverageOrderbookSpread",{this={'a','d','g',1,4,8,3,'3','4','m'}}}
local list, convert = DefineParameter(ListDynamicType, 'list', 'The list to convert', false, {}, 'ListDynamicType')
local seperator = DefineParameter(StringType, 'seperator', 'the string to use to seperate each item in the list', false, ', ', 'string, text')
local logging = DefineParameter(BooleanType, 'logging', 'enable log', false, false, 'boolean, true/false')

local logit = function(en, type, key, value)
    if en then 
        if type == 1 then 
            local keytype = Parse(GetType(key), StringType) 
            local valuetype = Parse(GetType(value), StringType)
            Log('keytype = '..keytype..' = '..key)
            Log('valuetype = '..valuetype..' = '..convert(value, false))
        elseif type == 2 then 
            Log(key)
            Log(GetType(value))
            Log(convert(value))
            Log(value)
        end
    end
end

convert = function(list, seperator, log)
    local count = 0
    local join = function(_key, _string, _value)
            if count == 0 then 
                count = count + 1 
                return _value
            else 
                return StringJoin(StringJoin(_string, seperator), _value,  ' ')
            end 
    end
    local string = ''
    if StringContains(GetType(list), ArrayDataType) then
        for key, value in pairs(list) do 
            logit(log, 1, key, value)
            
            if StringContains(GetType(value), ArrayDataType) then 
                logit(log, 2, key, value)
            
                local v = '{'..key..' = '..convert(value)..'}'
                string = join(key, string, v, seperator)
            else
                if StringContains(GetType(key), TextDataType) then 
                    local v = '{'..key..' = '..convert(value)..'}'
                    string = join(key, string, v, seperator)
                else 
                    string = join(key, string, value, seperator)
                end 
            end
        end
        return string
    else 
        return list
    end 
end
DefineOutput(StringType, convert(list, seperator, logging), 'The list converted to a string', 'string')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!