Skip to content

Path customization #2877

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

Merged
merged 4 commits into from
Sep 21, 2020
Merged

Conversation

jgonggrijp
Copy link
Collaborator

This is the first follow-up on documentcloud/underscore-contrib#231. The next (and final) follow-up will be a PR to Contrib.

I should emphasize that the behavior of paths in Underscore stays the same. That is, full-blown array paths (['a', 0, 'b']) remain supported while shorthand string paths ('a.0.b') remain unsupported.

The purpose of this PR is to add a customization point, _.toPath, which users can override, similar to _.iteratee. The most obvious application of such an override would be to enable string paths, though in principle, users can do something completely different. _.toPath is used internally throughout Underscore.

The follow-up PR to Contrib will add a function to its public interface, which users can optionally use to override _.toPath. Using that optional override will cause all functions in Underscore and Contrib to support string paths in a uniform way. As long as users don't do this, all functions in Underscore and Contrib will only support array paths. This will be a slightly breaking change for Contrib, but it will be a major step up in consistency between the libraries as well as within Contrib. The future Fusion library will use this mechanism, too, in order to support legacy code that depends on Lodash. Assuming the PR gets approved, of course.

This PR also adds one other function, _.get, which could be regarded a dual to _.has as well as a simpler counterpart to _.result that doesn't invoke methods. This function makes it easier to inherit Underscore's deep property extraction logic in Contrib and Fusion.

Some more functional relations:

_.get = _.compose(deepGet, _.toPath); // if we ignore _.get's optional third argument
_.propertyOf(obj) = _.partial(_.get, obj);
_.property(path) = _.partial(_.get, _, path);

@jashkenas Please let me know if anything is unclear.

@joshuacc FYI.

@jgonggrijp jgonggrijp added enhancement contrib probably belongs in Underscore-contrib labels Sep 21, 2020
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 96.296% when pulling f03f3ac on jgonggrijp:path-customization into 7ee13c8 on jashkenas:master.

@coveralls
Copy link

coveralls commented Sep 21, 2020

Coverage Status

Coverage increased (+0.02%) to 96.296% when pulling 982a0ec on jgonggrijp:path-customization into 7ee13c8 on jashkenas:master.

@jgonggrijp
Copy link
Collaborator Author

Thanks for the quick review and approval, @jashkenas!

@jgonggrijp jgonggrijp merged commit f5a9774 into jashkenas:master Sep 21, 2020
@jgonggrijp jgonggrijp deleted the path-customization branch September 21, 2020 15:49
Copy link

@joshuacc joshuacc left a comment

Choose a reason for hiding this comment

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

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contrib probably belongs in Underscore-contrib enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants