Implement _.isError

This commit is contained in:
Mike Pennisi
2014-04-18 20:38:31 -04:00
committed by John-David Dalton
parent 976e81be43
commit b018ada5c8
2 changed files with 74 additions and 3 deletions

View File

@@ -6531,6 +6531,28 @@
return typeof value == 'undefined';
}
/**
* Checks if `value` is an instance of a native `Error` class.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an instance of a native `Error`, else `false`.
* @example
*
* _.isError(new Error);
* // => true
*
* _.isError('string');
* // => false
*/
function isError(value) {
var type = typeof value;
return value && type == 'object' && toString.call(value) == errorClass ||
false;
}
/**
* Creates an array of the own enumerable property names of `object`.
*
@@ -8376,6 +8398,7 @@
lodash.isRegExp = isRegExp;
lodash.isString = isString;
lodash.isUndefined = isUndefined;
lodash.isError = isError;
lodash.kebabCase = kebabCase;
lodash.lastIndexOf = lastIndexOf;
lodash.mixin = mixin;

View File

@@ -278,6 +278,7 @@
"'_regexp': /x/,",
"'_string': new String('a'),",
"'_undefined': undefined,",
"'_error': new Error(),",
'})'
].join('\n')));
}
@@ -5255,13 +5256,60 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.isError');
(function() {
var args = arguments;
test('should return `true` for `Error` instances', 7, function() {
strictEqual(_.isError(new Error()), true);
strictEqual(_.isError(new EvalError()), true);
strictEqual(_.isError(new RangeError()), true);
strictEqual(_.isError(new ReferenceError()), true);
strictEqual(_.isError(new SyntaxError()), true);
strictEqual(_.isError(new TypeError()), true);
strictEqual(_.isError(new URIError()), true);
});
test('should return `false` for non `Error` instances', 10, function() {
var expected = _.map(falsey, function(value) { return false; });
var actual = _.map(falsey, function(value, index) {
return _.isError(value);
});
strictEqual(_.isError(args), false);
strictEqual(_.isError([1, 2, 3]), false);
strictEqual(_.isError(true), false);
strictEqual(_.isError(new Date), false);
strictEqual(_.isError(_), false);
strictEqual(_.isError({ 'a': 1 }), false);
strictEqual(_.isError(0), false);
strictEqual(_.isError(/x/), false);
strictEqual(_.isError('a'), false);
deepEqual(actual, expected);
});
test('should work with an `Error` from another realm', 1, function() {
if (_._error) {
strictEqual(_.isError(_._error), true);
}
else {
skipTest();
}
});
}(1, 2, 3));
/*--------------------------------------------------------------------------*/
QUnit.module('isType checks');
(function() {
test('should return `false` for subclassed values', 7, function() {
test('should return `false` for subclassed values', 8, function() {
var funcs = [
'isArray', 'isBoolean', 'isDate', 'isFunction',
'isNumber', 'isRegExp', 'isString'
'isNumber', 'isRegExp', 'isString', 'isError'
];
_.each(funcs, function(methodName) {
@@ -10283,7 +10331,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey);
test('should accept falsey arguments', 184, function() {
test('should accept falsey arguments', 185, function() {
var emptyArrays = _.map(falsey, _.constant([])),
isExposed = '_' in root,
oldDash = root._;