diff --git a/packages/adapter-commons/src/sort.ts b/packages/adapter-commons/src/sort.ts index d83dcd6880..4ad85fd2b9 100644 --- a/packages/adapter-commons/src/sort.ts +++ b/packages/adapter-commons/src/sort.ts @@ -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); @@ -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 }; }); diff --git a/packages/adapter-commons/test/sort.test.ts b/packages/adapter-commons/test/sort.test.ts index 1dde313091..67964b57a5 100644 --- a/packages/adapter-commons/test/sort.test.ts +++ b/packages/adapter-commons/test/sort.test.ts @@ -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 } + ]); + }); }); });