wrap.js 916 B

12345678910111213141516171819202122232425262728293031
  1. var identity = require('./identity'),
  2. partial = require('./partial');
  3. /**
  4. * Creates a function that provides `value` to the wrapper function as its
  5. * first argument. Any additional arguments provided to the function are
  6. * appended to those provided to the wrapper function. The wrapper is invoked
  7. * with the `this` binding of the created function.
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 0.1.0
  12. * @category Function
  13. * @param {*} value The value to wrap.
  14. * @param {Function} [wrapper=identity] The wrapper function.
  15. * @returns {Function} Returns the new function.
  16. * @example
  17. *
  18. * var p = _.wrap(_.escape, function(func, text) {
  19. * return '<p>' + func(text) + '</p>';
  20. * });
  21. *
  22. * p('fred, barney, & pebbles');
  23. * // => '<p>fred, barney, &amp; pebbles</p>'
  24. */
  25. function wrap(value, wrapper) {
  26. wrapper = wrapper == null ? identity : wrapper;
  27. return partial(wrapper, value);
  28. }
  29. module.exports = wrap;