mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 03:17:49 +00:00
Reduce code around _.bind and _.partial, and add _.lateBind.
Former-commit-id: 4c962d066ecfa54882cee2216a7310ab34b3b5a3
This commit is contained in:
7
build.js
7
build.js
@@ -109,6 +109,7 @@
|
|||||||
'keys': ['isArguments'],
|
'keys': ['isArguments'],
|
||||||
'last': [],
|
'last': [],
|
||||||
'lastIndexOf': [],
|
'lastIndexOf': [],
|
||||||
|
'lateBind': ['isFunction'],
|
||||||
'map': ['identity'],
|
'map': ['identity'],
|
||||||
'max': [],
|
'max': [],
|
||||||
'memoize': [],
|
'memoize': [],
|
||||||
@@ -120,7 +121,7 @@
|
|||||||
'omit': ['indexOf', 'isArguments'],
|
'omit': ['indexOf', 'isArguments'],
|
||||||
'once': [],
|
'once': [],
|
||||||
'pairs': [],
|
'pairs': [],
|
||||||
'partial': [],
|
'partial': ['isFunction'],
|
||||||
'pick': [],
|
'pick': [],
|
||||||
'pluck': [],
|
'pluck': [],
|
||||||
'random': [],
|
'random': [],
|
||||||
@@ -205,6 +206,7 @@
|
|||||||
'keys',
|
'keys',
|
||||||
'last',
|
'last',
|
||||||
'lastIndexOf',
|
'lastIndexOf',
|
||||||
|
'lateBind',
|
||||||
'map',
|
'map',
|
||||||
'max',
|
'max',
|
||||||
'min',
|
'min',
|
||||||
@@ -230,6 +232,7 @@
|
|||||||
'forIn',
|
'forIn',
|
||||||
'forOwn',
|
'forOwn',
|
||||||
'invert',
|
'invert',
|
||||||
|
'lateBind',
|
||||||
'merge',
|
'merge',
|
||||||
'object',
|
'object',
|
||||||
'omit',
|
'omit',
|
||||||
@@ -1052,7 +1055,7 @@
|
|||||||
|
|
||||||
// remove native `Function#bind` branch in `_.bind`
|
// remove native `Function#bind` branch in `_.bind`
|
||||||
if (methodName == '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`
|
// remove native `Array.isArray` branch in `_.isArray`
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
var compiledVars = [
|
var compiledVars = [
|
||||||
'argsIndex',
|
'argsIndex',
|
||||||
'argsLength',
|
'argsLength',
|
||||||
|
'bindIterator',
|
||||||
'callback',
|
'callback',
|
||||||
'collection',
|
'collection',
|
||||||
'concat',
|
'concat',
|
||||||
@@ -17,7 +18,6 @@
|
|||||||
'identity',
|
'identity',
|
||||||
'index',
|
'index',
|
||||||
'iteratee',
|
'iteratee',
|
||||||
'iteratorBind',
|
|
||||||
'length',
|
'length',
|
||||||
'nativeKeys',
|
'nativeKeys',
|
||||||
'object',
|
'object',
|
||||||
@@ -180,6 +180,7 @@
|
|||||||
'keys',
|
'keys',
|
||||||
'last',
|
'last',
|
||||||
'lastIndexOf',
|
'lastIndexOf',
|
||||||
|
'lateBind',
|
||||||
'map',
|
'map',
|
||||||
'max',
|
'max',
|
||||||
'memoize',
|
'memoize',
|
||||||
|
|||||||
@@ -134,6 +134,7 @@
|
|||||||
'debounce',
|
'debounce',
|
||||||
'defer',
|
'defer',
|
||||||
'delay',
|
'delay',
|
||||||
|
'lateBind',
|
||||||
'memoize',
|
'memoize',
|
||||||
'once',
|
'once',
|
||||||
'partial',
|
'partial',
|
||||||
@@ -241,6 +242,7 @@
|
|||||||
'forIn',
|
'forIn',
|
||||||
'forOwn',
|
'forOwn',
|
||||||
'invert',
|
'invert',
|
||||||
|
'lateBind',
|
||||||
'merge',
|
'merge',
|
||||||
'object',
|
'object',
|
||||||
'omit',
|
'omit',
|
||||||
@@ -370,14 +372,16 @@
|
|||||||
else if (functionsMethods.indexOf(methodName) > -1) {
|
else if (functionsMethods.indexOf(methodName) > -1) {
|
||||||
if (methodName == 'after') {
|
if (methodName == 'after') {
|
||||||
func(1, noop);
|
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)) {
|
} else if (/^(?:bind|partial)$/.test(methodName)) {
|
||||||
func(noop, object, array, string);
|
func(noop, object, array, string);
|
||||||
} else if (/^(?:compose|memoize|wrap)$/.test(methodName)) {
|
} else if (/^(?:compose|memoize|wrap)$/.test(methodName)) {
|
||||||
func(noop, noop);
|
func(noop, noop);
|
||||||
} else if (/^(?:debounce|throttle)$/.test(methodName)) {
|
} else if (/^(?:debounce|throttle)$/.test(methodName)) {
|
||||||
func(noop, 100);
|
func(noop, 100);
|
||||||
} else if (methodName == 'bindAll') {
|
|
||||||
func({ 'noop': noop });
|
|
||||||
} else {
|
} else {
|
||||||
func(noop);
|
func(noop);
|
||||||
}
|
}
|
||||||
|
|||||||
40
test/test.js
40
test/test.js
@@ -167,23 +167,6 @@
|
|||||||
bound(['b'], 'c');
|
bound(['b'], 'c');
|
||||||
deepEqual(args, ['a', ['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');
|
QUnit.module('lodash.merge');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user