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 1 commit
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
29 changes: 19 additions & 10 deletions src/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ var addToResult = function(fn, name){
};
};

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 && prop !== undefined) {
existingResult[prop] = 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 +337,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 +384,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 +407,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 +478,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