From 71ab64343f32f2b1d0d04df9fd8a6966c922a193 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Thu, 14 Apr 2022 23:30:49 +0300 Subject: [PATCH] refactor: extract shuffleArray to src/utils --- .../pullMetaculusForecastsToCSVForRating.ts | 10 +--------- src/utils.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 src/utils.ts diff --git a/src/backend/utils/evaluations/pullMetaculusForecastsToCSVForRating.ts b/src/backend/utils/evaluations/pullMetaculusForecastsToCSVForRating.ts index 0076f67..6cc2231 100644 --- a/src/backend/utils/evaluations/pullMetaculusForecastsToCSVForRating.ts +++ b/src/backend/utils/evaluations/pullMetaculusForecastsToCSVForRating.ts @@ -1,6 +1,7 @@ /* Imports */ import fs from "fs"; +import { shuffleArray } from "../../../utils"; import { pgRead } from "../../database/pg-wrapper"; /* Definitions */ @@ -13,15 +14,6 @@ let getQualityIndicators = (question) => .map((entry) => `${entry[0]}: ${entry[1]}`) .join("; "); -let shuffleArray = (array) => { - // See: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array - for (let i = array.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [array[i], array[j]] = [array[j], array[i]]; - } - return array; -}; - /* Body */ let main = async () => { diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..649b2a7 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,8 @@ +export const shuffleArray = (array: T[]): T[] => { + // See: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; +};