From 6ff4654686f5418f99a4c9d0a5dd10839cb9db43 Mon Sep 17 00:00:00 2001 From: ayntee Date: Tue, 23 Mar 2021 16:47:46 +0400 Subject: [PATCH] refactor(util): remove chooseRandom() (#682) --- src/util/collection.ts | 5 ++--- src/util/utils.ts | 16 +++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/util/collection.ts b/src/util/collection.ts index 78140bf0f..23b2933ea 100644 --- a/src/util/collection.ts +++ b/src/util/collection.ts @@ -1,5 +1,3 @@ -import { chooseRandom } from "./utils.ts"; - export class Collection extends Map { maxSize?: number; @@ -26,7 +24,8 @@ export class Collection extends Map { } random(): V | undefined { - return chooseRandom([...this.values()]); + const array = [...this.values()]; + return array[Math.floor(Math.random() * array.length)]; } find(callback: (value: V, key: K) => boolean) { diff --git a/src/util/utils.ts b/src/util/utils.ts index bed3a6abb..8f435a46f 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,7 +1,5 @@ import { encode } from "../../deps.ts"; import { - Activity, - ActivityType, Errors, GatewayOpcode, GatewayStatusUpdatePayload, @@ -10,7 +8,6 @@ import { SlashCommandOption, SlashCommandOptionChoice, SlashCommandOptionType, - StatusType, UpsertSlashCommandOptions, } from "../types/mod.ts"; import { basicShards, sendWS } from "../ws/shard.ts"; @@ -35,10 +32,6 @@ export function editBotStatus( }); } -export function chooseRandom(array: T[]) { - return array[Math.floor(Math.random() * array.length)]; -} - export async function urlToBase64(url: string) { const buffer = await fetch(url).then((res) => res.arrayBuffer()); const imageStr = encode(buffer); @@ -72,6 +65,7 @@ export const formatImageURL = ( function camelToSnakeCase(text: string) { return text.replace(/ID|[A-Z]/g, ($1) => { if ($1 === "ID") return "_id"; + return `_${$1.toLowerCase()}`; }); } @@ -79,6 +73,7 @@ function camelToSnakeCase(text: string) { function snakeToCamelCase(text: string) { return text.replace(/_id|([-_][a-z])/ig, ($1) => { if ($1 === "_id") return "ID"; + return $1.toUpperCase().replace("_", ""); }); } @@ -87,21 +82,25 @@ function isObject(obj: unknown) { return obj === Object(obj) && !Array.isArray(obj) && typeof obj !== "function"; } + // deno-lint-ignore no-explicit-any export function camelKeysToSnakeCase(obj: Record) { if (isObject(obj)) { // deno-lint-ignore no-explicit-any const convertedObject: Record = {}; + Object.keys(obj) .forEach((key) => { convertedObject[camelToSnakeCase(key)] = camelKeysToSnakeCase( obj[key], ); }); + return convertedObject; } else if (Array.isArray(obj)) { obj = obj.map((element) => camelKeysToSnakeCase(element)); } + return obj; } @@ -110,16 +109,19 @@ export function snakeKeysToCamelCase(obj: Record) { if (isObject(obj)) { // deno-lint-ignore no-explicit-any const convertedObject: Record = {}; + Object.keys(obj) .forEach((key) => { convertedObject[snakeToCamelCase(key)] = snakeKeysToCamelCase( obj[key], ); }); + return convertedObject; } else if (Array.isArray(obj)) { obj = obj.map((element) => snakeKeysToCamelCase(element)); } + return obj; }