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

gh 13192 #13195

Merged
merged 1 commit into from
Apr 4, 2023
Merged

gh 13192 #13195

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
37 changes: 29 additions & 8 deletions lib/helpers/update/removeUnusedArrayFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@
*/

module.exports = function removeUnusedArrayFilters(update, arrayFilters) {
const updateKeys = Object.keys(update).map(key => Object.keys(update[key])).reduce((cur, arr) => cur.concat(arr), []);
return arrayFilters.filter(obj => {
const firstKey = Object.keys(obj)[0];
const firstDot = firstKey.indexOf('.');
const arrayFilterKey = firstDot === -1 ? firstKey : firstKey.slice(0, firstDot);

return updateKeys.find(key => key.includes('$[' + arrayFilterKey + ']')) != null;
const updateKeys = Object.keys(update)
.map((key) => Object.keys(update[key]))
.reduce((cur, arr) => cur.concat(arr), []);
return arrayFilters.filter((obj) => {
return _checkSingleFilterKey(obj, updateKeys);
});
};
};

function _checkSingleFilterKey(arrayFilter, updateKeys) {
const firstKey = Object.keys(arrayFilter)[0];

if (firstKey === '$and' || firstKey === '$or') {
if (!Array.isArray(arrayFilter[firstKey])) {
return false;
}
return (
arrayFilter[firstKey].find((filter) =>
_checkSingleFilterKey(filter, updateKeys)
) != null
);
}

const firstDot = firstKey.indexOf('.');
const arrayFilterKey =
firstDot === -1 ? firstKey : firstKey.slice(0, firstDot);

return (
updateKeys.find((key) => key.includes('$[' + arrayFilterKey + ']')) != null
);
}
19 changes: 19 additions & 0 deletions test/helpers/update.removeUnusedArrayFilters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const assert = require('assert');
const removeUnusedArrayFilters = require('../../lib/helpers/update/removeUnusedArrayFilters');

describe('removeUnusedArrayFilters', function() {
it('respects `$or` (gh-10696)', function() {
const update = {
$set: {
'requests.$[i].status.aa': 'ON_GOING',
'requests.$[i].status.bb': 'ON_GOING'
}
};
const arrayFilters = [{ $or: [{ 'i.no': 1 }] }];

const ret = removeUnusedArrayFilters(update, arrayFilters);
assert.deepEqual(ret, [{ $or: [{ 'i.no': 1 }] }]);
});
});