Skip to content

Commit

Permalink
Merge 545c39c into 200aa48
Browse files Browse the repository at this point in the history
  • Loading branch information
mgred committed Aug 3, 2019
2 parents 200aa48 + 545c39c commit 9a303f5
Show file tree
Hide file tree
Showing 5 changed files with 1,487 additions and 1,482 deletions.
22 changes: 12 additions & 10 deletions lib/deep-equal.js
@@ -1,6 +1,9 @@
"use strict";

var valueToString = require("@sinonjs/commons").valueToString;
var className = require("@sinonjs/commons").className;
var arrayProto = require("@sinonjs/commons").prototypes.array;
var objectProto = require("@sinonjs/commons").prototypes.object;

var getClass = require("./get-class");
var identical = require("./identical");
Expand All @@ -12,12 +15,11 @@ var isNaN = require("./is-nan");
var isObject = require("./is-object");
var isSet = require("./is-set");
var isSubset = require("./is-subset");
var getClassName = require("./get-class-name");

var every = Array.prototype.every;
var every = arrayProto.every;
var getTime = Date.prototype.getTime;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var indexOf = Array.prototype.indexOf;
var hasOwnProperty = objectProto.hasOwnProperty;
var indexOf = arrayProto.indexOf;
var keys = Object.keys;
var getOwnPropertySymbols = Object.getOwnPropertySymbols;

Expand Down Expand Up @@ -114,8 +116,8 @@ function deepEqualCyclic(actual, expectation, match) {
var expectationClass = getClass(expectationObj);
var actualKeys = keys(actualObj);
var expectationKeys = keys(expectationObj);
var actualName = getClassName(actualObj);
var expectationName = getClassName(expectationObj);
var actualName = className(actualObj);
var expectationName = className(expectationObj);
var expectationSymbols =
typeof getOwnPropertySymbols === "function"
? getOwnPropertySymbols(expectationObj)
Expand Down Expand Up @@ -172,8 +174,8 @@ function deepEqualCyclic(actual, expectation, match) {
return mapsDeeplyEqual;
}

return every.call(expectationKeysAndSymbols, function(key) {
if (!hasOwnProperty.call(actualObj, key)) {
return every(expectationKeysAndSymbols, function(key) {
if (!hasOwnProperty(actualObj, key)) {
return false;
}

Expand All @@ -185,10 +187,10 @@ function deepEqualCyclic(actual, expectation, match) {
// (it's faster to check for isObject first, than to
// get -1 from getIndex for non objects)
var actualIndex = actualObject
? indexOf.call(actualObjects, actualValue)
? indexOf(actualObjects, actualValue)
: -1;
var expectationIndex = expectationObject
? indexOf.call(expectationObjects, expectationValue)
? indexOf(expectationObjects, expectationValue)
: -1;
// determines the new paths of the objects
// - for non cyclic objects the current path will be extended
Expand Down
4 changes: 2 additions & 2 deletions lib/get-class.js
@@ -1,12 +1,12 @@
"use strict";

var o = Object.prototype;
var toString = require("@sinonjs/commons").prototypes.object.toString;

function getClass(value) {
// Returns the internal [[Class]] by calling Object.prototype.toString
// with the provided value as this. Return value is a string, naming the
// internal class, e.g. "Array"
return o.toString.call(value).split(/[ \]]/)[1];
return toString(value).split(/[ \]]/)[1];
}

module.exports = getClass;
41 changes: 22 additions & 19 deletions lib/matcher.js
Expand Up @@ -19,9 +19,20 @@ var some = arrayProto.some;

var hasOwnProperty = objectProto.hasOwnProperty;
var isPrototypeOf = objectProto.isPrototypeOf;
var objectToString = objectProto.toString;

var stringIndexOf = stringProto.indexOf;

var matcher = {
toString: function() {
return this.message;
}
};

function isMatcher(object) {
return isPrototypeOf(matcher, object);
}

function assertType(value, type, name) {
var actual = typeOf(value);
if (actual !== type) {
Expand All @@ -44,14 +55,14 @@ function assertMethodExists(value, method, name, methodPath) {
}
}

var matcher = {
toString: function() {
return this.message;
function assertMatcher(value) {
if (!isMatcher(value)) {
throw new TypeError("Matcher expected");
}
};
}

function isMatcher(object) {
return isPrototypeOf(matcher, object);
function isIterable(value) {
return !!value && typeOf(value.forEach) === "function";
}

function matchObject(actual, expectation) {
Expand Down Expand Up @@ -246,9 +257,7 @@ match.instanceOf = function(type) {
}
return match(function(actual) {
return actual instanceof type;
}, "instanceOf(" +
(functionName(type) || Object.prototype.toString.call(type)) +
")");
}, "instanceOf(" + (functionName(type) || objectToString(type)) + ")");
};

function createPropertyMatcher(propertyTest, messagePrefix) {
Expand Down Expand Up @@ -305,9 +314,7 @@ match.hasNested = function(property, value) {
};

match.every = function(predicate) {
if (!isMatcher(predicate)) {
throw new TypeError("Matcher expected");
}
assertMatcher(predicate);

return match(function(actual) {
if (typeOf(actual) === "object") {
Expand All @@ -317,8 +324,7 @@ match.every = function(predicate) {
}

return (
!!actual &&
typeOf(actual.forEach) === "function" &&
isIterable(actual) &&
every(actual, function(element) {
return predicate.test(element);
})
Expand All @@ -327,9 +333,7 @@ match.every = function(predicate) {
};

match.some = function(predicate) {
if (!isMatcher(predicate)) {
throw new TypeError("Matcher expected");
}
assertMatcher(predicate);

return match(function(actual) {
if (typeOf(actual) === "object") {
Expand All @@ -339,8 +343,7 @@ match.some = function(predicate) {
}

return (
!!actual &&
typeOf(actual.forEach) === "function" &&
isIterable(actual) &&
!every(actual, function(element) {
return !predicate.test(element);
})
Expand Down

0 comments on commit 9a303f5

Please sign in to comment.