From 39c49bc8a28acc525a6fcfa7d0a86b867276bcaf Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Mon, 30 May 2022 13:56:12 +0200 Subject: [PATCH 1/3] fix(sort): convert Date to ISO string --- src/runtime/query/match/utils.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/runtime/query/match/utils.ts b/src/runtime/query/match/utils.ts index 84ae667cb..9cbbc8285 100644 --- a/src/runtime/query/match/utils.ts +++ b/src/runtime/query/match/utils.ts @@ -67,9 +67,18 @@ export const sortList = (data: any[], params: SortOptions) => { for (const key of keys) { data = data.sort((a, b) => { const values = [get(a, key), get(b, key)] - // `null` values are treated as `"null"` strings and ordered alphabetically - // Turn `null` values into `undefined` so they place at the end of the list - .map(value => value === null ? undefined : value) + .map((value) => { + // `null` values are treated as `"null"` strings and ordered alphabetically + // Turn `null` values into `undefined` so they place at the end of the list + if (value === null) { + return undefined + } + // Conver Date object to ISO string + if (value instanceof Date) { + return value.toISOString() + } + return value + }) if (params[key] === 0) { values.reverse() } From b8abc8b7acce869e647804c805373f9974b7e823 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Mon, 30 May 2022 14:22:36 +0200 Subject: [PATCH 2/3] test: add tests --- test/features/query/query.test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/features/query/query.test.ts b/test/features/query/query.test.ts index c4d1645a9..70c0f8e4c 100644 --- a/test/features/query/query.test.ts +++ b/test/features/query/query.test.ts @@ -75,6 +75,28 @@ describe('Database Provider', () => { }) }) + test('Apply sort Date', async () => { + const dates = [ + { date: new Date('2022-01-01 00:00:00.001Z') }, + { date: new Date('2021-01-01 00:00:00.001Z') }, + { date: new Date('2020-01-01 00:00:00.001Z') }, + { date: new Date('2019-01-01 00:00:00.001Z') }, + { date: new Date('2018-01-01 00:00:00.001Z') }, + { date: new Date('1900-01-01 00:00:00.001Z') } + ] + const fetcher = createPipelineFetcher(() => Promise.resolve(dates.sort(() => 1 - Math.random()))) + + const sortedByDate1 = await createQuery(fetcher).sort({ date: 1 }).find() + ;([...dates].reverse()).forEach(({ date }, index) => { + expect(sortedByDate1[index].date).toBe(date) + }) + + const sortedByDate0 = await createQuery(fetcher).sort({ date: 0 }).find() + dates.forEach(({ date }, index) => { + expect(sortedByDate0[index].date).toBe(date) + }) + }) + test('Apply sort $numeric', async () => { // sort string alphabetically const nonNumericSort = await createQuery(pipelineFetcher).sort({ numberString: 1 }).find() From 38cd3396aa29f8202e43e5c927b5bbda0b56c404 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 30 May 2022 14:42:55 +0000 Subject: [PATCH 3/3] test: fix --- test/features/query/query.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/features/query/query.test.ts b/test/features/query/query.test.ts index 201858bd9..02476a902 100644 --- a/test/features/query/query.test.ts +++ b/test/features/query/query.test.ts @@ -91,7 +91,7 @@ describe('Database Provider', () => { expect(sortedByDate1[index].date).toBe(date) }) - const sortedByDate0 = await createQuery(fetcher).sort({ date: 0 }).find() + const sortedByDate0 = await createQuery(fetcher).sort({ date: -1 }).find() dates.forEach(({ date }, index) => { expect(sortedByDate0[index].date).toBe(date) })