Skip to content

Commit

Permalink
[Feat] add logger & path utils and perf table utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Mar 14, 2024
1 parent 5e64ba4 commit 6face79
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lua/plugin-name/utils/logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local logger = {}

function logger.log(level, message)
vim.api.nvim_notify("[" .. level .. "] CodeSnap: " .. tostring(vim.inspect(message)), vim.log.levels[level], {})
end

function logger.error(message)
logger.log("ERROR", message)
end

return logger
9 changes: 9 additions & 0 deletions lua/plugin-name/utils/path.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local path_utils = {}

function path_utils.back(path)
local parsed_path, _ = path:gsub("/[^\\/]+/?$", "")

return parsed_path
end

return path_utils
53 changes: 53 additions & 0 deletions lua/plugin-name/utils/table.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local list_utils = require("codesnap.utils.list")
local table_utils = {}

function table_utils.assign(t, props)
Expand All @@ -14,4 +15,56 @@ function table_utils.merge(t1, t2)
return t1
end

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

return table_utils
2 changes: 1 addition & 1 deletion project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "plugin-name"
version = "0.0.3"
version = "0.0.4"
description = "description"
author = "Mist"
email = "[email protected]"

0 comments on commit 6face79

Please sign in to comment.