Skip to content

Commit

Permalink
Merge pull request #65 from canjs/object-result-collection
Browse files Browse the repository at this point in the history
collect results of nested object checks as new object
  • Loading branch information
nlundquist authored Jul 28, 2017
2 parents c19fef4 + aa80bb6 commit 710551a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
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 @@ -114,8 +114,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 @@ -133,7 +131,6 @@ test('set.difference({ function })', function() {
});

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

// set / subset
var res = set.union({}, { completed: true });
deepEqual(res , {}, "set / subset");
Expand All @@ -149,6 +146,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 @@ -185,8 +193,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

0 comments on commit 710551a

Please sign in to comment.