From 0aff36afbe83c42e01559b891831484df56f6ea5 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sat, 28 May 2022 13:39:46 +0000 Subject: [PATCH] fix(utils): add hasProperty back. 2x faster than Reflect.has --- helpers/messages/getMessages.ts | 9 +++++---- template/minimal/src/utils/helpers.ts | 5 +++-- testss/utils.test.ts | 18 +++++++++++++++++- util/utils.ts | 10 ++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/helpers/messages/getMessages.ts b/helpers/messages/getMessages.ts index da565777f..8dc3c9088 100644 --- a/helpers/messages/getMessages.ts +++ b/helpers/messages/getMessages.ts @@ -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"); } diff --git a/template/minimal/src/utils/helpers.ts b/template/minimal/src/utils/helpers.ts index 790f0f32e..84719c48e 100644 --- a/template/minimal/src/utils/helpers.ts +++ b/template/minimal/src/utils/helpers.ts @@ -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"); } diff --git a/testss/utils.test.ts b/testss/utils.test.ts index f1d86596a..4b7810239 100644 --- a/testss/utils.test.ts +++ b/testss/utils.test.ts @@ -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); + }, +}); \ No newline at end of file diff --git a/util/utils.ts b/util/utils.ts index 539ab65c6..e4c6339f3 100644 --- a/util/utils.ts +++ b/util/utils.ts @@ -13,3 +13,13 @@ export function delay(ms: number): Promise { 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( + obj: T, + prop: Y, +): obj is T & Record { + return obj.hasOwnProperty(prop); +}