Lodash _.wrap() Method
Last Updated :
03 Nov, 2023
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
const _ = require( 'lodash' );
let res = _.wrap(_.escape, function (functn, txt) {
return '<b>' + functn(txt) + '</b>' ;
});
console.log(res( 'GfG, geeks, & GeeksforGeeks' ));
|
Output:
<b>GfG, geeks, & 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
const _ = require( 'lodash' );
let newfn = _.wrap(_.upperCase, function (x, y) {
return x(y)
});
console.log(newfn( "geeksforgeeks" ));
|
Output:
GEEKSFORGEEKS
Reference: https://lodash.com/docs/4.17.15#wrap
Share your thoughts in the comments
Please Login to comment...