Open In App

Lodash _.wrap() Method

Last Updated : 03 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.wrap() method of Function in lodash is used to create a function that delivers value to the stated wrapper like its initial argument. Moreover, any further arguments delivered to the function are added to those which are delivered to the stated wrapper.

Note:

The wrapper used here is called the binding of the function formed.

Syntax:

_.wrap(value, [wrapper=identity]);

Parameters:

  • value: It is the value to be wrapped.
  • wrapper: It is the wrapper function.

Return Value:

This method returns the new function.

Example 1: In this example, we are using the lodash _.wrap() method passing the escape method and a custom function to it, and printing the result in the console.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling wrap() method with its parameter
let res = _.wrap(_.escape, function (functn, txt) {
    return '<b>' + functn(txt) + '</b>';
});
 
// Assigning values
console.log(res('GfG, geeks, & GeeksforGeeks'));


Output:

<b>GfG, geeks, &amp; GeeksforGeeks</b>

Example 2: In this example, we are using the lodash _.wrap() method passing the upperCase method and a custom function to it, and printing the result in the console.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling wrap() method with its parameter
let newfn = _.wrap(_.upperCase, function (x, y) {
    return x(y)
});
 
// Assigning values
console.log(newfn("geeksforgeeks"));


Output:

GEEKSFORGEEKS

Reference: https://lodash.com/docs/4.17.15#wrap


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads