wrap.js 871 B

12345678910111213141516171819202122232425262728293031
  1. var castFunction = require('./_castFunction'),
  2. partial = require('./partial');
  3. /**
  4. * Creates a function that provides `value` to `wrapper` as its first
  5. * argument. Any additional arguments provided to the function are appended
  6. * to those provided to the `wrapper`. The wrapper is invoked with the `this`
  7. * 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. return partial(castFunction(wrapper), value);
  27. }
  28. module.exports = wrap;