Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unexpected

Package Overview
Dependencies
Maintainers
2
Versions
330
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unexpected - npm Package Compare versions

Comparing version 3.1.8 to 3.2.0

1

lib/index.js

@@ -28,3 +28,2 @@ /*global console, __dirname, weknowhow, Uint8Array, Uint16Array*/

'unexpected-utils.js',
'unexpected-inspect.js',
'unexpected-core.js',

@@ -31,0 +30,0 @@ 'unexpected-types.js',

155

lib/unexpected-core.js

@@ -8,12 +8,9 @@ /*global namespace*/

var map = shim.map;
var indexOf = shim.indexOf;
var trim = shim.trim;
var reduce = shim.reduce;
var getKeys = shim.getKeys;
var indexOf = shim.indexOf;
var utils = namespace.utils;
var truncateStack = utils.truncateStack;
var isUndefinedOrNull = utils.isUndefinedOrNull;
var isArguments = utils.isArguments;
var getKeysOfDefinedProperties = utils.getKeysOfDefinedProperties;
var extend = utils.extend;

@@ -71,3 +68,15 @@ var levenshteinDistance = utils.levenshteinDistance;

Unexpected.prototype.equal = function (actual, expected, stack) {
Unexpected.prototype.equal = function (actual, expected, depth, seen) {
var that = this;
depth = depth || 0;
if (depth > 500) {
// detect recursive loops in the structure
seen = seen || [];
if (seen.indexOf(actual) !== -1) {
throw new Error('Cannot compare circular structures');
}
seen.push(actual);
}
var matchingCustomType = utils.findFirst(this.types || [], function (type) {

@@ -78,81 +87,36 @@ return type.identify(actual) && type.identify(expected);

if (matchingCustomType) {
return matchingCustomType.equal(actual, expected);
return matchingCustomType.equal(actual, expected, function (a, b) {
return that.equal(a, b, depth + 1, seen);
});
}
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
return false; // we should never get there
};
// 7.3. Other pairs that do not both pass typeof value == "object",
// equivalence is determined by ==.
} else if (typeof actual !== 'object' && typeof expected !== 'object') {
return actual === expected;
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values using === for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
} else if (typeof actual !== typeof expected) {
return false;
} else {
if (isUndefinedOrNull(actual) || isUndefinedOrNull(expected)) {
return false;
Unexpected.prototype.inspect = function (obj, depth) {
var types = this.types;
var seen = [];
var format = function (obj, depth) {
if (depth === 0) {
return '...';
}
// an identical "prototype" property.
if (actual.prototype !== expected.prototype) {
return false;
}
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (isArguments(actual)) {
if (!isArguments(expected)) {
return false;
}
return this.equal(Array.prototype.slice.call(actual), Array.prototype.slice.call(expected), stack);
seen = seen || [];
if (indexOf(seen, obj) !== -1) {
return '[Circular]';
}
var actualKeys = (actual && typeof actual === 'object') ? getKeysOfDefinedProperties(actual) : [],
expectedKeys = (expected && typeof expected === 'object') ? getKeysOfDefinedProperties(expected) : [],
key,
i;
// having the same number of owned properties (keys incorporates hasOwnProperty)
if (actualKeys.length !== expectedKeys.length) {
return false;
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(obj);
});
if (matchingCustomType) {
return matchingCustomType.inspect(obj, function (v) {
seen.push(obj);
return format(v, depth - 1);
}, depth);
}
//the same set of keys (although not necessarily the same order),
actualKeys.sort();
expectedKeys.sort();
//~~~cheap key test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
if (actualKeys[i] !== expectedKeys[i]) {
return false;
}
}
if (stack) {
if (indexOf(stack, actual) !== -1) {
throw new Error('Cannot compare circular structures');
}
stack.push(actual);
} else {
stack = [actual];
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
key = actualKeys[i];
if (!this.equal(actual[key], expected[key], stack)) {
stack.pop();
return false;
}
}
stack.pop();
return true;
}
};
};
Unexpected.prototype.inspect = function (obj) {
return namespace.inspect(obj, false, 2, this.types);
return format(obj, depth || 3);
};

@@ -210,5 +174,37 @@

Unexpected.prototype.addType = function (obj) {
this.types.unshift(obj);
Unexpected.prototype.getType = function (typeName) {
return utils.findFirst(this.types, function (type) {
return type.name === typeName;
});
};
var customTypePrototype = {
name: 'any',
equal: function (a, b) {
return a === b;
},
inspect: function (value) {
return '' + value;
},
toJSON: function (value) {
return value;
}
};
Unexpected.prototype.addType = function (type) {
var baseType;
if (type.base) {
baseType = utils.findFirst(this.types, function (t) {
return t.name === type.base;
});
if (!baseType) {
throw new Error('Unknown base type: ' + type.base);
}
} else {
baseType = customTypePrototype;
}
this.types.unshift(extend({}, baseType, type, { baseType: baseType }));
return this.expect;

@@ -228,2 +224,3 @@ };

Unexpected.prototype.sanitize = function (obj, stack) {
var that = this;
stack = stack || [];

@@ -245,3 +242,5 @@

if (matchingCustomType) {
sanitized = this.sanitize(matchingCustomType.toJSON(obj), stack);
sanitized = matchingCustomType.toJSON(obj, function (v) {
return that.sanitize(v, stack);
});
} else if (isArray(obj)) {

@@ -248,0 +247,0 @@ sanitized = map(obj, function (item) {

@@ -14,2 +14,12 @@ /*global namespace*/

some: function (arr, fn, thisObj) {
var scope = thisObj || null;
for (var i = 0, j = arr.length; i < j; i += 1) {
if (fn.call(scope, arr[i], i, arr)) {
return true;
}
}
return false;
},
indexOf: function (arr, searchElement, fromIndex) {

@@ -16,0 +26,0 @@ var length = arr.length >>> 0; // Hack to convert object.length to a UInt32

@@ -9,2 +9,3 @@ /*global namespace*/

every: Array.prototype.every,
some: Array.prototype.some,
indexOf: Array.prototype.indexOf,

@@ -11,0 +12,0 @@ forEach: Array.prototype.forEach,

@@ -8,4 +8,190 @@ /*global namespace, Uint8Array, Uint16Array*/

var leftPad = utils.leftPad;
var shim = namespace.shim;
var json = shim.JSON;
var every = shim.every;
var some = shim.some;
var map = shim.map;
var getKeys = shim.getKeys;
var reduce = shim.reduce;
var extend = utils.extend;
expect.addType({
name: 'fallback',
identify: function (value) {
return true;
},
equal: function (a, b) {
return a === b;
},
inspect: function (value) {
return '' + value;
},
toJSON: function (value) {
return value;
}
});
expect.addType({
name: 'object',
identify: function (arr) {
return typeof arr === 'object';
},
equal: function (a, b, equal) {
if (a === b) {
return true;
}
// an identical "prototype" property.
if (a.prototype !== b.prototype) {
return false;
}
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (utils.isArguments(a)) {
if (!utils.isArguments(b)) {
return false;
}
return equal(Array.prototype.slice.call(a), Array.prototype.slice.call(b));
}
var actualKeys = utils.getKeysOfDefinedProperties(a),
expectedKeys = utils.getKeysOfDefinedProperties(b),
key,
i;
// having the same number of owned properties (keys incorporates hasOwnProperty)
if (actualKeys.length !== expectedKeys.length) {
return false;
}
//the same set of keys (although not necessarily the same order),
actualKeys.sort();
expectedKeys.sort();
//~~~cheap key test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
if (actualKeys[i] !== expectedKeys[i]) {
return false;
}
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
key = actualKeys[i];
if (!equal(a[key], b[key])) {
return false;
}
}
return true;
},
inspect: function (obj, inspect) {
var keys = getKeys(obj);
if (keys.length === 0) {
return '{}';
}
var inspectedItems = map(keys, function (key) {
var parts = [];
if (key.match(/["' ]/)) {
parts.push(expect.inspect(key) + ':');
} else {
parts.push(key + ':');
}
var hasGetter = obj.__lookupGetter__ && obj.__lookupGetter__(key);
var hasSetter = obj.__lookupGetter__ && obj.__lookupSetter__(key);
if (hasGetter || !hasSetter) {
parts.push(inspect(obj[key]));
}
if (hasGetter && hasSetter) {
parts.push('[Getter/Setter]');
} else if (hasGetter) {
parts.push('[Getter]');
} else if (hasSetter) {
parts.push('[Setter]');
}
return parts.join(' ');
});
var length = 0;
var multipleLines = some(inspectedItems, function (v) {
length += v.length;
return length > 50 || v.match(/\n/);
});
if (multipleLines) {
return '{\n' + inspectedItems.join(',\n').replace(/^/gm, ' ') + '\n}';
} else {
return '{ ' + inspectedItems.join(', ') + ' }';
}
},
toJSON: function (obj, toJSON) {
return reduce(getKeys(obj), function (result, key) {
result[key] = toJSON(obj[key]);
return result;
}, {});
}
});
expect.addType({
name: 'array',
identify: function (arr) {
return utils.isArray(arr) || utils.isArguments(arr);
},
equal: function (a, b, equal) {
return a === b || (a.length === b.length && every(a, function (v, index) {
return equal(v, b[index]);
}));
},
inspect: function (arr, inspect, depth) {
if (arr.length === 0) {
return '[]';
}
if (depth === 1) {
return '[...]';
}
var inspectedItems = map(arr, function (v) {
return inspect(v);
});
var length = 0;
var multipleLines = some(inspectedItems, function (v) {
length += v.length;
return length > 50 || v.match(/\n/);
});
if (multipleLines) {
return '[\n' + inspectedItems.join(',\n').replace(/^/gm, ' ') + '\n]';
} else {
return '[ ' + inspectedItems.join(', ') + ' ]';
}
},
toJSON: function (arr, toJSON) {
return map(arr, toJSON);
}
});
expect.addType({
base: 'object',
name: 'Error',
identify: function (value) {
return utils.isError(value);
},
equal: function (a, b, equal) {
return a === b ||
(equal(a.message, b.message) && this.baseType.equal(a, b, equal));
},
inspect: function (value, inspect) {
var errorObject = extend({
message: value.message
}, value);
return '[Error: ' + inspect(errorObject) + ']';
}
});
expect.addType({
name: 'date',
identify: function (obj) {

@@ -28,2 +214,3 @@ return Object.prototype.toString.call(obj) === '[object Date]';

expect.addType({
name: 'function',
identify: function (f) {

@@ -47,5 +234,11 @@ return typeof f === 'function';

expect.addType({
name: 'regexp',
identify: isRegExp,
equal: function (a, b) {
return a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
return a === b || (
a.source === b.source &&
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline
);
},

@@ -62,2 +255,12 @@ inspect: function (regExp) {

expect.addType({
name: 'DomElement',
identify: function (value) {
return utils.isDOMElement(value);
},
inspect: function (value) {
return utils.getOuterHTML(value);
}
});
function getHexDumpLinesForBufferLikeObject(obj, width, digitWidth) {

@@ -107,2 +310,6 @@ digitWidth = digitWidth || 2;

function bufferLikeObjectsEqual(a, b) {
if (a === b) {
return true;
}
if (a.length !== b.length) return false;

@@ -119,2 +326,3 @@

expect.addType({
name: 'Buffer',
identify: Buffer.isBuffer,

@@ -135,2 +343,3 @@ equal: bufferLikeObjectsEqual,

expect.addType({
name: 'Uint8Array',
identify: function (obj) {

@@ -154,2 +363,3 @@ return obj && obj instanceof Uint8Array;

expect.addType({
name: 'Uint16Array',
identify: function (obj) {

@@ -169,2 +379,42 @@ return obj && obj instanceof Uint16Array;

}
expect.addType({
name: 'string',
identify: function (value) {
return typeof value === 'string';
},
inspect: function (value) {
return '\'' + json.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
}
});
expect.addType({
name: 'number',
identify: function (value) {
return typeof value === 'number';
}
});
expect.addType({
name: 'boolean',
identify: function (value) {
return typeof value === 'boolean';
}
});
expect.addType({
name: 'undefined',
identify: function (value) {
return typeof value === 'undefined';
}
});
expect.addType({
name: 'null',
identify: function (value) {
return value === null;
}
});
}());
{
"name": "unexpected",
"version": "3.1.8",
"version": "3.2.0",
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>",

@@ -5,0 +5,0 @@ "keywords": [

@@ -33,2 +33,3 @@ (function () {

every: Array.prototype.every,
some: Array.prototype.some,
indexOf: Array.prototype.indexOf,

@@ -237,211 +238,2 @@ forEach: Array.prototype.forEach,

var shim = namespace.shim;
var json = shim.JSON;
var getKeys = shim.getKeys;
var map = shim.map;
var indexOf = shim.indexOf;
var reduce = shim.reduce;
var utils = namespace.utils;
var isDOMElement = utils.isDOMElement;
var getOuterHTML = utils.getOuterHTML;
var isArray = utils.isArray;
var isRegExp = utils.isRegExp;
var isError = utils.isError;
function formatError(err) {
return '[' + Error.prototype.toString.call(err) + ']';
}
/**
* Inspects an object.
*
* @see taken from node.js `util` module (copyright Joyent, MIT license)
*/
var inspect = function (obj, showHidden, depth, types) {
var seen = [];
function format(value, recurseTimes) {
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(value);
});
if (matchingCustomType) {
return matchingCustomType.inspect(value);
}
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (value && typeof value.inspect === 'function' &&
// Filter out the util module, it's inspect function is special
(typeof exports === 'undefined' || value !== exports) &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
return value.inspect(recurseTimes);
}
// Primitive types cannot have properties
switch (typeof value) {
case 'undefined':
return 'undefined';
case 'string':
return '\'' + json.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
case 'number':
case 'boolean':
return '' + value;
}
// For some reason typeof null is "object", so special case here.
if (value === null) {
return 'null';
}
if (isDOMElement(value)) {
return getOuterHTML(value);
}
if (isRegExp(value)) {
return '' + value;
}
if (isError(value)) {
return formatError(value);
}
// Look up the keys of the object.
var visible_keys = getKeys(value);
var $keys = showHidden ? Object.getOwnPropertyNames(value) : visible_keys;
// Functions without properties can be shortcutted.
if (typeof value === 'function' && $keys.length === 0) {
if (isRegExp(value)) {
return '' + value;
} else {
var name = value.name ? ': ' + value.name : '';
return '[Function' + name + ']';
}
}
var base, type, braces;
// Determine the object type
if (isArray(value)) {
type = 'Array';
braces = ['[', ']'];
} else {
type = 'Object';
braces = ['{', '}'];
}
// Make functions say that they are functions
if (typeof value === 'function') {
var n = value.name ? ': ' + value.name : '';
base = (isRegExp(value)) ? ' ' + value : ' [Function' + n + ']';
} else {
base = '';
}
if ($keys.length === 0) {
return braces[0] + base + braces[1];
}
if (recurseTimes < 0) {
if (isRegExp(value)) {
return '' + value;
} else {
return '[Object]';
}
}
seen.push(value);
var output = map($keys, function (key) {
var name, str;
if (value.__lookupGetter__) {
if (value.__lookupGetter__(key)) {
if (value.__lookupSetter__(key)) {
str = '[Getter/Setter]';
} else {
str = '[Getter]';
}
} else {
if (value.__lookupSetter__(key)) {
str = '[Setter]';
}
}
}
if (indexOf(visible_keys, key) < 0) {
name = '[' + key + ']';
}
if (!str) {
if (indexOf(seen, value[key]) < 0) {
if (recurseTimes === null) {
str = format(value[key]);
} else {
str = format(value[key], recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (isArray(value)) {
str = map(str.split('\n'), function (line) {
return ' ' + line;
}).join('\n').substr(2);
} else {
str = '\n' + map(str.split('\n'), function (line) {
return ' ' + line;
}).join('\n');
}
}
} else {
str = '[Circular]';
}
}
if (typeof name === 'undefined') {
if (type === 'Array' && key.match(/^\d+$/)) {
return str;
}
name = json.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length - 2);
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
}
}
return name + ': ' + str;
});
seen.pop();
var numLinesEst = 0;
var length = reduce(output, function (prev, cur) {
numLinesEst += 1;
if (indexOf(cur, '\n') >= 0) numLinesEst += 1;
return prev + cur.length + 1;
}, 0);
if (length > 50) {
output = braces[0] +
(base === '' ? '' : base + '\n ') +
' ' +
output.join(',\n ') +
' ' +
braces[1];
} else {
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
}
return output;
}
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
};
namespace.inspect = inspect;
}());
(function () {
var shim = namespace.shim;
var bind = shim.bind;

@@ -451,12 +243,9 @@ var forEach = shim.forEach;

var map = shim.map;
var indexOf = shim.indexOf;
var trim = shim.trim;
var reduce = shim.reduce;
var getKeys = shim.getKeys;
var indexOf = shim.indexOf;
var utils = namespace.utils;
var truncateStack = utils.truncateStack;
var isUndefinedOrNull = utils.isUndefinedOrNull;
var isArguments = utils.isArguments;
var getKeysOfDefinedProperties = utils.getKeysOfDefinedProperties;
var extend = utils.extend;

@@ -514,3 +303,15 @@ var levenshteinDistance = utils.levenshteinDistance;

Unexpected.prototype.equal = function (actual, expected, stack) {
Unexpected.prototype.equal = function (actual, expected, depth, seen) {
var that = this;
depth = depth || 0;
if (depth > 500) {
// detect recursive loops in the structure
seen = seen || [];
if (seen.indexOf(actual) !== -1) {
throw new Error('Cannot compare circular structures');
}
seen.push(actual);
}
var matchingCustomType = utils.findFirst(this.types || [], function (type) {

@@ -521,81 +322,36 @@ return type.identify(actual) && type.identify(expected);

if (matchingCustomType) {
return matchingCustomType.equal(actual, expected);
return matchingCustomType.equal(actual, expected, function (a, b) {
return that.equal(a, b, depth + 1, seen);
});
}
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
return false; // we should never get there
};
// 7.3. Other pairs that do not both pass typeof value == "object",
// equivalence is determined by ==.
} else if (typeof actual !== 'object' && typeof expected !== 'object') {
return actual === expected;
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values using === for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
} else if (typeof actual !== typeof expected) {
return false;
} else {
if (isUndefinedOrNull(actual) || isUndefinedOrNull(expected)) {
return false;
Unexpected.prototype.inspect = function (obj, depth) {
var types = this.types;
var seen = [];
var format = function (obj, depth) {
if (depth === 0) {
return '...';
}
// an identical "prototype" property.
if (actual.prototype !== expected.prototype) {
return false;
}
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (isArguments(actual)) {
if (!isArguments(expected)) {
return false;
}
return this.equal(Array.prototype.slice.call(actual), Array.prototype.slice.call(expected), stack);
seen = seen || [];
if (indexOf(seen, obj) !== -1) {
return '[Circular]';
}
var actualKeys = (actual && typeof actual === 'object') ? getKeysOfDefinedProperties(actual) : [],
expectedKeys = (expected && typeof expected === 'object') ? getKeysOfDefinedProperties(expected) : [],
key,
i;
// having the same number of owned properties (keys incorporates hasOwnProperty)
if (actualKeys.length !== expectedKeys.length) {
return false;
var matchingCustomType = utils.findFirst(types || [], function (type) {
return type.identify(obj);
});
if (matchingCustomType) {
return matchingCustomType.inspect(obj, function (v) {
seen.push(obj);
return format(v, depth - 1);
}, depth);
}
//the same set of keys (although not necessarily the same order),
actualKeys.sort();
expectedKeys.sort();
//~~~cheap key test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
if (actualKeys[i] !== expectedKeys[i]) {
return false;
}
}
if (stack) {
if (indexOf(stack, actual) !== -1) {
throw new Error('Cannot compare circular structures');
}
stack.push(actual);
} else {
stack = [actual];
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
key = actualKeys[i];
if (!this.equal(actual[key], expected[key], stack)) {
stack.pop();
return false;
}
}
stack.pop();
return true;
}
};
};
Unexpected.prototype.inspect = function (obj) {
return namespace.inspect(obj, false, 2, this.types);
return format(obj, depth || 3);
};

@@ -653,5 +409,37 @@

Unexpected.prototype.addType = function (obj) {
this.types.unshift(obj);
Unexpected.prototype.getType = function (typeName) {
return utils.findFirst(this.types, function (type) {
return type.name === typeName;
});
};
var customTypePrototype = {
name: 'any',
equal: function (a, b) {
return a === b;
},
inspect: function (value) {
return '' + value;
},
toJSON: function (value) {
return value;
}
};
Unexpected.prototype.addType = function (type) {
var baseType;
if (type.base) {
baseType = utils.findFirst(this.types, function (t) {
return t.name === type.base;
});
if (!baseType) {
throw new Error('Unknown base type: ' + type.base);
}
} else {
baseType = customTypePrototype;
}
this.types.unshift(extend({}, baseType, type, { baseType: baseType }));
return this.expect;

@@ -671,2 +459,3 @@ };

Unexpected.prototype.sanitize = function (obj, stack) {
var that = this;
stack = stack || [];

@@ -688,3 +477,5 @@

if (matchingCustomType) {
sanitized = this.sanitize(matchingCustomType.toJSON(obj), stack);
sanitized = matchingCustomType.toJSON(obj, function (v) {
return that.sanitize(v, stack);
});
} else if (isArray(obj)) {

@@ -1365,4 +1156,190 @@ sanitized = map(obj, function (item) {

var leftPad = utils.leftPad;
var shim = namespace.shim;
var json = shim.JSON;
var every = shim.every;
var some = shim.some;
var map = shim.map;
var getKeys = shim.getKeys;
var reduce = shim.reduce;
var extend = utils.extend;
expect.addType({
name: 'fallback',
identify: function (value) {
return true;
},
equal: function (a, b) {
return a === b;
},
inspect: function (value) {
return '' + value;
},
toJSON: function (value) {
return value;
}
});
expect.addType({
name: 'object',
identify: function (arr) {
return typeof arr === 'object';
},
equal: function (a, b, equal) {
if (a === b) {
return true;
}
// an identical "prototype" property.
if (a.prototype !== b.prototype) {
return false;
}
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (utils.isArguments(a)) {
if (!utils.isArguments(b)) {
return false;
}
return equal(Array.prototype.slice.call(a), Array.prototype.slice.call(b));
}
var actualKeys = utils.getKeysOfDefinedProperties(a),
expectedKeys = utils.getKeysOfDefinedProperties(b),
key,
i;
// having the same number of owned properties (keys incorporates hasOwnProperty)
if (actualKeys.length !== expectedKeys.length) {
return false;
}
//the same set of keys (although not necessarily the same order),
actualKeys.sort();
expectedKeys.sort();
//~~~cheap key test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
if (actualKeys[i] !== expectedKeys[i]) {
return false;
}
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = actualKeys.length - 1; i >= 0; i -= 1) {
key = actualKeys[i];
if (!equal(a[key], b[key])) {
return false;
}
}
return true;
},
inspect: function (obj, inspect) {
var keys = getKeys(obj);
if (keys.length === 0) {
return '{}';
}
var inspectedItems = map(keys, function (key) {
var parts = [];
if (key.match(/["' ]/)) {
parts.push(expect.inspect(key) + ':');
} else {
parts.push(key + ':');
}
var hasGetter = obj.__lookupGetter__ && obj.__lookupGetter__(key);
var hasSetter = obj.__lookupGetter__ && obj.__lookupSetter__(key);
if (hasGetter || !hasSetter) {
parts.push(inspect(obj[key]));
}
if (hasGetter && hasSetter) {
parts.push('[Getter/Setter]');
} else if (hasGetter) {
parts.push('[Getter]');
} else if (hasSetter) {
parts.push('[Setter]');
}
return parts.join(' ');
});
var length = 0;
var multipleLines = some(inspectedItems, function (v) {
length += v.length;
return length > 50 || v.match(/\n/);
});
if (multipleLines) {
return '{\n' + inspectedItems.join(',\n').replace(/^/gm, ' ') + '\n}';
} else {
return '{ ' + inspectedItems.join(', ') + ' }';
}
},
toJSON: function (obj, toJSON) {
return reduce(getKeys(obj), function (result, key) {
result[key] = toJSON(obj[key]);
return result;
}, {});
}
});
expect.addType({
name: 'array',
identify: function (arr) {
return utils.isArray(arr) || utils.isArguments(arr);
},
equal: function (a, b, equal) {
return a === b || (a.length === b.length && every(a, function (v, index) {
return equal(v, b[index]);
}));
},
inspect: function (arr, inspect, depth) {
if (arr.length === 0) {
return '[]';
}
if (depth === 1) {
return '[...]';
}
var inspectedItems = map(arr, function (v) {
return inspect(v);
});
var length = 0;
var multipleLines = some(inspectedItems, function (v) {
length += v.length;
return length > 50 || v.match(/\n/);
});
if (multipleLines) {
return '[\n' + inspectedItems.join(',\n').replace(/^/gm, ' ') + '\n]';
} else {
return '[ ' + inspectedItems.join(', ') + ' ]';
}
},
toJSON: function (arr, toJSON) {
return map(arr, toJSON);
}
});
expect.addType({
base: 'object',
name: 'Error',
identify: function (value) {
return utils.isError(value);
},
equal: function (a, b, equal) {
return a === b ||
(equal(a.message, b.message) && this.baseType.equal(a, b, equal));
},
inspect: function (value, inspect) {
var errorObject = extend({
message: value.message
}, value);
return '[Error: ' + inspect(errorObject) + ']';
}
});
expect.addType({
name: 'date',
identify: function (obj) {

@@ -1385,2 +1362,3 @@ return Object.prototype.toString.call(obj) === '[object Date]';

expect.addType({
name: 'function',
identify: function (f) {

@@ -1404,5 +1382,11 @@ return typeof f === 'function';

expect.addType({
name: 'regexp',
identify: isRegExp,
equal: function (a, b) {
return a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
return a === b || (
a.source === b.source &&
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline
);
},

@@ -1419,2 +1403,12 @@ inspect: function (regExp) {

expect.addType({
name: 'DomElement',
identify: function (value) {
return utils.isDOMElement(value);
},
inspect: function (value) {
return utils.getOuterHTML(value);
}
});
function getHexDumpLinesForBufferLikeObject(obj, width, digitWidth) {

@@ -1464,2 +1458,6 @@ digitWidth = digitWidth || 2;

function bufferLikeObjectsEqual(a, b) {
if (a === b) {
return true;
}
if (a.length !== b.length) return false;

@@ -1476,2 +1474,3 @@

expect.addType({
name: 'Buffer',
identify: Buffer.isBuffer,

@@ -1492,2 +1491,3 @@ equal: bufferLikeObjectsEqual,

expect.addType({
name: 'Uint8Array',
identify: function (obj) {

@@ -1511,2 +1511,3 @@ return obj && obj instanceof Uint8Array;

expect.addType({
name: 'Uint16Array',
identify: function (obj) {

@@ -1526,2 +1527,42 @@ return obj && obj instanceof Uint16Array;

}
expect.addType({
name: 'string',
identify: function (value) {
return typeof value === 'string';
},
inspect: function (value) {
return '\'' + json.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
}
});
expect.addType({
name: 'number',
identify: function (value) {
return typeof value === 'number';
}
});
expect.addType({
name: 'boolean',
identify: function (value) {
return typeof value === 'boolean';
}
});
expect.addType({
name: 'undefined',
identify: function (value) {
return typeof value === 'undefined';
}
});
expect.addType({
name: 'null',
identify: function (value) {
return value === null;
}
});
}());

@@ -1528,0 +1569,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc