Lodash: Chaining in per-method build

Created on 14 Feb 2015  路  3Comments  路  Source: lodash/lodash

When using lodash from separate packages, it's not possible to chain functions - it's clear and obvious. But without chaining, complex operations get more and more unreadable, so usually I use helpers like this one. I wonder if there is a place in lodash for util like that? I mean, in per-module build? I can imagine it working exactly like chaining functions in "full" library (without passing arguments to interceptor).

var chain = require('lodash.chain');
var assign = require('lodash.assign');
var groupBy = require('lodash.groupby');
var pick = require('lodash.pick');

chain([ 42 ])
  .thru((v) => groupBy(v))
  .thru((v) => assign(v, { "24": "24" }))
  .tap((v) => console.log(v))
  .thru((v) => pick(v, '42'))
  .value()

Do you find it can be useful?

question

Most helpful comment

Would it make sense to make that code a lodash module? That's a lot of boilerplate.
To enable a simpler API, e.g.:

```js
const _ = require('lodash/chainHelper')({capitalize, groupBy});

All 3 comments

Individual packages like lodash.assign won't likely get chaining. I could maybe see the chain category module, require('lodash/chain'), being used to add such sugar though not sure exactly how it would know what to add to be chainable. Related #920.

Using the monolithic module index.js for reference, you can recreate the chaining sugar using require('lodash/foo') modules. For example:

var LodashWrapper = require('lodash/_LodashWrapper'),
    lodash = require('lodash/_wrapperLodash')
    baseForOwn = require('lodash/_baseForOwn'),
    mixin = require('lodash/mixin');

// Ensure `new LodashWrapper` is an instance of `lodash`.
LodashWrapper.prototype = lodash.prototype;

// Add functions that return wrapped values when chaining.
lodash.chunk = require('lodash/chunk');
// ...

// Add functions to `lodash.prototype`.
mixin(lodash, lodash);

// Add functions that return unwrapped values when chaining.
lodash.capitalize = require('lodash/capitalize');
// ...

mixin(lodash, (function() {
  var source = {};
  baseForOwn(lodash, function(func, methodName) {
    if (!lodash.prototype[methodName]) {
      source[methodName] = func;
    }
  });
  return source;
}()), false);

// Add chaining functions to the lodash wrapper.
var chain = require('lodash/chain');
lodash.prototype.chain = chain.wrapperChain;
lodash.prototype.value = chain.value;

Would it make sense to make that code a lodash module? That's a lot of boilerplate.
To enable a simpler API, e.g.:

```js
const _ = require('lodash/chainHelper')({capitalize, groupBy});

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sofianito picture sofianito  路  3Comments

sarahquigley picture sarahquigley  路  3Comments

Alexsey picture Alexsey  路  3Comments

gajus picture gajus  路  3Comments

thoraage picture thoraage  路  3Comments