Update type-detect to v3.0.0 and fix issues

- Use typeof instead of type-detect whenever possible for performance
- Fix casing whenever type-detect is used per v3.0.0 breaking change
- Remove duplicate type-detect calls by storing results in variables
This commit is contained in:
Grant Snodgrass 2016-10-08 21:03:46 -04:00
parent d1bf6c9a96
commit abbc771aff
4 changed files with 27 additions and 22 deletions

View file

@ -225,7 +225,7 @@ module.exports = function (chai, _) {
, article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(type.charAt(0)) ? 'an ' : 'a ';
this.assert(
type === _.type(obj)
type === _.type(obj).toLowerCase()
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
);
@ -303,11 +303,12 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, objType = _.type(obj).toLowerCase()
, isDeep = flag(this, 'deep')
, descriptor = isDeep ? 'deep ' : '';
// This block is for asserting a subset of properties in an object.
if (_.type(obj) === 'object') {
if (objType === 'object') {
var props = Object.keys(val)
, negate = flag(this, 'negate')
, firstErr = null
@ -342,8 +343,8 @@ module.exports = function (chai, _) {
// Assert inclusion in an array or substring in a string.
this.assert(
typeof obj === 'string' || !isDeep ? ~obj.indexOf(val)
: isDeepIncluded(obj, val)
objType === 'string' || !isDeep ? ~obj.indexOf(val)
: isDeepIncluded(obj, val)
, 'expected #{this} to ' + descriptor + 'include ' + _.inspect(val)
, 'expected #{this} to not ' + descriptor + 'include ' + _.inspect(val));
}
@ -534,7 +535,7 @@ module.exports = function (chai, _) {
var val = flag(this, 'object')
, itemsCount;
switch (_.type(val)) {
switch (_.type(val).toLowerCase()) {
case 'array':
case 'string':
itemsCount = val.length;
@ -582,7 +583,7 @@ module.exports = function (chai, _) {
var obj = flag(this, 'object')
, type = _.type(obj);
this.assert(
'arguments' === type
'Arguments' === type
, 'expected #{this} to be arguments but got ' + type
, 'expected #{this} to not be arguments'
);
@ -702,7 +703,7 @@ module.exports = function (chai, _) {
new Assertion(obj, msg).is.a('number');
}
if (_.type(n) !== 'number') {
if (typeof n !== 'number') {
throw new Error('the argument to above must be a number');
}
@ -763,7 +764,7 @@ module.exports = function (chai, _) {
new Assertion(obj, msg).is.a('number');
}
if (_.type(n) !== 'number') {
if (typeof n !== 'number') {
throw new Error('the argument to least must be a number');
}
@ -824,7 +825,7 @@ module.exports = function (chai, _) {
new Assertion(obj, msg).is.a('number');
}
if (_.type(n) !== 'number') {
if (typeof n !== 'number') {
throw new Error('the argument to below must be a number');
}
@ -885,7 +886,7 @@ module.exports = function (chai, _) {
new Assertion(obj, msg).is.a('number');
}
if (_.type(n) !== 'number') {
if (typeof n !== 'number') {
throw new Error('the argument to most must be a number');
}
@ -946,7 +947,7 @@ module.exports = function (chai, _) {
new Assertion(obj, msg).is.a('number');
}
if (_.type(start) !== 'number' || _.type(finish) !== 'number') {
if (typeof start !== 'number' || typeof finish !== 'number') {
throw new Error('the arguments to within must be numbers');
}
@ -1381,31 +1382,33 @@ module.exports = function (chai, _) {
function assertKeys (keys) {
var obj = flag(this, 'object')
, objType = _.type(obj)
, keysType = _.type(keys)
, isDeep = flag(this, 'deep')
, str
, deepStr = ''
, ok = true
, mixedArgsMsg = 'when testing keys against an object or an array you must give a single Array|Object|String argument or multiple String arguments';
if (_.type(obj) === 'map' || _.type(obj) === 'set') {
if (objType === 'Map' || objType === 'Set') {
deepStr = isDeep ? 'deeply ' : '';
actual = [];
// Map and Set '.keys' aren't supported in IE 11. Therefore, use .forEach.
obj.forEach(function (val, key) { actual.push(key) });
if (_.type(keys) !== 'array') {
if (keysType !== 'Array') {
keys = Array.prototype.slice.call(arguments);
}
} else {
actual = _.getOwnEnumerableProperties(obj);
switch (_.type(keys)) {
case 'array':
switch (keysType) {
case 'Array':
if (arguments.length > 1) throw new Error(mixedArgsMsg);
break;
case 'object':
case 'Object':
if (arguments.length > 1) throw new Error(mixedArgsMsg);
keys = Object.keys(keys);
break;
@ -1691,7 +1694,7 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, itself = flag(this, 'itself')
, context = ('function' === _.type(obj) && !itself)
, context = ('function' === typeof obj && !itself)
? obj.prototype[method]
: obj[method];
@ -1778,7 +1781,7 @@ module.exports = function (chai, _) {
var obj = flag(this, 'object');
new Assertion(obj, msg).is.a('number');
if (_.type(expected) !== 'number' || _.type(delta) !== 'number') {
if (typeof expected !== 'number' || typeof delta !== 'number') {
throw new Error('the arguments to closeTo or approximately must be numbers');
}

View file

@ -34,9 +34,11 @@ module.exports = function (obj, types) {
return or + art + ' ' + t;
}).join(', ');
if (!types.some(function (expected) { return type(obj) === expected; })) {
var objType = type(obj).toLowerCase();
if (!types.some(function (expected) { return objType === expected; })) {
throw new AssertionError(
'object tested must be ' + str + ', but ' + type(obj) + ' given'
'object tested must be ' + str + ', but ' + objType + ' given'
);
}
};

View file

@ -37,7 +37,7 @@
"check-error": "^1.0.1",
"deep-eql": "^0.1.3",
"pathval": "^1.0.0",
"type-detect": "^2.0.1"
"type-detect": "^3.0.0"
},
"devDependencies": {
"browserify": "^13.0.1",

View file

@ -23,7 +23,7 @@ global.err = function (fn, val) {
try {
fn();
} catch (err) {
switch (chai.util.type(val)) {
switch (chai.util.type(val).toLowerCase()) {
case 'undefined': return;
case 'string': return chai.expect(err.message).to.equal(val);
case 'regexp': return chai.expect(err.message).to.match(val);