1
0
Fork 0
codesnap.nvim/lua/codesnap/utils/table.lua

71 lines
1.3 KiB
Lua
Raw Normal View History

2024-02-19 14:27:17 +00:00
local list_utils = require("codesnap.utils.list")
2024-02-13 06:05:36 +00:00
local table_utils = {}
function table_utils.assign(t, props)
for k, v in pairs(props) do
t[k] = v
end
end
function table_utils.merge(t1, t2)
for k, v in pairs(t2) do
t1[k] = v
end
return t1
end
2024-02-19 14:27:17 +00:00
function table_utils.is_array(t)
return type(t[1]) == "number"
end
function table_utils.typeof(value)
if type(value) == "table" then
if table_utils.is_array(value) then
return "array"
else
return "table"
end
end
return type(value)
end
function table_utils.serialize_array(t)
local result = list_utils.map(t, function(ele)
table_utils.serialize_json(ele)
end)
return "[" .. result.concat(t, ",") .. "]"
end
function table_utils.serialize_table(t)
local result = {}
for key, value in pairs(t) do
table.insert(result, string.format('"%s":%s', key, table_utils.serialize_json(value)))
end
return "{" .. table.concat(result, ",") .. "}"
end
function table_utils.serialize_string(value)
return '"' .. value .. '"'
end
function table_utils.serialize_json(t)
local complex_type_parser = {
array = table_utils.serialize_array,
table = table_utils.serialize_table,
string = table_utils.serialize_string,
}
local parse = complex_type_parser[table_utils.typeof(t)] or function(v)
return v
end
return parse(t)
end
2024-02-13 06:05:36 +00:00
return table_utils