mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-14 04:37:50 +00:00
Allow _.attempt to provide additional arguments to func. [closes #929]
This commit is contained in:
@@ -10140,8 +10140,8 @@
|
|||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to invoke `func`, returning either the result or the caught
|
* Attempts to invoke `func`, returning either the result or the caught error
|
||||||
* error object.
|
* object. Any additional arguments are provided to `func` when it is invoked.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
@@ -10151,9 +10151,9 @@
|
|||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* // avoid throwing errors for invalid selectors
|
* // avoid throwing errors for invalid selectors
|
||||||
* var elements = _.attempt(function() {
|
* var elements = _.attempt(function(selector) {
|
||||||
* return document.querySelectorAll(selector);
|
* return document.querySelectorAll(selector);
|
||||||
* });
|
* }, '>_>');
|
||||||
*
|
*
|
||||||
* if (_.isError(elements)) {
|
* if (_.isError(elements)) {
|
||||||
* elements = [];
|
* elements = [];
|
||||||
@@ -10161,7 +10161,7 @@
|
|||||||
*/
|
*/
|
||||||
function attempt(func) {
|
function attempt(func) {
|
||||||
try {
|
try {
|
||||||
return func();
|
return func.apply(undefined, baseSlice(arguments, 1));
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
return isError(e) ? e : Error(e);
|
return isError(e) ? e : Error(e);
|
||||||
}
|
}
|
||||||
|
|||||||
17
test/test.js
17
test/test.js
@@ -1076,6 +1076,11 @@
|
|||||||
strictEqual(_.attempt(_.constant('x')), 'x');
|
strictEqual(_.attempt(_.constant('x')), 'x');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should provide additional arguments to `func`', 1, function() {
|
||||||
|
var actual = _.attempt(function() { return slice.call(arguments); }, 1, 2);
|
||||||
|
deepEqual(actual, [1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
test('should return the caught error', 1, function() {
|
test('should return the caught error', 1, function() {
|
||||||
var expected = _.map(errors, _.constant(true));
|
var expected = _.map(errors, _.constant(true));
|
||||||
|
|
||||||
@@ -3378,16 +3383,16 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest('should accept additional arguments', 1, function() {
|
asyncTest('should provide additional arguments to `func`', 1, function() {
|
||||||
if (!(isRhino && isModularize)) {
|
if (!(isRhino && isModularize)) {
|
||||||
var args;
|
var args;
|
||||||
|
|
||||||
_.defer(function() {
|
_.defer(function() {
|
||||||
args = slice.call(arguments);
|
args = slice.call(arguments);
|
||||||
}, 1, 2, 3);
|
}, 1, 2);
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
deepEqual(args, [1, 2, 3]);
|
deepEqual(args, [1, 2]);
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
}, 128);
|
}, 128);
|
||||||
}
|
}
|
||||||
@@ -3444,16 +3449,16 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest('should accept additional arguments', 1, function() {
|
asyncTest('should provide additional arguments to `func`', 1, function() {
|
||||||
if (!(isRhino && isModularize)) {
|
if (!(isRhino && isModularize)) {
|
||||||
var args;
|
var args;
|
||||||
|
|
||||||
_.delay(function() {
|
_.delay(function() {
|
||||||
args = slice.call(arguments);
|
args = slice.call(arguments);
|
||||||
}, 32, 1, 2, 3);
|
}, 32, 1, 2);
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
deepEqual(args, [1, 2, 3]);
|
deepEqual(args, [1, 2]);
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
}, 128);
|
}, 128);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user