Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

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

Merged
thelindat merged 3 commits intooverextended:masterfrom
tom-osborne:master
Jan 16, 2025
Merged

fix(table): Respect falsy value for addDuplicateNumbers#672
thelindat merged 3 commits intooverextended:masterfrom
tom-osborne:master

Conversation

@tom-osborne
Copy link
Contributor

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]

@tom-osborne
Copy link
Contributor Author

See also #671

Copy link
Collaborator

@jag3dagster jag3dagster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic checks out @thelindat

@jag3dagster
Copy link
Collaborator

Thank you for the detailed explanation @tom-osborne

---Merges two tables together. Defaults to adding duplicate keys together if they are numbers, otherwise they are overriden.
local function table_merge(t1, t2, addDuplicateNumbers)
addDuplicateNumbers = addDuplicateNumbers ~= nil and addDuplicateNumbers or true
addDuplicateNumbers = addDuplicateNumbers == nil and true or addDuplicateNumbers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addDuplicateNumbers = addDuplicateNumbers == nil or addDuplicateNumbers

Copy link
Contributor Author

@tom-osborne tom-osborne Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelindat Changes made as requested. Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants