Feat: Refactoring

This commit is contained in:
looskie
2021-06-22 14:07:25 -04:00
parent 4e0af9327f
commit 75870b6c0b
4 changed files with 78 additions and 81 deletions

View File

@@ -1,16 +1,16 @@
export const getFlags = (flag: number): string[] => {
let flags: string[] = [];
if (flag & 1) flags.push("Discord_Employee")
if (flag & 2) flags.push("Partnered_Server_Owner")
if (flag & 4) flags.push("HypeSquad_Events")
if (flag & 8) flags.push("Bug_Hunter_Level_1")
if (flag & 64) flags.push("House_Bravery")
if (flag & 128) flags.push("House_Brilliance")
if (flag & 256) flags.push("House_Balance")
if (flag & 512) flags.push("Early_Supporter")
if (flag & 16384) flags.push("Bug_Hunter_Level_2")
if (flag & 131072) flags.push("Early_Verified_Bot_Developer")
if (flag & 1) flags.push("Discord_Employee");
if (flag & 2) flags.push("Partnered_Server_Owner");
if (flag & 4) flags.push("HypeSquad_Events");
if (flag & 8) flags.push("Bug_Hunter_Level_1");
if (flag & 64) flags.push("House_Bravery");
if (flag & 128) flags.push("House_Brilliance");
if (flag & 256) flags.push("House_Balance");
if (flag & 512) flags.push("Early_Supporter");
if (flag & 16384) flags.push("Bug_Hunter_Level_2");
if (flag & 131072) flags.push("Early_Verified_Bot_Developer");
return flags;
}
};

View File

@@ -1,47 +1,46 @@
interface DeconstructedSnowflake {
timestamp: number;
date: Date;
workerID: number;
processID: number;
increment: number;
binary: string;
timestamp: number;
date: Date;
workerID: number;
processID: number;
increment: number;
binary: string;
}
const EPOCH = 1420070400000; // Discord's EPOCH
export function isSnowflake(snowflake: string): boolean {
const { timestamp } = deconstruct(snowflake);
if (timestamp > EPOCH && timestamp <= 3619093655551) {
return true;
}
return false;
}
export const isSnowflake = (snowflake: string): boolean => {
const { timestamp } = deconstruct(snowflake);
if (timestamp > EPOCH && timestamp <= 3619093655551) return true;
function deconstruct(snowflake: string): DeconstructedSnowflake {
const BINARY = idToBinary(snowflake).padStart(64, "0");
return {
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
get date() {
return new Date(this.timestamp);
},
workerID: parseInt(BINARY.substring(42, 47), 2),
processID: parseInt(BINARY.substring(47, 52), 2),
increment: parseInt(BINARY.substring(52, 64), 2),
binary: BINARY,
};
}
return false;
};
function idToBinary(snowflake: string): string {
let bin = "";
let high = parseInt(snowflake.slice(0, -10)) || 0;
let low = parseInt(snowflake.slice(-10));
while (low > 0 || high > 0) {
bin = String(low & 1) + bin;
low = Math.floor(low / 2);
if (high > 0) {
low += 5000000000 * (high % 2);
high = Math.floor(high / 2);
}
}
return bin;
}
const deconstruct = (snowflake: string): DeconstructedSnowflake => {
const BINARY = idToBinary(snowflake).padStart(64, "0");
return {
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
get date() {
return new Date(this.timestamp);
},
workerID: parseInt(BINARY.substring(42, 47), 2),
processID: parseInt(BINARY.substring(47, 52), 2),
increment: parseInt(BINARY.substring(52, 64), 2),
binary: BINARY,
};
};
const idToBinary = (snowflake: string): string => {
let bin = "";
let high = parseInt(snowflake.slice(0, -10)) || 0;
let low = parseInt(snowflake.slice(-10));
while (low > 0 || high > 0) {
bin = String(low & 1) + bin;
low = Math.floor(low / 2);
if (high > 0) {
low += 5000000000 * (high % 2);
high = Math.floor(high / 2);
}
}
return bin;
};