From f9936b8b5489e3aea8fbdee45ba3e096a3994b57 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 15 Aug 2016 22:00:43 -0700 Subject: [PATCH] Add `setToString` helper. --- lodash.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lodash.js b/lodash.js index 630590a4a..8fe30b59d 100644 --- a/lodash.js +++ b/lodash.js @@ -3781,7 +3781,7 @@ */ function baseRest(func, start) { start = nativeMax(start === undefined ? (func.length - 1) : start, 0); - return function() { + return setToString(function() { var args = arguments, index = -1, length = nativeMax(args.length - start, 0), @@ -3797,7 +3797,7 @@ } otherArgs[start] = array; return apply(func, this, otherArgs); - }; + }, func + ''); } /** @@ -6313,6 +6313,22 @@ return root.setTimeout(func, wait); }; + /** + * Sets the `toString` method of `func` to return `string`. + * + * @private + * @param {Function} func The function to modify. + * @param {Function} string The `toString` result. + * @returns {Function} Returns `func`. + */ + var setToString = !defineProperty ? identity : function(func, string) { + return defineProperty(func, 'toString', { + 'configurable': true, + 'enumerable': false, + 'value': constant(string) + }); + }; + /** * Sets the `toString` method of `wrapper` to mimic the source of `reference` * with wrapper details in a comment at the top of the source body. @@ -6323,14 +6339,10 @@ * @param {number} bitmask The bitmask flags. See `createWrap` for more details. * @returns {Function} Returns `wrapper`. */ - var setWrapToString = !defineProperty ? identity : function(wrapper, reference, bitmask) { + function setWrapToString(wrapper, reference, bitmask) { var source = (reference + ''); - return defineProperty(wrapper, 'toString', { - 'configurable': true, - 'enumerable': false, - 'value': constant(insertWrapDetails(source, updateWrapDetails(getWrapDetails(source), bitmask))) - }); - }; + return setToString(wrapper, insertWrapDetails(source, updateWrapDetails(getWrapDetails(source), bitmask))); + } /** * Converts `string` to a property path array.