refactor(util): remove chooseRandom() (#682)

This commit is contained in:
ayntee
2021-03-23 16:47:46 +04:00
committed by GitHub
parent 5aef37c776
commit 6ff4654686
2 changed files with 11 additions and 10 deletions
+2 -3
View File
@@ -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
View File
@@ -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;
}