Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect results of nested object checks as new object #65

Merged
merged 2 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ var addToResult = function(fn, name){
};
};

// takes an existing comparator function and returns a new comparator function that runs the
// existing comparator with an empty object results object. if that existing comparator execution
// evaluates to true the results it produced are included as part of the larger result set.
// this is done to include results from comparisons made within sub-objects at the expected level of
// nesting within the final results object.
var addResultsToNewObject = function(fn, name){
return function(a, b, aParent, bParent, prop, compares, options) {
var existingResult = options.result;
options.result = {};

var res = fn.apply(this, arguments);
if (res) {
// if we are comparing a sub-object assign results to the given prop,
// if we are comparing the root assign all props to results
if (prop !== undefined) {
existingResult[prop] = options.result;
} else {
assign(existingResult, options.result);
}
}

options.result = existingResult;
return res;
};
};

module.exports = compareHelpers = {
equal: function(a, b, aParent, bParent, prop, compares, options) {
options.checks = [
Expand Down Expand Up @@ -323,9 +349,9 @@ module.exports = compareHelpers = {
options.performedUnion = 0;
options.checks = [
compareHelpers.unionComparesType,
addToResult(compareHelpers.equalBasicTypes,"equalBasicTypes"),
addToResult(compareHelpers.unionArrayLike,"unionArrayLike"),
addToResult(compareHelpers.unionObject, "unionObject")
addToResult(compareHelpers.equalBasicTypes, "equalBasicTypes"),
addToResult(compareHelpers.unionArrayLike, "unionArrayLike"),
addResultsToNewObject(compareHelpers.unionObject, "unionObject")
];
options.getUnions = [];

Expand Down Expand Up @@ -370,7 +396,6 @@ module.exports = compareHelpers = {
// if everything is the same OR doesn't have a property on the left or right (only)
unionObject: function(a, b, aParent, bParent, prop, compares, options){
var subsetCompare = function(a, b, aParent, bParent, prop){

var compare = compares[prop] === undefined ? compares['*'] : compares[prop];

if (! loop(a, b, aParent, bParent, prop, compare, options ) ) {
Expand All @@ -394,11 +419,7 @@ module.exports = compareHelpers = {

var aType = typeof a;
if(aType === 'object' || aType === 'function') {
return h.eachInUnique(a,
subsetCompare,
b,
subsetCompare,
true);
return h.eachInUnique(a, subsetCompare, b, subsetCompare, true);
}
},
// this might be expensive, but work that out later
Expand Down Expand Up @@ -469,7 +490,7 @@ module.exports = compareHelpers = {
compareHelpers.intersectionComparesType,
addToResult(compareHelpers.equalBasicTypes,"equalBasicTypes"),
addToResult(compareHelpers.intersectionArrayLike,"intersectionArrayLike"),
compareHelpers.intersectionObject,
addResultsToNewObject(compareHelpers.intersectionObject),
];

options["default"] = false;
Expand Down
26 changes: 22 additions & 4 deletions src/set-core_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ test('set.difference', function(){

res = set.difference({ completed: true }, { userId: 5 });
equal(res, false); // TODO: probably should be undefined


});

test('set.difference({ function })', function() {
Expand All @@ -134,7 +132,6 @@ test('set.difference({ function })', function() {
});

test('set.union', function(){

// set / subset
var res = set.union({}, { completed: true });
deepEqual(res , {}, "set / subset");
Expand All @@ -150,6 +147,17 @@ test('set.union', function(){

res = set.union({foo: "bar"},{name: "A"});
ok(!res, "values not equal");

res = set.union(
{sort: {name: {first: 'Rick', last: 'Flair'}, type: 'split'}},
{sort: {name: {first: 'Rick', last: 'Flair'}, type: 'split'}}
);

deepEqual(
res,
{sort: {name: {first: 'Rick', last: 'Flair'}, type: 'split'}},
'correctly unifies nested objects'
);
});

test('set.union Array', function(){
Expand Down Expand Up @@ -186,8 +194,18 @@ test('set.intersection', function(){
res = set.intersection({foo: "bar"},{foo: "zed"});
ok(!res, "values not equal");

res = set.intersection({foo: 'bar'},{completed: true});
res = set.intersection({foo: 'bar'}, {completed: true});
deepEqual(res, {foo: 'bar', completed: true}, 'intersection should combine definitions');

res = set.intersection(
{name: {title: 'Ravishing', last: 'Rude'}, type: 'split'},
{name: {first: 'Rick'}}
);
deepEqual(
res,
{name: {title: 'Ravishing', first: 'Rick', last: 'Rude'}, type: 'split'},
'intersects nested objects'
);
});

test('set.intersection Array', function(){
Expand Down