diff --git a/build.js b/build.js index d412e73e8..446cf3d16 100755 --- a/build.js +++ b/build.js @@ -109,6 +109,7 @@ 'keys': ['isArguments'], 'last': [], 'lastIndexOf': [], + 'lateBind': ['isFunction'], 'map': ['identity'], 'max': [], 'memoize': [], @@ -120,7 +121,7 @@ 'omit': ['indexOf', 'isArguments'], 'once': [], 'pairs': [], - 'partial': [], + 'partial': ['isFunction'], 'pick': [], 'pluck': [], 'random': [], @@ -205,6 +206,7 @@ 'keys', 'last', 'lastIndexOf', + 'lateBind', 'map', 'max', 'min', @@ -230,6 +232,7 @@ 'forIn', 'forOwn', 'invert', + 'lateBind', 'merge', 'object', 'omit', @@ -1052,7 +1055,7 @@ // remove native `Function#bind` branch in `_.bind` if (methodName == 'bind') { - modified = modified.replace(/(?:\s*\/\/.*)*\s*else if *\(isBindFast[^}]+}/, ''); + modified = modified.replace(/(?:\s*\/\/.*)*\s*return isBindFast[^:]+:\s*/, 'return '); } // remove native `Array.isArray` branch in `_.isArray` else { diff --git a/build/pre-compile.js b/build/pre-compile.js index 27b97f1b9..44e3c056e 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -9,6 +9,7 @@ var compiledVars = [ 'argsIndex', 'argsLength', + 'bindIterator', 'callback', 'collection', 'concat', @@ -17,7 +18,6 @@ 'identity', 'index', 'iteratee', - 'iteratorBind', 'length', 'nativeKeys', 'object', @@ -180,6 +180,7 @@ 'keys', 'last', 'lastIndexOf', + 'lateBind', 'map', 'max', 'memoize', diff --git a/test/test-build.js b/test/test-build.js index 5c6840782..16b34ac3a 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -134,6 +134,7 @@ 'debounce', 'defer', 'delay', + 'lateBind', 'memoize', 'once', 'partial', @@ -241,6 +242,7 @@ 'forIn', 'forOwn', 'invert', + 'lateBind', 'merge', 'object', 'omit', @@ -370,14 +372,16 @@ else if (functionsMethods.indexOf(methodName) > -1) { if (methodName == 'after') { func(1, noop); + } else if (methodName == 'bindAll') { + func({ 'noop': noop }); + } else if (methodName == 'lateBind') { + func(lodash, 'identity', array, string); } else if (/^(?:bind|partial)$/.test(methodName)) { func(noop, object, array, string); } else if (/^(?:compose|memoize|wrap)$/.test(methodName)) { func(noop, noop); } else if (/^(?:debounce|throttle)$/.test(methodName)) { func(noop, 100); - } else if (methodName == 'bindAll') { - func({ 'noop': noop }); } else { func(noop); } diff --git a/test/test.js b/test/test.js index 067cf3349..4ecdaf24c 100644 --- a/test/test.js +++ b/test/test.js @@ -167,23 +167,6 @@ bound(['b'], 'c'); deepEqual(args, ['a', ['b'], 'c']); }); - - test('supports lazy bind', function() { - var object = { - 'name': 'moe', - 'greet': function(greeting) { - return greeting + ': ' + this.name; - } - }; - - var func = _.bind(object, 'greet', 'hi'); - equal(func(), 'hi: moe'); - - object.greet = function(greeting) { - return greeting + ' ' + this.name + '!'; - }; - equal(func(), 'hi moe!'); - }); }()); /*--------------------------------------------------------------------------*/ @@ -914,6 +897,29 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.lateBind'); + + (function() { + test('should work when the target function is overwritten', function() { + var object = { + 'name': 'moe', + 'greet': function(greeting) { + return greeting + ': ' + this.name; + } + }; + + var func = _.lateBind(object, 'greet', 'hi'); + equal(func(), 'hi: moe'); + + object.greet = function(greeting) { + return greeting + ' ' + this.name + '!'; + }; + equal(func(), 'hi moe!'); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.merge'); (function() {