From bb7f1d8f4791a59290ab0167eabd7365cb3fadd5 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Thu, 1 Apr 2021 23:45:00 +0300 Subject: [PATCH] use custom random element function instead of lodash --- app/plus/service.ts | 2 +- package-lock.json | 1 - package.json | 1 - pages/maps.tsx | 2 +- pages/play/index.tsx | 2 +- prisma/factories/plusStatus.ts | 10 +++++----- utils/arrays.ts | 16 ++++++++++++++++ utils/shuffleArray.ts | 6 ------ 8 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 utils/arrays.ts delete mode 100644 utils/shuffleArray.ts diff --git a/app/plus/service.ts b/app/plus/service.ts index 5949585e0..63c0e57ae 100644 --- a/app/plus/service.ts +++ b/app/plus/service.ts @@ -1,10 +1,10 @@ import { PlusRegion, Prisma } from "@prisma/client"; import { httpError } from "@trpc/server"; import prisma from "prisma/client"; +import { shuffleArray } from "utils/arrays"; import { ADMIN_ID } from "utils/constants"; import { getPercentageFromCounts, getVotingRange } from "utils/plus"; import { userBasicSelection } from "utils/prisma"; -import { shuffleArray } from "utils/shuffleArray"; import { suggestionFullSchema } from "utils/validators/suggestion"; import { voteSchema, votesSchema } from "utils/validators/votes"; import { vouchSchema } from "utils/validators/vouch"; diff --git a/package-lock.json b/package-lock.json index 8fdf5f1f7..ba12d4193 100644 --- a/package-lock.json +++ b/package-lock.json @@ -63,7 +63,6 @@ "cross-env": "^7.0.3", "cypress": "^6.8.0", "fishery": "^1.2.0", - "lodash": "^4.17.21", "prettier": "^2.2.1", "prisma": "^2.20.1", "ts-node": "^9.1.1", diff --git a/package.json b/package.json index 96f605d00..f681770a3 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,6 @@ "cross-env": "^7.0.3", "cypress": "^6.8.0", "fishery": "^1.2.0", - "lodash": "^4.17.21", "prettier": "^2.2.1", "prisma": "^2.20.1", "ts-node": "^9.1.1", diff --git a/pages/maps.tsx b/pages/maps.tsx index fcbb4133f..13e13489e 100644 --- a/pages/maps.tsx +++ b/pages/maps.tsx @@ -28,9 +28,9 @@ import HeaderBanner from "components/layout/HeaderBanner"; import { useRouter } from "next/router"; import { ChangeEvent, Fragment, useEffect, useState } from "react"; import { FiCheck, FiFilter, FiRotateCw } from "react-icons/fi"; +import { shuffleArray } from "utils/arrays"; import { stages } from "utils/lists/stages"; import { setManySearchParams } from "utils/setSearchParams"; -import { shuffleArray } from "utils/shuffleArray"; const MapsGeneratorPage = () => { const router = useRouter(); diff --git a/pages/play/index.tsx b/pages/play/index.tsx index c5f7b5978..ffab3e502 100644 --- a/pages/play/index.tsx +++ b/pages/play/index.tsx @@ -12,8 +12,8 @@ import prisma from "prisma/client"; import { getAllLadderRegisteredTeamsForMatches } from "prisma/queries/getAllLadderRegisteredTeamsForMatches"; import { getLadderDay, GetLadderDayData } from "prisma/queries/getLadderDay"; import { Fragment } from "react"; +import { shuffleArray } from "utils/arrays"; import { getLadderRounds } from "utils/playFunctions"; -import { shuffleArray } from "utils/shuffleArray"; interface Props { ladderDay: GetLadderDayData; diff --git a/prisma/factories/plusStatus.ts b/prisma/factories/plusStatus.ts index 5e99c822d..bac3ed9ea 100644 --- a/prisma/factories/plusStatus.ts +++ b/prisma/factories/plusStatus.ts @@ -1,21 +1,21 @@ +import { PlusStatus } from "@prisma/client"; import { Factory } from "fishery"; -import { PlusStatus, PlusRegion } from "@prisma/client"; +import { randomElement } from "utils/arrays"; import prisma from "../client"; -import _ from "lodash"; export default Factory.define(({ params, onCreate }) => { - onCreate(plusStatus => { + onCreate((plusStatus) => { return prisma.plusStatus.create({ data: plusStatus }); }); return { userId: 1, // TODO: automatically build a User object, if necessary membershipTier: 1, - region: _.sample(Object.values(PlusRegion))!, + region: randomElement(["EU", "NA"]), voucherId: null, vouchTier: null, canVouchFor: null, canVouchAgainAfter: null, - nameForVoting: null + nameForVoting: null, }; }); diff --git a/utils/arrays.ts b/utils/arrays.ts new file mode 100644 index 000000000..a2c38d95a --- /dev/null +++ b/utils/arrays.ts @@ -0,0 +1,16 @@ +export const shuffleArray = (array: T[]) => { + return array + .map((a) => ({ sort: Math.random(), value: a })) + .sort((a, b) => a.sort - b.sort) + .map((a) => a.value); +}; + +/** + * Get random element from array. + * @link https://stackoverflow.com/a/5915122 + * @example + * randomElement(["dog", "cat", "horse"]) + */ +export const randomElement = (items: [T, ...T[]]) => { + return items[Math.floor(Math.random() * items.length)]; +}; diff --git a/utils/shuffleArray.ts b/utils/shuffleArray.ts deleted file mode 100644 index fd984115f..000000000 --- a/utils/shuffleArray.ts +++ /dev/null @@ -1,6 +0,0 @@ -export const shuffleArray = (array: T[]) => { - return array - .map((a) => ({ sort: Math.random(), value: a })) - .sort((a, b) => a.sort - b.sort) - .map((a) => a.value); -};