refactor: extract shuffleArray to src/utils

This commit is contained in:
Vyacheslav Matyukhin 2022-04-14 23:30:49 +03:00
parent 4da6e08448
commit 71ab64343f
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
2 changed files with 9 additions and 9 deletions

View File

@ -1,6 +1,7 @@
/* Imports */ /* Imports */
import fs from "fs"; import fs from "fs";
import { shuffleArray } from "../../../utils";
import { pgRead } from "../../database/pg-wrapper"; import { pgRead } from "../../database/pg-wrapper";
/* Definitions */ /* Definitions */
@ -13,15 +14,6 @@ let getQualityIndicators = (question) =>
.map((entry) => `${entry[0]}: ${entry[1]}`) .map((entry) => `${entry[0]}: ${entry[1]}`)
.join("; "); .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 */ /* Body */
let main = async () => { let main = async () => {

8
src/utils.ts Normal file
View File

@ -0,0 +1,8 @@
export const shuffleArray = <T>(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;
};