Skip to content

Commit

Permalink
feat(utils): re-add merge & cleanup merge_inplace usage;
Browse files Browse the repository at this point in the history
  • Loading branch information
5-pebbles committed Jun 16, 2024
1 parent 4f5b445 commit 8c8fff4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lua/nordic/groups/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ local M = {}
function M.get_groups()
local native = require('nordic.groups.native').get_groups()
local integrations = require('nordic.groups.integrations').get_groups()
local groups = merge_inplace(native, integrations)

local groups = {}
merge_inplace(groups, native)
merge_inplace(groups, integrations)

-- Apply on_highlight
local palette = require('nordic.colors')
Expand Down
14 changes: 10 additions & 4 deletions lua/nordic/tests/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ local t2 = {b = 2}
U.merge_inplace(t1, t2)
assert_eq(t1, {a = 1, b = 2}, 'U.merge_inplace(t1, t2) basic merge')

local t1 = {a = 1}
local t2 = {b = 2}
assert_eq(U.merge_inplace(t1, t2) == t1, true, 'U.merge_inplace(t1, t2) should return t1')

local t1 = {a = 1, b = 3}
local t2 = {b = 2, c = 4}
U.merge_inplace(t1, t2)
Expand All @@ -46,6 +42,16 @@ local t2 = {d = nested, e = 5}
U.merge_inplace(t1, t2)
assert_eq(t1['d'] ~= nested, true, 'U.merge_inplace(t1, t2) copy t2 nested values')

-- merge
assert_eq(U.merge({}, {}), {}, 'U.merge({}, {}) should return an empty table')

assert_eq(U.merge(nil, nil), {}, 'U.merge(nil, nil) should return an empty table')

assert_eq(U.merge(nil, {a = 1}), {a = 1}, 'U.merge(nil, {a = 1}) should return {a = 1}')
assert_eq(U.merge({a = 1}, nil), {a = 1}, 'U.merge({a = 1}, nil) should return {a = 1}')

assert_eq(U.merge({a = 1, b = 3}, {b = 2, c = 4}), {a = 1, b = 2, c = 4}, 'U.merge({a = 1, b = 3}, {b = 2, c = 4}) should return {a = 1, b = 2, c = 4}')

-- hex_to_rgb
assert_eq({ U.hex_to_rgb('#191D24') }, {25, 29, 36}, 'U.hex_to_rgb("#191D24") should return 25, 29, 36')

Expand Down
11 changes: 9 additions & 2 deletions lua/nordic/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ function M.is_table(value)
return type(value) == 'table'
end

function M.merge(t1, t2)
if not t1 then
return t2 or {}
elseif not t2 then
return t1
end
return vim.tbl_deep_extend('force', t1, t2)
end

function M.merge_inplace(t1, t2)
-- clone values
for k, v in pairs(t1) do
Expand All @@ -41,8 +50,6 @@ function M.merge_inplace(t1, t2)
t1[k] = v
end
end

return t1
end

function M.hex_to_rgb(str)
Expand Down

0 comments on commit 8c8fff4

Please sign in to comment.