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

Incorrect comparison of Boolean values #51

Closed
karelkryda opened this issue Sep 4, 2022 · 2 comments
Closed

Incorrect comparison of Boolean values #51

karelkryda opened this issue Sep 4, 2022 · 2 comments
Assignees

Comments

@karelkryda
Copy link

Package's name
object-fill-missing-keys
object-merge-advanced

Describe the bug
When comparing two objects using the "object-fill-missing-keys" library with different Boolean values, the values are replaced despite the fact that the value is not missing (is true or false).

If one object is false and the other object is true, true is always returned.

To Reproduce
Steps to reproduce the behavior:

  1. Create one object with false value and one object with true value
  2. Fill object with fillMissing(objectOne, objectTwo)
  3. See output
  4. Object with true value is returned from method

Expected behavior
If the value is true or false (not missing in the object), the original value of the object should always be returned, regardless of the boolean value in the schema (the object according to which the first object fills in the missing values).

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
An error occurs in the "object-merge-advanced" library when comparing Boolean values on this line.

If I use the following code, the Boolean values are returned correctly according to the first (original) object.

var _currentResult25 = uni ? uniRes : i2;
return _currentResult25;
@revelt
Copy link
Member

revelt commented Sep 4, 2022

Hi Karel, sorry about that; I'll investigate.

@revelt revelt closed this as completed in 1830aff Oct 16, 2022
@revelt
Copy link
Member

revelt commented Oct 16, 2022

Hi Karel,

Sorry for delay, at first I thought I'll have to recode the object-fill-missing-keys from scratch but with fresh head this morning I've been able to sort this.
I've been using this package to merge JSON data sources for email templates and they follow the data hierarchy (seen in https://codsen.com/os/object-merge-advanced#purpose). I'd prepare schema, and its placeholders would always be boolean false — that's lower in the "food chain" than anything on the source. Technically, this was not a bug for me, but it was for everybody, and this program didn't actually deliver what it promised — besides filling the values it was overwriting source with schema's value if the source was lower in the "values food chain" — like you rightly discovered with { foo: false } vs. schema { foo: true } merges.

The fix was a three-liner, I used object-merge-advanced callback to conditionally merge — if types of schema and original are matching, original is kept. Otherwise, we leave it up to object-merge-advanced to decide (<v.10, original behaviour).

if (typ(inputArg1) === typ(inputArg2)) {
return inputArg2;
}

I've just fixed it and released a major bump, v10 please try.

Again, sorry for the bug and thanks for spotting this!

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

No branches or pull requests

2 participants