wrap.js 1.0 KB

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