Skip to content

Simpler Chaining Request #583

Closed
Closed
@machineghost

Description

@machineghost

I realize that underscore has been around for awhile, is widely used, and as such won't be changing its basic chaining approach anytime soon. Despite that, I think that the current chaining model is needlessly verbose, which is why I think it'd be great if there was an option (either a plug-in or some sort of Underscore setting) that allowed the following alternative chain syntax:

(foo).operation1().operation2().;

In other words, this plug-in would:

  • Change the _(foo) usage of underscore to imply a .chain() call
  • When in "chained" mode, underscore would give the return value of any function an "" property, which would be set to the same value that would have been returned had the function not been in "chained" mode
    (This doesn't really have to be "
    "; it could be "$" or "v" or "val" or ... you get the idea; I just thought using underscore for this value seemed appropriate, and it gives the whole line a nice symmetry, as it starts and ends with an underscore).

This alternate syntax would take code like this:

return _(foo).chain().pluck('bars').reduce(reducer, []).compact().value();

or:

return .compact(.reduce(reducer, _.pluck(foo, 'bars')));

and replace it with:

return (foo).pluck('bars').reduce(reducer, []).compact().;

which is both clearer and simpler.

Of course, you could make the argument that if one is doing enough operations to justify chaining (3+ for me, but I'm sure others have different thresholds), the extra .chain() and .value() aren't a huge deal. And they aren't ... but just because something isn't a huge deal doesn't mean it can't be improved :-) I use underscore chaining fairly often (a testament to how useful the library is), and even a minor reduction in verbosity would have a noticeable effect on my code.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions