assertthat
Version:
assertthat provides fluent TDD.
37 lines (36 loc) • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareSets = void 0;
const compare_1 = require("../typeAware/compare");
const size_1 = require("../../size/typeAware/size");
const EqualDiff_1 = require("../../diffs/EqualDiff");
const SetDiff_1 = require("../../diffs/forSets/SetDiff");
const compareSets = function (actual, expected) {
const newDiff = (0, SetDiff_1.setDiff)({
cost: 0,
additions: new Set(),
omissions: new Set(),
equal: new Set()
});
for (const actualSubValue of actual) {
newDiff.additions.add(actualSubValue);
newDiff.cost += (0, size_1.size)(actualSubValue);
}
for (const expectedElement of expected) {
const elementInBothSets = [...newDiff.additions].find((addition) => (0, compare_1.compare)(addition, expectedElement).cost === 0);
if (elementInBothSets !== undefined) {
newDiff.additions.delete(elementInBothSets);
newDiff.equal.add(elementInBothSets);
newDiff.cost -= (0, size_1.size)(elementInBothSets);
}
else {
newDiff.omissions.add(expectedElement);
newDiff.cost += (0, size_1.size)(expectedElement);
}
}
if (newDiff.cost === 0) {
return (0, EqualDiff_1.equalDiff)({ value: actual });
}
return newDiff;
};
exports.compareSets = compareSets;