Files
discordeno/util/utils.ts
2022-02-11 09:49:53 +00:00

29 lines
1020 B
TypeScript

import type { ImageFormat } from "../types/misc/imageFormat.ts";
import type { ImageSize } from "../types/misc/imageSize.ts";
/** Pause the execution for a given amount of milliseconds. */
export function delay(ms: number): Promise<void> {
return new Promise((res): number =>
setTimeout((): void => {
res();
}, ms)
);
}
/** Help format an image url. */
export const 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 */
// deno-lint-ignore ban-types
export function hasProperty<T extends {}, Y extends PropertyKey = string>(
obj: T,
prop: Y,
): obj is T & Record<Y, unknown> {
// deno-lint-ignore no-prototype-builtins
return obj.hasOwnProperty(prop);
}