Lodash _.wrap() Method

The _.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 with the this binding of the function formed.

Syntax:

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

Parameters: This method accepts two parameters as mentioned above and described below:

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

Return Value: This method returns the new function.



Below examples illustrate the Lodash _.wrap() method in JavaScript:

Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

GEEKSFORGEEKS

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

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.