Skip to content

feat(keys-and): ability to compare nested list objects #61

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions can-query-logic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,37 @@ QUnit.test("filterMembers basics", function(assert) {
]);
});

QUnit.test("filterMembers nested", function(assert) {
var subset = algebra.filterMembers(
{ filter: { 'employees': {name: 'Patrick' } }},
[{
projectId: 1,
employees: [{
name: 'Patrick',
},
{
name: 'Justin',
}]
}, {
projectId: 2,
employees: [
{
name: 'Justin',
}]
}],
);

assert.deepEqual(subset, [{
projectId: 1,
employees: [{
name: 'Patrick',
},
{
name: 'Justin',
}]
}])
})


QUnit.test("unionMembers basics", function(assert) {
var union = algebra.unionMembers({
Expand Down
39 changes: 23 additions & 16 deletions src/types/keys-and.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ var isMemberSymbol = canSymbol.for("can.isMember");


KeysAnd.prototype.isMember = function(props, root, rootKey) {
var equal = true;
var preKey = rootKey ? rootKey + "." : "";
canReflect.eachKey(this.values, function(value, key) {
var isMember = value && (value[isMemberSymbol] || value.isMember);
if (isMember) {
if (!isMember.call(value, canGet(props, key), root || props, preKey + key)) {
equal = false;
}
} else {
if (value !== canGet(props, key)) {
equal = false;
}
}
});
return equal;
var equal = true;
var preKey = rootKey ? rootKey + "." : "";
canReflect.eachKey(this.values, function(value, key) {
var isMember = value && (value[isMemberSymbol] || value.isMember);
if (isMember) {
if(canReflect.isListLike(props)) {
var match = false;
canReflect.each(props, (objInList) => {
if(match) return;

match = isMember.call(value, canGet(objInList, key), root || props, preKey + key)
})
equal = match;
} else if (!isMember.call(value, canGet(props, key), root || props, preKey + key)) {
equal = false;
}
} else {
if (value !== canGet(props, key)) {
equal = false;
}
}
});
return equal;
};


// ====== DEFINE COMPARISONS ========

// Helpers ----------------------------
Expand Down