Skip to content

Commit

Permalink
fix: remove uses of the spread operator
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalvata committed Aug 24, 2022
1 parent c410f05 commit 7e8a8c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 11 additions & 12 deletions src/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,23 @@ module.exports = async function aggregate(collection, params) {
const $sort = generateSort(params);
const $limit = params.limit + 1;

let addFields = [];
let cleanUp = [];
let aggregation;
if (params.sortCaseInsensitive) {
addFields = [{ $addFields: { __lc: { $toLower: '$' + params.paginatedField } } }];
cleanUp = [{ $project: { __lc: 0 } }];
aggregation = params.aggregation.concat([
{ $addFields: { __lc: { $toLower: '$' + params.paginatedField } } },
{ $match },
{ $sort },
{ $limit },
{ $project: { __lc: 0 } },
]);
} else {
aggregation = params.aggregation.concat([{ $match }, { $sort }, { $limit }]);
}
const aggregation = params.aggregation.concat([
...addFields,
{ $match },
{ $sort },
{ $limit },
...cleanUp,
]);

// Aggregation options:
// https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#aggregate
// https://mongodb.github.io/node-mongodb-native/4.0/interfaces/aggregateoptions.html
const options = { ...params.options };
const options = Object.assign({}, params.options);
/**
* IMPORTANT
*
Expand Down
10 changes: 6 additions & 4 deletions src/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ module.exports = async function(collection, params) {
let response;
if (params.sortCaseInsensitive) {
// For case-insensitive sorting, we need to work with an aggregation:
response = aggregate(collection, {
...params,
aggregation: params.query ? [{ $match: params.query }] : [],
});
response = aggregate(
collection,
Object.assign({}, params, {
aggregation: params.query ? [{ $match: params.query }] : [],
})
);
} else {
// Need to repeat `params.paginatedField` default value ('_id') since it's set in 'sanitizeParams()'
params = _.defaults(await sanitizeParams(collection, params), { query: {} });
Expand Down

0 comments on commit 7e8a8c9

Please sign in to comment.