Overview of lodash/fp
lodash/fp is a version of the Lodash library designed for pure functional programming. It provides the same utility functionality as standard Lodash but is optimized for functional patterns, such as immutability and currying.
website·Indexed 30 days ago
https://lodash-fp-docs.vercel.appDocumentation for lodash/fp, a functional programming wrapper for the Lodash library. It provides the same functionality as Lodash but with a functional approach, featuring utilities for piping, flowing, and data manipulation across categories such as Object, String, Number, and Util.
_.intersectionWith to create an array of unique values that are present in all provided arrays. Unlike _.intersection, which uses strict equality, _.intersectionWith accepts a comparator function to determine if two elements match. The comparator is invoked with two arguments: (arrVal, othVal). The order and references of the resulting values are determined by the first array provided.var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
// Use _.isEqual as the comparator to compare object contents
_.intersectionWith(_.isEqual, objects, others);
// => [{ 'x': 1, 'y': 2 }]_.chain(value) to create a lodash wrapper instance around a value. This enables explicit method chaining, allowing you to sequence multiple lodash operations. To retrieve the final result from a chain, you must call the .value() method.var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
var youngest = _
.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
.value();
// => 'pebbles is 1'_.orderBy method sorts a collection based on a set of iteratees and corresponding sort orders. It is similar to _.sortBy, but provides explicit control over whether each single criterion is sorted in ascending ('asc') or descending ('desc') order. If the orders argument is omitted, all values are sorted in ascending order by default. The function returns a new sorted array.var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 34 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 36 }
];
// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(['user', 'age'], ['asc', 'desc'], users);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]_.chain() or by calling a chainable method), it is no longer a plain JavaScript type (e.g., it is not a plain Array). To convert the wrapped object back into its underlying value, call the .value() method.function square(n) { return n * n; }
var wrapped = _([1, 2, 3]);
var squares = wrapped.map(square);
console.log(_.isArray(squares)); // => false (it is a wrapper)
console.log(_.isArray(squares.value())); // => true (unwrapped to array)_.templateSettings.variable property is a string used to define the name of the variable that references the data object within the template text. This allows you to customize the identifier used to access passed-in data when rendering templates.// Example: Change the default data object variable to 'data'
_.templateSettings.variable = 'data';
// Now the template can reference 'data' instead of the default
const compiled = _.template('Hello ${data.name}!');
compiled({ name: 'World' }); // 'Hello World!'_.assignIn(object, sources) takes the following parameters:
object (Object): The destination object to be modified.sources (Object|Object[]): The source object or array of source objects to copy properties from.Returns the modified destination object.
_.unset takes two arguments:
path (Array|string): The path of the property to be removed from the object.object (Object): The target object to modify._.takeRight accepts two arguments:
n (number): The number of elements to take from the end of the array.array (Array): The array to query.Returns: (Array) The resulting slice of the array.
_.templateSettings.imports object is used to specify variables that should be imported and made available within the scope of a compiled template.// Example: Adding a custom variable to all templates
_.templateSettings.imports = { // variables to import };_.assign accepts the following arguments:
object (Object): The destination object that will receive the properties.sources (Object|Object[]): The source object or array of source objects to copy properties from.Returns: The modified destination object.
_.templateSettings object to change the default template delimiters used by lodash. By default, lodash uses delimiters similar to those found in embedded Ruby (ERB) and ES2015 template strings.// Example: Accessing template settings
console.log(_.templateSettings);