mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-31 16:00:07 +00:00
fix(utils): add hasProperty back. 2x faster than Reflect.has
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { Bot } from "../../bot.ts";
|
||||
import { DiscordMessage } from "../../types/discord.ts";
|
||||
import { hasProperty } from "../../util/utils.ts";
|
||||
|
||||
/** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
|
||||
export async function getMessages(
|
||||
@@ -47,17 +48,17 @@ export interface GetMessagesAfter extends GetMessagesLimit {
|
||||
export type GetMessagesOptions = GetMessagesAfter | GetMessagesBefore | GetMessagesAround | GetMessagesLimit;
|
||||
|
||||
export function isGetMessagesAfter(options: GetMessagesOptions): options is GetMessagesAfter {
|
||||
return Reflect.has(options, "after");
|
||||
return hasProperty(options, "after");
|
||||
}
|
||||
|
||||
export function isGetMessagesBefore(options: GetMessagesOptions): options is GetMessagesBefore {
|
||||
return Reflect.has(options, "before");
|
||||
return hasProperty(options, "before");
|
||||
}
|
||||
|
||||
export function isGetMessagesAround(options: GetMessagesOptions): options is GetMessagesAround {
|
||||
return Reflect.has(options, "around");
|
||||
return hasProperty(options, "around");
|
||||
}
|
||||
|
||||
export function isGetMessagesLimit(options: GetMessagesOptions): options is GetMessagesLimit {
|
||||
return Reflect.has(options, "limit");
|
||||
return hasProperty(options, "limit");
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
CreateApplicationCommand,
|
||||
getGuild,
|
||||
Guild,
|
||||
hasProperty,
|
||||
MakeRequired,
|
||||
upsertApplicationCommands,
|
||||
} from "../../deps.ts";
|
||||
@@ -131,11 +132,11 @@ export function humanizeMilliseconds(milliseconds: number) {
|
||||
export function isSubCommand(
|
||||
data: subCommand | subCommandGroup,
|
||||
): data is subCommand {
|
||||
return !Reflect.has(data, "subCommands");
|
||||
return !hasProperty(data, "subCommands");
|
||||
}
|
||||
|
||||
export function isSubCommandGroup(
|
||||
data: subCommand | subCommandGroup,
|
||||
): data is subCommandGroup {
|
||||
return Reflect.has(data, "subCommands");
|
||||
return hasProperty(data, "subCommands");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { formatImageURL, iconBigintToHash, iconHashToBigInt, validateLength } from "../mod.ts";
|
||||
import { formatImageURL, hasProperty, iconBigintToHash, iconHashToBigInt, validateLength } from "../mod.ts";
|
||||
import { bigintToSnowflake, snowflakeToBigint } from "../util/bigint.ts";
|
||||
import { removeTokenPrefix } from "../util/token.ts";
|
||||
import { assertEquals, assertNotEquals } from "./deps.ts";
|
||||
@@ -168,3 +168,19 @@ Deno.test({
|
||||
assertEquals(validateLength("test", { min: 3, max: 6 }), true);
|
||||
},
|
||||
});
|
||||
|
||||
const obj = { prop: "lts372005" };
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] hasProperty does HAVE property",
|
||||
fn() {
|
||||
assertEquals(hasProperty(obj, "prop"), true);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] hasProperty does NOT HAVE property",
|
||||
fn() {
|
||||
assertEquals(hasProperty(obj, "lts372005"), false);
|
||||
},
|
||||
});
|
||||
@@ -13,3 +13,13 @@ export function delay(ms: number): Promise<void> {
|
||||
export function formatImageURL(url: string, size: ImageSize = 128, format?: ImageFormat) {
|
||||
return `${url}.${format || (url.includes("/a_") ? "gif" : "jpg")}?size=${size}`;
|
||||
}
|
||||
|
||||
// Typescript is not so good as we developers so we need this little utility function to help it out
|
||||
// Taken from https://fettblog.eu/typescript-hasownproperty/
|
||||
/** TS save way to check if a property exists in an object */
|
||||
export function hasProperty<T extends {}, Y extends PropertyKey = string>(
|
||||
obj: T,
|
||||
prop: Y,
|
||||
): obj is T & Record<Y, unknown> {
|
||||
return obj.hasOwnProperty(prop);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user