Skip to content

Commit

Permalink
fix: simplify parse_opts with vim.tbl_deep_extend
Browse files Browse the repository at this point in the history
  • Loading branch information
fabridamicelli committed Apr 27, 2024
1 parent 5353f91 commit 3cfc000
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lua/cronex/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,24 @@ local defaults = {
}

M.parse_opts = function(opts)
local user = vim.F.if_nil(opts, {})

-- Set first level keys, otherwise user.* will fail
for k, v in pairs(defaults) do
user[k] = vim.F.if_nil(user[k], v)
end
opts = vim.tbl_deep_extend("force", defaults, opts)

local extract = function()
local extract = user.extractor.extract or defaults.extractor.extract
local cron_from_line = user.extractor.cron_from_line or defaults.extractor.cron_from_line
return extract(cron_from_line)
return opts.extractor.extract(opts.extractor.cron_from_line)
end

local explain = function(cron)
local cmd = user.explainer.cmd or defaults.explainer.cmd
local args = user.explainer.args or defaults.explainer.args
local cmd = opts.explainer.cmd
local args = opts.explainer.args
local full_cmd = vim.tbl_flatten({ cmd, args })
return require("cronex.explain").explain(full_cmd, cron)
end

return {
file_patterns = user.file_patterns or defaults.file_patterns,
file_patterns = opts.file_patterns,
extract = extract,
explain = explain,
format = user.format or defaults.format,
format = opts.format,
}
end

Expand Down

0 comments on commit 3cfc000

Please sign in to comment.