MatchersUtil#contains uses deep equality rather than === for set members

[#169001712]
This commit is contained in:
Steve Gravrock
2021-07-24 14:23:05 -07:00
parent 0170005015
commit c73df57720
5 changed files with 106 additions and 20 deletions

View File

@@ -161,6 +161,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
);
};
j$.isIterable_ = function(value) {
return value && !!value[Symbol.iterator];
};
j$.isDataView = function(obj) {
return (
obj !== null &&

View File

@@ -32,23 +32,45 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
* @returns {boolean} True if `needle` was found in `haystack`
*/
MatchersUtil.prototype.contains = function(haystack, needle) {
if (j$.isSet(haystack)) {
return haystack.has(needle);
if (!haystack) {
return false;
}
if (
Object.prototype.toString.apply(haystack) === '[object Array]' ||
(!!haystack && !haystack.indexOf)
) {
if (j$.isSet(haystack)) {
// Try .has() first. It should be faster in cases where
// needle === something in haystack. Fall back to .equals() comparison
// if that fails.
if (haystack.has(needle)) {
return true;
}
}
if (j$.isIterable_(haystack) && !j$.isString_(haystack)) {
// Arrays, Sets, etc.
for (const candidate of haystack) {
if (this.equals(candidate, needle)) {
return true;
}
}
return false;
}
if (haystack.indexOf) {
// Mainly strings
return haystack.indexOf(needle) >= 0;
}
if (j$.isNumber_(haystack.length)) {
// Objects that are shaped like arrays but aren't iterable
for (var i = 0; i < haystack.length; i++) {
if (this.equals(haystack[i], needle)) {
return true;
}
}
return false;
}
return !!haystack && haystack.indexOf(needle) >= 0;
return false;
};
MatchersUtil.prototype.buildFailureMessage = function() {