Clean up some type guards

This commit is contained in:
Rory&
2026-03-14 04:09:37 +01:00
parent 6e8292508e
commit 0370985a4e
4 changed files with 6 additions and 9 deletions
@@ -56,7 +56,7 @@ router.get(
if (parsedLimit < 1 || parsedLimit > 100) throw new HTTPError("limit must be between 1 and 100", 422);
if (sort_order) {
if (typeof sort_order != "string" || ["desc", "asc"].indexOf(sort_order) == -1)
if (["desc", "asc"].indexOf(sort_order) == -1)
throw FieldErrors({
sort_order: {
message: "Value must be one of ('desc', 'asc').",
+3 -3
View File
@@ -40,11 +40,11 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
let data: Payload;
if (
(buffer instanceof Buffer && buffer[0] === 123) || // ASCII 123 = `{`. Bad check for JSON
(Buffer.isBuffer(buffer) && buffer[0] === 123) || // ASCII 123 = `{`. Bad check for JSON
typeof buffer === "string"
) {
data = bigIntJson.parse(buffer.toString());
} else if (this.encoding === "json" && buffer instanceof Buffer) {
} else if (this.encoding === "json" && Buffer.isBuffer(buffer)) {
if (this.compress === "zlib-stream") {
try {
buffer = this.inflate!.process(buffer);
@@ -59,7 +59,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
}
}
data = bigIntJson.parse(buffer as string);
} else if (this.encoding === "etf" && buffer instanceof Buffer && erlpack) {
} else if (this.encoding === "etf" && Buffer.isBuffer(buffer) && erlpack) {
try {
data = erlpack.unpack(buffer);
} catch {
+1 -1
View File
@@ -199,7 +199,7 @@ export class User extends BaseClass {
validate() {
if (this.discriminator) {
const discrim = Number(this.discriminator);
if (isNaN(discrim) || !(typeof discrim == "number") || !Number.isInteger(discrim) || discrim <= 0 || discrim >= 10000)
if (isNaN(discrim) || !Number.isInteger(discrim) || discrim <= 0 || discrim >= 10000)
throw FieldErrors({
discriminator: {
message: "Discriminator must be a number.",
+1 -4
View File
@@ -19,10 +19,7 @@
export class DateBuilder {
private date: Date;
// constructors
constructor(date = new Date()) {
if (!(date instanceof Date)) {
throw new Error("Invalid date object.");
}
constructor(date: Date = new Date()) {
this.date = new Date(date.getTime()); // Create a copy to avoid mutating the original date
}