From a8d7dfbb10a246d4aa28c7965d94f9a97c459c6f Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Tue, 5 Jul 2022 16:03:58 +0800 Subject: [PATCH] feat: add fisher-yates shuffle --- .../nocodb/src/lib/meta/helpers/PagedResponse.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/lib/meta/helpers/PagedResponse.ts b/packages/nocodb/src/lib/meta/helpers/PagedResponse.ts index 00681d9b95..1f79e7228e 100644 --- a/packages/nocodb/src/lib/meta/helpers/PagedResponse.ts +++ b/packages/nocodb/src/lib/meta/helpers/PagedResponse.ts @@ -6,9 +6,21 @@ export class PagedResponseImpl { { limit = 25, offset = 0, - count = null - }: { limit?: number; offset?: number; count?: number } = {} + count = null, + shuffle = false, + }: { + limit?: number; + offset?: number; + count?: number; + shuffle?: boolean; + } = {} ) { + if (shuffle) { + for (let i = list.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [list[i], list[j]] = [list[j], list[i]]; + } + } this.list = list; if (count !== null) { this.pageInfo = { totalRows: +count };