change path functions to operate on lists of strings#876
change path functions to operate on lists of strings#876davidchambers merged 1 commit intoramda:masterfrom
Conversation
ff01db7 to
bbc9e07
Compare
There was a problem hiding this comment.
What should happen if path is []?
There was a problem hiding this comment.
I'm thinking a no-op, just returning the obj supplied.
There was a problem hiding this comment.
It's a question of philosophy. One can make an argument for either approach. One could argue that _assocPath (and by extension assocPath) should always return an object to which no one holds a reference. On the other hand one could argue that since Ramda promotes programming without mutation, cloning is unnecessary.
There was a problem hiding this comment.
We should also consider the current behaviour of R.path:
assert.strictEqual(R.path('', obj), undefined);
There was a problem hiding this comment.
undefined is what you get for path('x', {y: 1}), yes? Seems like the same situation, analogous to find. Another unsafe function, to be sure
There was a problem hiding this comment.
On the other hand one could argue that since Ramda promotes programming without mutation, cloning is unnecessary.
That's the argument I would make. I don't see a reason to treat the top-level object any different from the sub-object properties, which are references.
|
we should also update |
|
Is this the first internal recursion in Ramda? We've been pretty gun-shy about it after Eweda's stack problems. I don't mind here. The paths should never be long enough to worry us, but we need to note it and be wary. This should close another issue that's been open for a while, but I'm too tired to try to find it. |
bbc9e07 to
2b874e3
Compare
Good catch.
|
test/path.js
Outdated
There was a problem hiding this comment.
We should agree on how this should evaluate.
There was a problem hiding this comment.
seems to me to be the same case as this: assert.strictEqual(R.path(['j', '1'], obj), undefined);
yes. do we need to deprecate first? |
I don't think so. We're making breaking changes to |
2b874e3 to
d583642
Compare
|
agreed. this change has been in the air for a while anyways |
|
Just curious, why is this preferred? |
e.g. imagine you are modeling a filesystem as an object, you could have keys with '.' in them. |
|
🐄 |
d583642 to
32a001c
Compare
There was a problem hiding this comment.
I don't love the obj == null guard, but removing it is a matter for a separate pull request.
good point |
32a001c to
bfb4136
Compare
There was a problem hiding this comment.
_pickBy is no longer used (except in pickBy).
|
This pull request is ready for a final review. |
|
🐄 |
change path functions to operate on lists of strings
|
It's too bad you did not let users using a path as a string to maintain backward compatibility and for simplicity (Ramda could internally split it on "dot" if an argumet was a string). Sometimes it's so much easier and shorter to be able to specify "a.b.c.d" instead of awkward array notation (which is in itself useful some other times). |
|
TiIt's actually for reasons of simplicity (as opposed to those of ease) that we choose for our API not to do such different things on different input types. However I'm with you on how nice the string-based API was compared to the array-based one. But I couldn't argue against the additional flexibility of the current approach. Luckily, it should be easy to write your own version on top of the existing one. |
|
I wish I had convinced you to make an exception, for the sake of "a practical" adjective describing the library at the front page :) |
I discuss the notion of what we mean by "practical" in The Philosophy of Ramda and further in #1002 (comment) . I think there's a good reason not to do what you're suggesting, even though I initially argued for it as well, and even though we did it that way at first. A number of our functions are dynamically dispatched, and we internally supply the version that works for lists or for plain objects. And we offer tranduced versions of many of our functions. But other than that, we don't vary our behavior based on argument type. I think that's a pretty clean line to draw. But creating a version that works the way you want should be as simple as: |
Closes #637
This pull request introduces four new internal functions:
_assoc,_assocPath,_dissoc, and_dissocPath. These are defined with as few dependencies as possible._assoc, for example, has no dependencies whereas the existingassocimplementation depends on_extend,_map,createMapEntry,fromPairs, andkeysIn!Most significantly, this pull request changes the type of the
pathparameter from (dot-separated)Stringto[String], and updates the test suite accordingly.