3 The `lodash/fp` module is an instance of `lodash` with its methods wrapped to
4 produce immutable auto-curried iteratee-first data-last methods.
10 <script src='path/to/lodash.js'></script>
11 <script src='path/to/lodash.fp.js'></script>
13 // Loading `lodash.fp.js` converts `_` to its fp variant.
14 _.defaults({ 'a': 2, 'b': 2 })({ 'a': 1 });
15 // → { 'a: 1, 'b': 2 }
17 // Use `noConflict` to restore the pre-fp variant.
18 var fp = _.noConflict();
20 _.defaults({ 'a': 1 }, { 'a': 2, 'b': 2 });
21 // → { 'a: 1, 'b': 2 }
22 fp.defaults({ 'a': 2, 'b': 2 })({ 'a': 1 });
23 // → { 'a: 1, 'b': 2 }
30 var fp = require('lodash/fp');
32 // Load a method category.
33 var object = require('lodash/fp/object');
35 // Load a single method for smaller builds with browserify/rollup/webpack.
36 var extend = require('lodash/fp/extend');
41 Immutable auto-curried iteratee-first data-last methods sound great, but what
42 does that really mean for each method? Below is a breakdown of the mapping used
43 to convert each method.
45 #### Capped Iteratee Arguments
47 Iteratee arguments are capped to avoid gotchas with variadic iteratees.
49 // The `lodash/map` iteratee receives three arguments:
50 // (value, index|key, collection)
51 _.map(['6', '8', '10'], parseInt);
54 // The `lodash/fp/map` iteratee is capped at one argument:
56 fp.map(parseInt)(['6', '8', '10']);
60 Methods that cap iteratees to one argument:<br>
61 <%= toFuncList(_.keys(_.pickBy(mapping.iterateeAry, _.partial(_.eq, _, 1)))) %>
63 Methods that cap iteratees to two arguments:<br>
64 <%= toFuncList(_.keys(_.pickBy(mapping.iterateeAry, _.partial(_.eq, _, 2)))) %>
66 The iteratee of `mapKeys` is invoked with one argument: (key)
70 Methods have fixed arities to support auto-currying.
72 // `lodash/padStart` accepts an optional `chars` param.
73 _.padStart('a', 3, '-')
76 // `lodash/fp/padStart` does not.
79 fp.padCharsStart('-')(3)('a');
83 Methods with a fixed arity of one:<br>
84 <%= toFuncList(_.difference(mapping.aryMethod[1], _.keys(mapping.skipFixed))) %>
86 Methods with a fixed arity of two:<br>
87 <%= toFuncList(_.difference(mapping.aryMethod[2], _.keys(mapping.skipFixed))) %>
89 Methods with a fixed arity of three:<br>
90 <%= toFuncList(_.difference(mapping.aryMethod[3], _.keys(mapping.skipFixed))) %>
92 Methods with a fixed arity of four:<br>
93 <%= toFuncList(_.difference(mapping.aryMethod[4], _.keys(mapping.skipFixed))) %>
95 #### Rearranged Arguments
97 Method arguments are rearranged to make composition easier.
99 // `lodash/filter` is data-first iteratee-last:
100 // (collection, iteratee)
101 var compact = _.partial(_.filter, _, Boolean);
102 compact(['a', null, 'c']);
105 // `lodash/fp/filter` is iteratee-first data-last:
106 // (iteratee, collection)
107 var compact = fp.filter(Boolean);
108 compact(['a', null, 'c']);
112 ##### Most methods follow these rules
114 A fixed arity of two has an argument order of:<br>
115 <%= toArgOrder(mapping.aryRearg[2]) %>
117 A fixed arity of three has an argument order of:<br>
118 <%= toArgOrder(mapping.aryRearg[3]) %>
120 A fixed arity of four has an argument order of:<br>
121 <%= toArgOrder(mapping.aryRearg[4]) %>
123 ##### Exceptions to the rules
125 Methods that accept an array of arguments as their second parameter:<br>
126 <%= toFuncList(_.keys(mapping.methodSpread)) %>
128 Methods with unchanged argument orders:<br>
129 <%= toFuncList(_.keys(mapping.skipRearg)) %>
131 Methods with custom argument orders:<br>
132 <%= _.map(_.keys(mapping.methodRearg), function(methodName) {
133 var orders = mapping.methodRearg[methodName];
134 return ' * `_.' + methodName + '` has an order of ' + toArgOrder(orders);
139 Not all variadic methods have corresponding new method variants. Feel free to
140 [request](https://github.com/lodash/lodash/blob/master/.github/CONTRIBUTING.md#feature-requests)
143 Methods created to accommodate Lodash’s variadic methods:<br>
144 <%= toFuncList(_.keys(mapping.remap)) %>
148 There are <%= _.size(mapping.aliasToReal) %> method aliases:<br>
149 <%= _.map(_.keys(mapping.aliasToReal).sort(), function(alias) {
150 var realName = mapping.aliasToReal[alias];
151 return ' * `_.' + alias + '` is an alias of `_.' + realName + '`';
156 The placeholder argument, which defaults to `_`, may be used to fill in method
157 arguments in a different order. Placeholders are filled by the first available
158 arguments of the curried returned function.
160 // The equivalent of `2 > 5`.
164 // The equivalent of `_.gt(5, 2)` or `5 > 2`.
171 The `lodash/fp` module **does not** convert chain sequence methods. See
172 [Izaak Schroeder’s article](https://medium.com/making-internets/why-using-chain-is-a-mistake-9bc1f80d51ba)
173 on using functional composition as an alternative to method chaining.
177 Although `lodash/fp` & its method modules come pre-converted, there are times
178 when you may want to customize the conversion. That’s when the `convert` method
181 // Every option is `true` by default.
182 var _fp = fp.convert({
183 // Specify capping iteratee arguments.
187 // Specify fixed arity.
189 // Specify immutable operations.
191 // Specify rearranging arguments.
195 // The `convert` method is available on each method too.
196 var mapValuesWithKey = fp.mapValues.convert({ 'cap': false });
198 // Here’s an example of disabling iteratee argument caps to access the `key` param.
199 mapValuesWithKey(function(value, key) {
200 return key == 'a' ? -1 : value;
201 })({ 'a': 1, 'b': 1 });
202 // => { 'a': -1, 'b': 1 }
205 Manual conversions are also possible with the `convert` module.
207 var convert = require('lodash/fp/convert');
210 var assign = convert('assign', require('lodash.assign'));
212 // Convert by object.
214 'assign': require('lodash.assign'),
215 'chunk': require('lodash.chunk')
218 // Convert by `lodash` instance.
219 var fp = convert(lodash.runInContext());