Skip to content

Commit

Permalink
fix(adapter-commons): Fix sorting for embedded objects (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhillerstrom authored Nov 20, 2021
1 parent 37d1a30 commit 9c22f70
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/adapter-commons/src/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function compare (a: any, b: any, compareStrings: any = exports.compareNS
// An in-memory sorting function according to the
// $sort special query parameter
export function sorter ($sort: any) {
let sortLevels = false; // True if $sort has tags with '.' i.e. '{a: 1, b: -1, "c.x.z": 1}'
let sortLevels = 0; // > 0 if $sort has tags with '.' i.e. '{a: 1, b: -1, "c.x.z": 1}'

const getVal = (a: any, sortKeys: any[]) => {
let keys = sortKeys.map(key => key);
Expand All @@ -85,7 +85,7 @@ export function sorter ($sort: any) {
const criteria = Object.keys($sort).map(key => {
const direction = $sort[key];
const keys = key.split('.');
sortLevels = keys.length > 1;
sortLevels += (keys.length > 1) ? 1 : 0;

return { keys, direction };
});
Expand Down
49 changes: 49 additions & 0 deletions packages/adapter-commons/test/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,55 @@ describe('@feathersjs/adapter-commons', () => {
{ _id: 2, item: { category: "cookies", type: "chocolate chip" }, amount: 50 }
]);
});

it('embedded sort 3', () => {
const sort = sorter({
"item.category": 1,
"item.type": 1,
amount: -1
});

assert.deepStrictEqual(data.sort(sort), [
{ _id: 6, item: { category: "brownies", type: "blondie" }, amount: 10 },
{ _id: 5, item: { category: "cake", type: "carrot" }, amount: 20 },
{ _id: 1, item: { category: "cake", type: "chiffon" }, amount: 10 },
{ _id: 4, item: { category: "cake", type: "lemon" }, amount: 30 },
{ _id: 2, item: { category: "cookies", type: "chocolate chip" }, amount: 50 },
{ _id: 3, item: { category: "cookies", type: "chocolate chip" }, amount: 15 }
]);
});

it('embedded sort 4', () => {
const sort = sorter({
amount: -1,
"item.category": 1
});

assert.deepStrictEqual(data.sort(sort), [
{ _id: 2, item: { category: "cookies", type: "chocolate chip" }, amount: 50 },
{ _id: 4, item: { category: "cake", type: "lemon" }, amount: 30 },
{ _id: 5, item: { category: "cake", type: "carrot" }, amount: 20 },
{ _id: 3, item: { category: "cookies", type: "chocolate chip" }, amount: 15 },
{ _id: 6, item: { category: "brownies", type: "blondie" }, amount: 10 },
{ _id: 1, item: { category: "cake", type: "chiffon" }, amount: 10 }
]);
});

it('embedded sort 5', () => {
const sort = sorter({
"item.category": 1,
amount: 1
});

assert.deepStrictEqual(data.sort(sort), [
{ _id: 6, item: { category: "brownies", type: "blondie" }, amount: 10 },
{ _id: 1, item: { category: "cake", type: "chiffon" }, amount: 10 },
{ _id: 5, item: { category: "cake", type: "carrot" }, amount: 20 },
{ _id: 4, item: { category: "cake", type: "lemon" }, amount: 30 },
{ _id: 3, item: { category: "cookies", type: "chocolate chip" }, amount: 15 },
{ _id: 2, item: { category: "cookies", type: "chocolate chip" }, amount: 50 }
]);
});
});

});

0 comments on commit 9c22f70

Please sign in to comment.