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

Replace defaultTo with undefined check in pathOr #2888

Conversation

Tworley96
Copy link

No description provided.

Copy link
Member

@Bradcomp Bradcomp left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this @Tworley96! It looks good!

I made a couple of comments, I think we should iron out what the intended functionality is on this.

test/propOr.js Outdated Show resolved Hide resolved
@@ -23,6 +23,7 @@ import path from './path';
* R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A"
*/
var pathOr = _curry3(function pathOr(d, p, obj) {
return defaultTo(d, path(p, obj));
var val = path(p, obj)
return val === undefined ? d : val
});
Copy link
Member

Choose a reason for hiding this comment

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

Can you please clean up the linting errors in this file. You can test it by running npm run lint

@CrossEye
Copy link
Member

CrossEye commented Sep 1, 2019

I think what we have documented is what we probably want to support, even if that breaks backward compatibility: [prop|path]Or should return whatever value is there at the property -- even if it's a nil value -- so long as there is such a named property, and the default value otherwise.

That this is different from defaultTo doesn't bother me at all; that one is explicitly about handling nil values.

Copy link
Author

@Tworley96 Tworley96 left a comment

Choose a reason for hiding this comment

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

I cleaned up the linting errors but had to redesign some things somewhat significantly to meet the new requirements. I did not fully clean up the code because I was not sure if this is the correct path, if it is I will make the code more organized. If paths is to continue to be used as the base for prop,path, propOr, and pathOr it needs to throw an error as simply returning undefined does not give propOr an pathOr enough information to determine whether or not to default. Similarly with using nth in paths. nth doesn't differentiate between returning undefined in case of finding it at the index in the list versus the index exceding the bounds of the list. I'd just like to double check that this is the desired behavior because it doesn't seem to necessarily 'fit' with the behavior of other library functions (see nth). I could also see the logic in saying { a: undefined } is not a supported case because undefined is supposed to be a seminal value.

On a somewhat unrelated note, I found another behavior that I presumed was a bug so I went ahead and accounted for in my changes, but I can change that if it is intended behavior. https://ramdajs.com/repl/?v=0.26.1#?R.path%28%5B1%5D%2C%20%27Alice%27%29

@@ -54,8 +54,8 @@ describe('path', function() {
eq(R.path(['a', 'b'], {a: null}), undefined);
});

it('works with falsy items', function() {
eq(R.path(['toString'], false), Boolean.prototype.toString);
it('returns undefined for non-object items', function() {
Copy link
Author

Choose a reason for hiding this comment

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

toString in false errors, so this test no longer makes sense

@Tworley96
Copy link
Author

It's been awhile but I believe that this has fixed all of the stated issues. If not let me know.

@CrossEye CrossEye merged commit 5b0ea82 into ramda:master Mar 5, 2020
@CrossEye
Copy link
Member

CrossEye commented Mar 5, 2020

Yes, it does. Sorry this got dropped. Thanks for the contribution! 🌿

@Bradcomp
Copy link
Member

Bradcomp commented Mar 8, 2020

@Tworley96 @CrossEye I've reverted this merge because it breaks our test suite. Can we get an update so we can add this to the codebase?

@Tworley96
Copy link
Author

I've updated the code so that the test suite passes in #2995. The code that was reverted will undo some of the optimizations in #2962. Personally, I think some of these changes might not be the best and there might be some more discussion to be had about how these functions handle null vs undefined.

@CrossEye
Copy link
Member

CrossEye commented Mar 9, 2020

I've reverted this merge because it breaks our test suite.

Weird, how did I miss that!?

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.

None yet

3 participants