mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 16:57:22 +00:00
refactor(util): remove chooseRandom() (#682)
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { chooseRandom } from "./utils.ts";
|
||||
|
||||
export class Collection<K, V> extends Map<K, V> {
|
||||
maxSize?: number;
|
||||
|
||||
@@ -26,7 +24,8 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
}
|
||||
|
||||
random(): V | undefined {
|
||||
return chooseRandom<V>([...this.values()]);
|
||||
const array = [...this.values()];
|
||||
return array[Math.floor(Math.random() * array.length)];
|
||||
}
|
||||
|
||||
find(callback: (value: V, key: K) => boolean) {
|
||||
|
||||
+9
-7
@@ -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<T>(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<string, any>) {
|
||||
if (isObject(obj)) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const convertedObject: Record<string, any> = {};
|
||||
|
||||
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<string, any>) {
|
||||
if (isObject(obj)) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const convertedObject: Record<string, any> = {};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user