Skip to content

Commit

Permalink
[Feat] add some common functional utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Feb 12, 2024
1 parent adfd940 commit e21ac58
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Lua
/luarocks
/lua
/lua_modules
/.luarocks

# OS
.DS_Store
.DS_Store
9 changes: 9 additions & 0 deletions lua/plugin-name/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local table_utils = require("utils.table")
local static = require("plugin-name.static")
local main = {}

function main.setup(config)
static.config = table_utils.merge(static.config, config)
end

return main
3 changes: 3 additions & 0 deletions lua/plugin-name/static.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return {
config = {},
}
12 changes: 12 additions & 0 deletions lua/plugin-name/utils/command.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local command_util = {}

function command_util.exec_command(command, mode)
local handle = assert(io.popen(command, mode))
local origin = assert(handle:read("*a"))

handle:close()

return origin
end

return command_util
23 changes: 23 additions & 0 deletions lua/plugin-name/utils/list.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local list_utils = {}

function list_utils.find(list, predicate)
for _, value in ipairs(list) do
if predicate(value) then
return value
end
end

return nil
end

function list_utils.some(list, predicate)
return list_utils.find(list, predicate) ~= nil
end

function list_utils.includes(list, value)
return list_utils.find(list, function(item)
return item == value
end) ~= nil
end

return list_utils
15 changes: 15 additions & 0 deletions lua/plugin-name/utils/string.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local string_util = {}

function string_util.trim(str)
return str:gsub("%s+", "")
end

function string_util.escape(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
end

function string_util.ends_with(str, suffix)
return str:sub(-#suffix) == suffix
end

return string_util
17 changes: 17 additions & 0 deletions lua/plugin-name/utils/table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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

return table_utils

0 comments on commit e21ac58

Please sign in to comment.