lodash/fp Documentation

website·Indexed 30 days ago

https://lodash-fp-docs.vercel.app

Documentation 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.

Tokens
53.6K
Snippets
322
Records
523
Agent score
74%

What's inside lodash/fp

  1. 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.
  2. Intersect multiple arrays using a custom comparator with _.intersectionWith

    Use _.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 }]
  3. Create a chainable lodash wrapper with `_.chain`

    Use _.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'
  4. Sort a collection with multiple criteria using _.orderBy

    The _.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]]
  5. Unwrap a lodash wrapper using `.value()`

    When a value is wrapped (either via _.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)
  6. Configure the template data object variable name with _.templateSettings.variable

    The _.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!'
  7. Reference for _.assignIn arguments and return value

    _.assignIn(object, sources) takes the following parameters:

    1. object (Object): The destination object to be modified.
    2. sources (Object|Object[]): The source object or array of source objects to copy properties from.

    Returns the modified destination object.

  8. Reference parameters for _.takeRight

    _.takeRight accepts two arguments:

    1. n (number): The number of elements to take from the end of the array.
    2. array (Array): The array to query.

    Returns: (Array) The resulting slice of the array.

  9. Import variables into compiled templates using _.templateSettings.imports

    The _.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 };
  10. Reference for _.assign parameters and return value

    _.assign accepts the following arguments:

    1. object (Object): The destination object that will receive the properties.
    2. sources (Object|Object[]): The source object or array of source objects to copy properties from.

    Returns: The modified destination object.

  11. Configure custom template delimiters with _.templateSettings

    Use the _.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);