Load a single lodash methods for smaller builds with browserify/rollup/webpack

Viewed 1490

From https://lodash.com/:

// Load a single method for smaller builds with browserify/rollup/webpack.
var chunk = require('lodash/chunk');
var extend = require('lodash/fp/extend');

This works well with most of the methods: each, map, isArray, etc. The one method I connot get working is lodash/chain.

The current code where I import the whole lodash library goes something like this:

_.chain(items)
  .filter(...)
  .groupBy(...)
  .map(...)
  .concat(...)
  .value();

What would be the proper way of creating a proper chainable object that does not contain all methods included in the lodash build? The chain method creates a lodash wrapper object and returns it. I could create my own chain method like this

var lodash = require('lodash/wrapperLodash');
var filter = require('lodash/filter');
var map = require('lodash/map');

function chain(value) {
  var result = lodash(value);
  result.__chain__ = true;
  result.filter = filter
  result.map = map;
  return result;
}

module.exports = chain;

Now the call to chain will be able to execute filter and map. Unfortunately, the result from chain().filter will not have the methods I have attached in the original chain. What is the proper way of creating a custom chainable lodash object?

1 Answers
Related