Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Sep 12, 2024
1 parent ba2d527 commit 7956fd7
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 193 deletions.
1 change: 0 additions & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ overrides:
rules:
"@sinonjs/no-prototype-methods/no-prototype-methods": off
max-nested-callbacks: off

10 changes: 5 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ samsam.match(
return "yeah";
},
},
"Yeah!"
"Yeah!",
); // true
```

Expand Down Expand Up @@ -278,7 +278,7 @@ samsam.match(
return "yeah!";
},
},
/yeah/
/yeah/,
); // true
samsam.match(234, /[a-z]/); // false
```
Expand All @@ -296,7 +296,7 @@ samsam.match(
return "42";
},
},
42
42,
); // true
samsam.match(234, 1234); // false
```
Expand Down Expand Up @@ -328,7 +328,7 @@ samsam.match(
},
function () {
return true;
}
},
);

// false
Expand Down Expand Up @@ -367,7 +367,7 @@ samsam.match(
},
{
name: "Chris",
}
},
);

// false
Expand Down
221 changes: 128 additions & 93 deletions lib/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function createMatcher(expectation, message) {

if (arguments.length > 2) {
throw new TypeError(
`Expected 1 or 2 arguments, received ${arguments.length}`
`Expected 1 or 2 arguments, received ${arguments.length}`,
);
}

Expand Down Expand Up @@ -89,21 +89,27 @@ createMatcher.falsy = createMatcher(function (actual) {
}, "falsy");

createMatcher.same = function (expectation) {
return createMatcher(function (actual) {
return expectation === actual;
}, `same(${valueToString(expectation)})`);
return createMatcher(
function (actual) {
return expectation === actual;
},
`same(${valueToString(expectation)})`,
);
};

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

return createMatcher(function (actual) {
return some(arrayOfExpectations, function (expectation) {
return expectation === actual;
});
}, `in(${valueToString(arrayOfExpectations)})`);
return createMatcher(
function (actual) {
return some(arrayOfExpectations, function (expectation) {
return expectation === actual;
});
},
`in(${valueToString(arrayOfExpectations)})`,
);
};

createMatcher.typeOf = function (type) {
Expand All @@ -125,12 +131,15 @@ createMatcher.instanceOf = function (type) {
type,
Symbol.hasInstance,
"type",
"[Symbol.hasInstance]"
"[Symbol.hasInstance]",
);
}
return createMatcher(function (actual) {
return actual instanceof type;
}, `instanceOf(${functionName(type) || objectToString(type)})`);
return createMatcher(
function (actual) {
return actual instanceof type;
},
`instanceOf(${functionName(type) || objectToString(type)})`,
);
};

/**
Expand Down Expand Up @@ -259,111 +268,137 @@ createMatcher.some = function (predicate) {
createMatcher.array = createMatcher.typeOf("array");

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 (
typeOf(actual) === "array" &&
sameLength &&
every(actual, function (element, index) {
var expected = expectation[index];
return typeOf(expected) === "array" &&
typeOf(element) === "array"
? createMatcher.array.deepEquals(expected).test(element)
: deepEqual(expected, element);
})
);
}, `deepEquals([${iterableToString(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 (
typeOf(actual) === "array" &&
sameLength &&
every(actual, function (element, index) {
var expected = expectation[index];
return typeOf(expected) === "array" &&
typeOf(element) === "array"
? createMatcher.array.deepEquals(expected).test(element)
: deepEqual(expected, element);
})
);
},
`deepEquals([${iterableToString(expectation)}])`,
);
};

createMatcher.array.startsWith = function (expectation) {
return createMatcher(function (actual) {
return (
typeOf(actual) === "array" &&
every(expectation, function (expectedElement, index) {
return actual[index] === expectedElement;
})
);
}, `startsWith([${iterableToString(expectation)}])`);
return createMatcher(
function (actual) {
return (
typeOf(actual) === "array" &&
every(expectation, function (expectedElement, index) {
return actual[index] === expectedElement;
})
);
},
`startsWith([${iterableToString(expectation)}])`,
);
};

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;

return (
typeOf(actual) === "array" &&
every(expectation, function (expectedElement, index) {
return actual[offset + index] === expectedElement;
})
);
}, `endsWith([${iterableToString(expectation)}])`);
return createMatcher(
function (actual) {
// This indicates the index in which we should start matching
var offset = actual.length - expectation.length;

return (
typeOf(actual) === "array" &&
every(expectation, function (expectedElement, index) {
return actual[offset + index] === expectedElement;
})
);
},
`endsWith([${iterableToString(expectation)}])`,
);
};

createMatcher.array.contains = function (expectation) {
return createMatcher(function (actual) {
return (
typeOf(actual) === "array" &&
every(expectation, function (expectedElement) {
return arrayIndexOf(actual, expectedElement) !== -1;
})
);
}, `contains([${iterableToString(expectation)}])`);
return createMatcher(
function (actual) {
return (
typeOf(actual) === "array" &&
every(expectation, function (expectedElement) {
return arrayIndexOf(actual, expectedElement) !== -1;
})
);
},
`contains([${iterableToString(expectation)}])`,
);
};

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

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 (
typeOf(actual) === "map" &&
sameLength &&
every(actual, function (element, key) {
return expectation.has(key) && expectation.get(key) === element;
})
);
}, `deepEquals(Map[${iterableToString(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 (
typeOf(actual) === "map" &&
sameLength &&
every(actual, function (element, key) {
return (
expectation.has(key) && expectation.get(key) === element
);
})
);
},
`deepEquals(Map[${iterableToString(expectation)}])`,
);
};

createMatcher.map.contains = function mapContains(expectation) {
return createMatcher(function (actual) {
return (
typeOf(actual) === "map" &&
every(expectation, function (element, key) {
return actual.has(key) && actual.get(key) === element;
})
);
}, `contains(Map[${iterableToString(expectation)}])`);
return createMatcher(
function (actual) {
return (
typeOf(actual) === "map" &&
every(expectation, function (element, key) {
return actual.has(key) && actual.get(key) === element;
})
);
},
`contains(Map[${iterableToString(expectation)}])`,
);
};

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

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 (
typeOf(actual) === "set" &&
sameLength &&
every(actual, function (element) {
return expectation.has(element);
})
);
}, `deepEquals(Set[${iterableToString(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 (
typeOf(actual) === "set" &&
sameLength &&
every(actual, function (element) {
return expectation.has(element);
})
);
},
`deepEquals(Set[${iterableToString(expectation)}])`,
);
};

createMatcher.set.contains = function setContains(expectation) {
return createMatcher(function (actual) {
return (
typeOf(actual) === "set" &&
every(expectation, function (element) {
return actual.has(element);
})
);
}, `contains(Set[${iterableToString(expectation)}])`);
return createMatcher(
function (actual) {
return (
typeOf(actual) === "set" &&
every(expectation, function (element) {
return actual.has(element);
})
);
},
`contains(Set[${iterableToString(expectation)}])`,
);
};

createMatcher.bool = createMatcher.typeOf("boolean");
Expand Down
Loading

0 comments on commit 7956fd7

Please sign in to comment.