Skip to content

Commit

Permalink
Rename function to createMatcher
Browse files Browse the repository at this point in the history
Using a descriptive name helps make the code easier to understand

The function is already exported as samsam.createMatcher
  • Loading branch information
mroderick committed Aug 31, 2019
1 parent 5c993af commit 73a9554
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions lib/create-matcher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var arrayProto = require("@sinonjs/commons").prototypes.array;
var deepEqual = require("./deep-equal").use(match); // eslint-disable-line no-use-before-define
var deepEqual = require("./deep-equal").use(createMatcher); // eslint-disable-line no-use-before-define
var every = require("@sinonjs/commons").every;
var functionName = require("@sinonjs/commons").functionName;
var get = require("lodash").get;
Expand Down Expand Up @@ -195,7 +195,7 @@ var TYPE_MAP = {
* @param {string} message A message for the expectation
* @returns {object} A matcher object
*/
function match(expectation, message) {
function createMatcher(expectation, message) {
var m = Object.create(matcher);
var type = typeOf(expectation);

Expand Down Expand Up @@ -229,7 +229,7 @@ matcher.or = function(valueOrMatcher) {
throw new TypeError("Matcher expected");
}

var m2 = isMatcher(valueOrMatcher) ? valueOrMatcher : match(valueOrMatcher);
var m2 = isMatcher(valueOrMatcher) ? valueOrMatcher : createMatcher(valueOrMatcher);
var m1 = this;
var or = Object.create(matcher);
or.test = function(actual) {
Expand All @@ -244,7 +244,7 @@ matcher.and = function(valueOrMatcher) {
throw new TypeError("Matcher expected");
}

var m2 = isMatcher(valueOrMatcher) ? valueOrMatcher : match(valueOrMatcher);
var m2 = isMatcher(valueOrMatcher) ? valueOrMatcher : createMatcher(valueOrMatcher);
var m1 = this;
var and = Object.create(matcher);
and.test = function(actual) {
Expand All @@ -254,50 +254,50 @@ matcher.and = function(valueOrMatcher) {
return and;
};

match.isMatcher = isMatcher;
createMatcher.isMatcher = isMatcher;

match.any = match(function() {
createMatcher.any = createMatcher(function() {
return true;
}, "any");

match.defined = match(function(actual) {
createMatcher.defined = createMatcher(function(actual) {
return actual !== null && actual !== undefined;
}, "defined");

match.truthy = match(function(actual) {
createMatcher.truthy = createMatcher(function(actual) {
return Boolean(actual);
}, "truthy");

match.falsy = match(function(actual) {
createMatcher.falsy = createMatcher(function(actual) {
return !actual;
}, "falsy");

match.same = function(expectation) {
return match(function(actual) {
createMatcher.same = function(expectation) {
return createMatcher(function(actual) {
return expectation === actual;
}, "same(" + valueToString(expectation) + ")");
};

match.in = function(arrayOfExpectations) {
createMatcher.in = function(arrayOfExpectations) {
if (typeOf(arrayOfExpectations) !== "array") {
throw new TypeError("array expected");
}

return match(function(actual) {
return createMatcher(function(actual) {
return some(arrayOfExpectations, function(expectation) {
return expectation === actual;
});
}, "in(" + valueToString(arrayOfExpectations) + ")");
};

match.typeOf = function(type) {
createMatcher.typeOf = function(type) {
assertType(type, "string", "type");
return match(function(actual) {
return createMatcher(function(actual) {
return typeOf(actual) === type;
}, 'typeOf("' + type + '")');
};

match.instanceOf = function(type) {
createMatcher.instanceOf = function(type) {
if (
typeof Symbol === "undefined" ||
typeof Symbol.hasInstance === "undefined"
Expand All @@ -311,7 +311,7 @@ match.instanceOf = function(type) {
"[Symbol.hasInstance]"
);
}
return match(function(actual) {
return createMatcher(function(actual) {
return actual instanceof type;
}, "instanceOf(" + (functionName(type) || objectToString(type)) + ")");
};
Expand All @@ -333,7 +333,7 @@ function createPropertyMatcher(propertyTest, messagePrefix) {
message += ", " + valueToString(value);
}
message += ")";
return match(function(actual) {
return createMatcher(function(actual) {
if (
actual === undefined ||
actual === null ||
Expand All @@ -346,26 +346,26 @@ function createPropertyMatcher(propertyTest, messagePrefix) {
};
}

match.has = createPropertyMatcher(function(actual, property) {
createMatcher.has = createPropertyMatcher(function(actual, property) {
if (typeof actual === "object") {
return property in actual;
}
return actual[property] !== undefined;
}, "has");

match.hasOwn = createPropertyMatcher(function(actual, property) {
createMatcher.hasOwn = createPropertyMatcher(function(actual, property) {
return hasOwnProperty(actual, property);
}, "hasOwn");

match.hasNested = function(property, value) {
createMatcher.hasNested = function(property, value) {
assertType(property, "string", "property");
var onlyProperty = arguments.length === 1;
var message = 'hasNested("' + property + '"';
if (!onlyProperty) {
message += ", " + valueToString(value);
}
message += ")";
return match(function(actual) {
return createMatcher(function(actual) {
if (
actual === undefined ||
actual === null ||
Expand All @@ -377,10 +377,10 @@ match.hasNested = function(property, value) {
}, message);
};

match.every = function(predicate) {
createMatcher.every = function(predicate) {
assertMatcher(predicate);

return match(function(actual) {
return createMatcher(function(actual) {
if (typeOf(actual) === "object") {
return every(Object.keys(actual), function(key) {
return predicate.test(actual[key]);
Expand All @@ -396,10 +396,10 @@ match.every = function(predicate) {
}, "every(" + predicate.message + ")");
};

match.some = function(predicate) {
createMatcher.some = function(predicate) {
assertMatcher(predicate);

return match(function(actual) {
return createMatcher(function(actual) {
if (typeOf(actual) === "object") {
return !every(Object.keys(actual), function(key) {
return !predicate.test(actual[key]);
Expand All @@ -415,10 +415,10 @@ match.some = function(predicate) {
}, "some(" + predicate.message + ")");
};

match.array = match.typeOf("array");
createMatcher.array = createMatcher.typeOf("array");

match.array.deepEquals = function(expectation) {
return match(function(actual) {
createMatcher.array.deepEquals = function(expectation) {
return createMatcher(function(actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.length === expectation.length;
return (
Expand All @@ -428,15 +428,15 @@ match.array.deepEquals = function(expectation) {
var expected = expectation[index];
return typeOf(expected) === "array" &&
typeOf(element) === "array"
? match.array.deepEquals(expected).test(element)
? createMatcher.array.deepEquals(expected).test(element)
: deepEqual(expected, element);
})
);
}, "deepEquals([" + iterableToString(expectation) + "])");
};

match.array.startsWith = function(expectation) {
return match(function(actual) {
createMatcher.array.startsWith = function(expectation) {
return createMatcher(function(actual) {
return (
typeOf(actual) === "array" &&
every(expectation, function(expectedElement, index) {
Expand All @@ -446,8 +446,8 @@ match.array.startsWith = function(expectation) {
}, "startsWith([" + iterableToString(expectation) + "])");
};

match.array.endsWith = function(expectation) {
return match(function(actual) {
createMatcher.array.endsWith = function(expectation) {
return createMatcher(function(actual) {
// This indicates the index in which we should start matching
var offset = actual.length - expectation.length;

Expand All @@ -460,8 +460,8 @@ match.array.endsWith = function(expectation) {
}, "endsWith([" + iterableToString(expectation) + "])");
};

match.array.contains = function(expectation) {
return match(function(actual) {
createMatcher.array.contains = function(expectation) {
return createMatcher(function(actual) {
return (
typeOf(actual) === "array" &&
every(expectation, function(expectedElement) {
Expand All @@ -471,10 +471,10 @@ match.array.contains = function(expectation) {
}, "contains([" + iterableToString(expectation) + "])");
};

match.map = match.typeOf("map");
createMatcher.map = createMatcher.typeOf("map");

match.map.deepEquals = function mapDeepEquals(expectation) {
return match(function(actual) {
createMatcher.map.deepEquals = function mapDeepEquals(expectation) {
return createMatcher(function(actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.size === expectation.size;
return (
Expand All @@ -487,8 +487,8 @@ match.map.deepEquals = function mapDeepEquals(expectation) {
}, "deepEquals(Map[" + iterableToString(expectation) + "])");
};

match.map.contains = function mapContains(expectation) {
return match(function(actual) {
createMatcher.map.contains = function mapContains(expectation) {
return createMatcher(function(actual) {
return (
typeOf(actual) === "map" &&
every(expectation, function(element, key) {
Expand All @@ -498,10 +498,10 @@ match.map.contains = function mapContains(expectation) {
}, "contains(Map[" + iterableToString(expectation) + "])");
};

match.set = match.typeOf("set");
createMatcher.set = createMatcher.typeOf("set");

match.set.deepEquals = function setDeepEquals(expectation) {
return match(function(actual) {
createMatcher.set.deepEquals = function setDeepEquals(expectation) {
return createMatcher(function(actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.size === expectation.size;
return (
Expand All @@ -514,8 +514,8 @@ match.set.deepEquals = function setDeepEquals(expectation) {
}, "deepEquals(Set[" + iterableToString(expectation) + "])");
};

match.set.contains = function setContains(expectation) {
return match(function(actual) {
createMatcher.set.contains = function setContains(expectation) {
return createMatcher(function(actual) {
return (
typeOf(actual) === "set" &&
every(expectation, function(element) {
Expand All @@ -525,13 +525,13 @@ match.set.contains = function setContains(expectation) {
}, "contains(Set[" + iterableToString(expectation) + "])");
};

match.bool = match.typeOf("boolean");
match.number = match.typeOf("number");
match.string = match.typeOf("string");
match.object = match.typeOf("object");
match.func = match.typeOf("function");
match.regexp = match.typeOf("regexp");
match.date = match.typeOf("date");
match.symbol = match.typeOf("symbol");
createMatcher.bool = createMatcher.typeOf("boolean");
createMatcher.number = createMatcher.typeOf("number");
createMatcher.string = createMatcher.typeOf("string");
createMatcher.object = createMatcher.typeOf("object");
createMatcher.func = createMatcher.typeOf("function");
createMatcher.regexp = createMatcher.typeOf("regexp");
createMatcher.date = createMatcher.typeOf("date");
createMatcher.symbol = createMatcher.typeOf("symbol");

module.exports = match;
module.exports = createMatcher;

0 comments on commit 73a9554

Please sign in to comment.