Skip to content

Commit

Permalink
feat(standard): Add Logic.nilIfEmpty (#4221)
Browse files Browse the repository at this point in the history
* feat(standard): Add `Logic.nilIfEmpty`

* testcases `Logic.nilIfEmpty`

* as per review
  • Loading branch information
hjpalpha authored Apr 25, 2024
1 parent fb4d0a3 commit ff53816
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions spec/logic_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ describe('logic', function()
end)
end)

describe('NilIfEmpty', function()
it('check', function()
assert.is_nil(Logic.nilIfEmpty({}))
assert.is_nil(Logic.nilIfEmpty())
assert.is_nil(Logic.nilIfEmpty(''))
assert.are_same({''}, Logic.nilIfEmpty({''}))
assert.are_same({'string'}, Logic.nilIfEmpty({'string'}))
assert.are_same({{}}, Logic.nilIfEmpty({{}}))
assert.are_same(1, Logic.nilIfEmpty(1))
assert.are_same('string', Logic.nilIfEmpty('string'))
end)
end)

describe('IsDeepEmpty', function()
it('check', function()
assert.is_true(Logic.isDeepEmpty({}))
Expand Down
7 changes: 7 additions & 0 deletions standard/logic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ function Logic.isNotEmpty(val)
end
end

---@param val table|string?
---@return table|string?
function Logic.nilIfEmpty(val)
return Logic.isNotEmpty(val) and val or nil
end


---Checks if a given object (table|string|nil) is deep empty
---i.e. is empty itself or only contains objects that are deep empty
---@param val table|string|nil
Expand Down

0 comments on commit ff53816

Please sign in to comment.