Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): Respect falsy value for addDuplicateNumbers #672

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tom-osborne
Copy link

The logic for the third parameter (addDuplicateNumbers) does not respect a falsy value.

addDuplicateNumbers = addDuplicateNumbers ~= nil and addDuplicateNumbers or true

When a value of false is passed the logic is resolved as follows:

  1. addDuplicateNumbers ~= nil resolves to true
  2. addDuplicateNumbers ~= nil and addDuplicateNumbers is therefore the same as true and false which resolves to false
  3. addDuplicateNumbers ~= nil and addDuplicateNumbers or true is therefore the same a false or true, which resolves to true

So a falsy value always coerces to true.

Simple test script:

local t1 = { 1 }
local t2 = { 1 }

local tmerged = lib.table.merge(t1, t2, false)
print("Merged", json.encode(tmerged))
--------
-- Output:
-- Merged   [2]

Proposed solution is to reverse the logic somewhat so it sets the value to true only when nil.

addDuplicateNumbers = addDuplicateNumbers == nil and true or addDuplicateNumbers

Running the same test script:

-- Output:
-- Merged   [1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant