mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
135 lines
3.6 KiB
Plaintext
135 lines
3.6 KiB
Plaintext
## lodash/fp
|
||
|
||
The `lodash/fp` module is an instance of `lodash` with its methods wrapped to
|
||
produce immutable auto-curried iteratee-first data-last methods.
|
||
|
||
## Installation
|
||
|
||
In a browser:
|
||
```html
|
||
<script src='path/to/lodash.js'></script>
|
||
<script src='path/to/lodash.fp.js'></script>
|
||
```
|
||
|
||
In Node.js:
|
||
```js
|
||
// Load the fp build.
|
||
var fp = require('lodash/fp');
|
||
|
||
// Load a method category.
|
||
var object = require('lodash/fp/object');
|
||
|
||
// Load a single method for smaller builds with browserify/rollup/webpack.
|
||
var extend = require('lodash/fp/extend');
|
||
```
|
||
|
||
## Convert
|
||
|
||
Although `lodash/fp` & its method modules come pre-converted, there are times
|
||
when you may want to customize the conversion. That’s when the `convert` method
|
||
comes in handy.
|
||
```js
|
||
// Every option is `true` by default.
|
||
var mapValues = fp.mapValues.convert({
|
||
// Specify capping iteratee arguments.
|
||
'cap': true,
|
||
// Specify currying.
|
||
'curry': true,
|
||
// Specify fixed arity.
|
||
'fixed': true,
|
||
// Specify immutable operations.
|
||
'immutable': true,
|
||
// Specify rearranging arguments.
|
||
'rearg': true
|
||
});
|
||
|
||
// Disable capping of iteratee arguments to access the `key` param.
|
||
var mapValuesWithKey = fp.mapValues.convert({ 'cap': false });
|
||
|
||
mapValuesWithKey(function(value, key) {
|
||
return key == 'a' ? -1 : value;
|
||
})({ 'a': 1, 'b': 1 });
|
||
// => { 'a': -1, 'b': 1 }
|
||
```
|
||
|
||
It’s also possible to use the convert module directly for manual conversions.
|
||
```js
|
||
var convert = require('lodash/fp/convert');
|
||
|
||
// Convert by name.
|
||
var assign = convert('assign', require('lodash.assign'));
|
||
|
||
// Convert by object.
|
||
var fp = convert({
|
||
'assign': require('lodash.assign'),
|
||
'chunk': require('lodash.chunk')
|
||
});
|
||
|
||
// Convert by `lodash` instance.
|
||
var fp = convert(lodash.runInContext());
|
||
```
|
||
|
||
## Mapping
|
||
|
||
Immutable auto-curried iteratee-first data-last methods sound great, but what
|
||
does that really mean for each method? Below is a breakdown of the mapping used
|
||
to convert each method.
|
||
|
||
#### Capped Iteratee Arguments
|
||
|
||
Methods that cap iteratees to one argument:<br>
|
||
<%= toFuncList(_.keys(_.pickBy(mapping.iterateeAry, _.partial(_.eq, _, 1)))) %>
|
||
|
||
Methods that cap iteratees to two arguments:<br>
|
||
<%= toFuncList(_.keys(_.pickBy(mapping.iterateeAry, _.partial(_.eq, _, 2)))) %>
|
||
|
||
The iteratee of `mapKeys` is invoked with one argument: (key)
|
||
|
||
#### Fixed Arity
|
||
|
||
Methods with a fixed arity of one:<br>
|
||
<%= toFuncList(mapping.aryMethod[1]) %>
|
||
|
||
Methods with a fixed arity of two:<br>
|
||
<%= toFuncList(mapping.aryMethod[2]) %>
|
||
|
||
Methods with a fixed arity of three:<br>
|
||
<%= toFuncList(mapping.aryMethod[3]) %>
|
||
|
||
Methods with a fixed arity of four:<br>
|
||
<%= toFuncList(mapping.aryMethod[4]) %>
|
||
|
||
#### Rearranged Arguments
|
||
|
||
Methods with a fixed arity of two have an argument order of:<br>
|
||
<%= toArgOrder(mapping.aryRearg[2]) %>
|
||
|
||
Methods with a fixed arity of three have an argument order of:<br>
|
||
<%= toArgOrder(mapping.aryRearg[3]) %>
|
||
|
||
Methods with a fixed arity of four have an argument order of:<br>
|
||
<%= toArgOrder(mapping.aryRearg[4]) %>
|
||
|
||
Methods with custom argument orders:<br>
|
||
<%= _.map(mapping.methodRearg, function(orders, methodName) {
|
||
return ' * `_.' + methodName + '` has an order of ' + toArgOrder(orders);
|
||
}).join('\n') %>
|
||
|
||
Methods with unchanged argument orders:<br>
|
||
<%= toFuncList(_.keys(mapping.skipRearg)) %>
|
||
|
||
The methods `partial` & `partialRight` accept an array of arguments to partially
|
||
apply as their second parameter.
|
||
|
||
#### New Methods
|
||
|
||
Methods created to accommodate Lodash’s variadic methods:<br>
|
||
<%= toFuncList(_.keys(mapping.remap)) %>
|
||
|
||
#### Aliases
|
||
|
||
There are <%= _.size(mapping.aliasToReal) %> method aliases:<br>
|
||
<%= _.map(mapping.aliasToReal, function(realName, alias) {
|
||
return ' * `_.' + alias + '` is an alias of `_.' + realName + '`';
|
||
}).join('\n') %>
|