mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-29 04:06:39 +09:00
Added Module: ExpGamingCore.Gui
This commit is contained in:
@@ -394,128 +394,4 @@ function table.arr_to_bool(tbl)
|
||||
end
|
||||
end
|
||||
return newtbl
|
||||
end
|
||||
|
||||
-- Any thing below here i (cooldude2606) have added and was not here by default
|
||||
--- Returns a value in a form able to be read as a value
|
||||
-- @usage local a = 'value'
|
||||
-- table.val_to_str(a) -- return '"value"'
|
||||
-- @param v value to convert
|
||||
-- @treturn string the converted value
|
||||
function table.val_to_str(v)
|
||||
if "string" == type( v ) then
|
||||
v = string.gsub(v,"\n","\\n")
|
||||
if string.match(string.gsub(v,"[^'\"]",""),'^"+$') then
|
||||
return "'"..v.."'"
|
||||
end
|
||||
return '"'..string.gsub(v,'"', '\\"' )..'"'
|
||||
else
|
||||
return "table" == type( v) and table.tostring(v) or
|
||||
"function" == type(v) and '"cant-display-function"' or
|
||||
"userdata" == type(v) and '"cant-display-userdata"' or
|
||||
tostring(v)
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns a value in a form able to be read as a key
|
||||
-- @usage local a = 'key'
|
||||
-- table.key_to_str(a) -- return '["key"]'
|
||||
-- @param k key to convert
|
||||
-- @treturn string the converted key
|
||||
function table.key_to_str (k)
|
||||
if "string" == type(k) and string.match(k,"^[_%player][_%player%d]*$") then
|
||||
return k
|
||||
else
|
||||
return "["..table.val_to_str(k).."]"
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns a table in a form able to be read as a table
|
||||
-- @usage local a = {k1='foo',k2='bar'}
|
||||
-- table.tostring(a) -- return '{["k1"]="foo",["k2"]="bar"}'
|
||||
-- @tparam table tbl table to convert
|
||||
-- @treturn string the converted table
|
||||
function table.tostring(tbl)
|
||||
local result, done = {}, {}
|
||||
for k, v in ipairs(tbl) do
|
||||
table.insert(result,table.val_to_str(v))
|
||||
done[k] = true
|
||||
end
|
||||
for k, v in pairs(tbl) do
|
||||
if not done[k] then
|
||||
table.insert(result,
|
||||
table.key_to_str(k).."="..table.val_to_str(v))
|
||||
end
|
||||
end
|
||||
return "{"..table.concat(result,",") .."}"
|
||||
end
|
||||
|
||||
--- Simmilar to table.tostring but converts a lua table to a json one
|
||||
-- @usage local a = {k1='foo',k2='bar'}
|
||||
-- talbe.json(a) -- return '{"k1":"foo","k2":"bar"}'
|
||||
-- @tparam table lua_table the table to convert
|
||||
-- @treturn string the table in a json format
|
||||
function table.json(lua_table)
|
||||
local result, done, only_indexs = {}, {}, true
|
||||
for key,value in ipairs(lua_table) do
|
||||
done[key] = true
|
||||
if type(value) == 'table' then table.insert(result,table.json(value,true))
|
||||
elseif type(value) == 'string' then table.insert(result,'"'..value..'"')
|
||||
elseif type(value) == 'number' then table.insert(result,value)
|
||||
elseif type(value) == 'boolean' then table.insert(result,tostring(value))
|
||||
else table.insert(result,'null')
|
||||
end
|
||||
end
|
||||
for key,value in pairs(lua_table) do
|
||||
if not done[key] then
|
||||
only_indexs = false
|
||||
if type(value) == 'table' then table.insert(result,'"'..key..'":'..table.json(value,true))
|
||||
elseif type(value) == 'string' then table.insert(result,'"'..key..'":"'..value..'"')
|
||||
elseif type(value) == 'number' then table.insert(result,'"'..key..'":'..value)
|
||||
elseif type(value) == 'boolean' then table.insert(result,'"'..key..'":'..tostring(value))
|
||||
else table.insert(result,'"'..key..'":null')
|
||||
end
|
||||
end
|
||||
end
|
||||
if only_indexs then return "["..table.concat(result,",").."]"
|
||||
else return "{"..table.concat(result,",").."}"
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns the closest match to a key
|
||||
-- @usage tbl = {foo=1,bar=2}
|
||||
-- table.autokey(tbl,'f') -- return 1
|
||||
function table.autokey(tbl,str)
|
||||
local _return = {}
|
||||
for key,value in pairs(tbl) do
|
||||
if string.contains(string.lower(key),string.lower(str)) then table.insert(_return,value) end
|
||||
end
|
||||
return _return[1] or false
|
||||
end
|
||||
|
||||
--- Returns the list is a sorted way that would be expected by people (this is by key)
|
||||
-- @usage tbl = table.alphanumsort(tbl)
|
||||
-- @tparam table tbl the table to be sorted
|
||||
-- @treturn table the sorted table
|
||||
function table.alphanumsort(tbl)
|
||||
local o = table.keys(tbl)
|
||||
local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)")
|
||||
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) end
|
||||
table.sort(o, function(a,b)
|
||||
return tostring(a):gsub("%.?%d+",padnum)..("%3d"):format(#b)
|
||||
< tostring(b):gsub("%.?%d+",padnum)..("%3d"):format(#a) end)
|
||||
local _tbl = {}
|
||||
for _,k in pairs(o) do _tbl[k] = tbl[k] end
|
||||
return _tbl
|
||||
end
|
||||
|
||||
--- Returns the list is a sorted way that would be expected by people (this is by key) (faster alterative than above)
|
||||
-- @usage tbl = table.alphanumsort(tbl)
|
||||
-- @tparam table tbl the table to be sorted
|
||||
-- @treturn table the sorted table
|
||||
function table.keysort(tbl)
|
||||
local o = table.keys(tbl,true)
|
||||
local _tbl = {}
|
||||
for _,k in pairs(o) do _tbl[k] = tbl[k] end
|
||||
return _tbl
|
||||
end
|
||||
Reference in New Issue
Block a user