signavio-chai-subset
Advanced tools
Comparing version 1.1.0 to 1.1.1
(function () { | ||
"use strict"; | ||
"use strict"; | ||
/* The following module compatibility snippet has been taken from chai-as-promised */ | ||
/* The following module compatibility snippet has been taken from chai-as-promised */ | ||
// Module systems magic dance. | ||
// Module systems magic dance. | ||
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") { | ||
// NodeJS | ||
module.exports = chaiSubset; | ||
} else if (typeof define === "function" && define.amd) { | ||
// AMD | ||
define(function () { | ||
return chaiSubset; | ||
}); | ||
} else { | ||
/*global self: false */ | ||
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") { | ||
// NodeJS | ||
module.exports = chaiSubset; | ||
} else if (typeof define === "function" && define.amd) { | ||
// AMD | ||
define(function () { | ||
return chaiSubset; | ||
}); | ||
} else { | ||
/*global self: false */ | ||
// Other environment (usually <script> tag): plug in to global chai instance directly. | ||
chai.use(chaiSubset); | ||
// Other environment (usually <script> tag): plug in to global chai instance directly. | ||
chai.use(chaiSubset); | ||
self.chaiSubset = chaiSubset; | ||
} | ||
self.chaiSubset = chaiSubset; | ||
} | ||
function chaiSubset(chai, utils) { | ||
var Assertion = chai.Assertion; | ||
var assertionPrototype = Assertion.prototype; | ||
function chaiSubset(chai, utils) { | ||
var Assertion = chai.Assertion; | ||
var assertionPrototype = Assertion.prototype; | ||
Assertion.addChainableMethod('containSubset', function (expected) { | ||
var actual = utils.flag(this, 'object'); | ||
var showDiff = chai.config.showDiff; | ||
Assertion.addChainableMethod('containSubset', function (expected) { | ||
var actual = utils.flag(this, 'object'); | ||
var showDiff = chai.config.showDiff; | ||
assertionPrototype.assert.call(this, | ||
compare(expected, actual), | ||
'expected #{act} to contain subset #{exp}', | ||
'expected #{act} to not contain subset #{exp}', | ||
expected, | ||
actual, | ||
showDiff | ||
); | ||
}); | ||
}; | ||
assertionPrototype.assert.call(this, | ||
compare(expected, actual), | ||
'expected #{act} to contain subset #{exp}', | ||
'expected #{act} to not contain subset #{exp}', | ||
expected, | ||
actual, | ||
showDiff | ||
); | ||
}); | ||
}; | ||
function compare(expected, actual) { | ||
if (typeof(actual) !== typeof(expected)) { | ||
function compare(expected, actual) { | ||
if (typeof(actual) !== typeof(expected)) { | ||
return false; | ||
} | ||
if (typeof(expected) !== 'object' || expected === null) { | ||
return expected === actual; | ||
} | ||
if (!!expected && !actual) { | ||
return false; | ||
} | ||
if (Array.isArray(expected)) { | ||
if (typeof(actual.length) !== 'number') { | ||
return false; | ||
} | ||
if (typeof(expected) !== 'object' || expected === null) { | ||
return expected === actual; | ||
} | ||
if (!!expected && !actual) { | ||
return false; | ||
} | ||
if (Array.isArray(expected)) { | ||
if (typeof(actual.length) !== 'number') { | ||
return false; | ||
} | ||
var aa = Array.prototype.slice.call(actual); | ||
return expected.every(function (exp) { | ||
return aa.some(function (act) { | ||
return compare(exp, act); | ||
}); | ||
var aa = Array.prototype.slice.call(actual); | ||
return expected.every(function (exp) { | ||
return aa.some(function (act) { | ||
return compare(exp, act); | ||
}); | ||
} | ||
return Object.keys(expected).every(function (key) { | ||
var eo = expected[key]; | ||
var ao = actual[key]; | ||
if (typeof(eo) === 'object' && eo !== null && ao !== null) { | ||
return compare(eo, ao); | ||
} | ||
return ao === eo; | ||
}); | ||
} | ||
return Object.keys(expected).every(function (key) { | ||
var eo = expected[key]; | ||
var ao = actual[key]; | ||
if (typeof(eo) === 'object' && eo !== null && ao !== null) { | ||
return compare(eo, ao); | ||
} | ||
return ao === eo; | ||
}); | ||
} | ||
}()); |
{ | ||
"name": "signavio-chai-subset", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "(Fork by Signavio GmbH) Object properties matcher for Chai", | ||
@@ -5,0 +5,0 @@ "main": "lib/chai-subset.js", |
5851