helper.lua 1.47 KB
--[[
@Author  : Mr.wang
@Date    : 2016-04-25 09:25:06
@Content : helper
--]]

local cc = cc
local _tostring = tostring
local type, tonumber, table, string = type, tonumber, table, string

local helper = {}
setfenv(1, helper)

function tostring(value)
    local str = ''

    if (type(value) ~= 'table') then
        if (type(value) == 'string') then
            str = string.format("%q", value)
        else
            str = _tostring(value)
        end
    else
        local auxTable = {}
        table.foreach(value, function(i, v)
            if (tonumber(i) ~= i) then
                table.insert(auxTable, i)
            else
                table.insert(auxTable, tostring(i))
            end
        end)
        table.sort(auxTable)

        str = str..'{'
        local separator = ""
        local entry = ""
        table.foreachi (auxTable, function (i, fieldName)
            if ((tonumber(fieldName)) and (tonumber(fieldName) > 0)) then
                entry = tostring(value[tonumber(fieldName)])
            else
                entry = fieldName.." = "..tostring(value[fieldName])
            end
            str = str..separator..entry
            separator = ", "
        end)
        str = str..'}'
    end
    return str
end

function format(e)
    if type(e) ~= "string" then
        e = tostring(e)
    end
    return e
end

function isWindows()
    local platform = cc.Application:getInstance():getTargetPlatform()
    return platform == cc.PLATFORM_OS_WINDOWS
end

return helper