mirror of
https://github.com/NohamR/lanyard-profile-readme.git
synced 2026-05-26 04:17:19 +00:00
Refactoring: made this work the exact same in under 10 lines
This commit is contained in:
@@ -1,44 +1,9 @@
|
|||||||
interface DeconstructedSnowflake {
|
|
||||||
timestamp: number;
|
|
||||||
date: Date;
|
|
||||||
workerID: number;
|
|
||||||
processID: number;
|
|
||||||
increment: number;
|
|
||||||
binary: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const EPOCH = 1420070400000; // Discord's EPOCH
|
const EPOCH = 1420070400000; // Discord's EPOCH
|
||||||
|
|
||||||
export function isSnowflake(snowflake: string): boolean {
|
// Snowflakes will never be a string
|
||||||
const { timestamp } = deconstruct(snowflake);
|
export function isSnowflake(snowflake: number | string): boolean {
|
||||||
return timestamp > EPOCH && timestamp <= 3619093655551;
|
snowflake = Number(snowflake);
|
||||||
|
return (
|
||||||
|
Number.isInteger(+snowflake) && snowflake > 4194304 && !isNaN(new Date(snowflake / 4194304 + EPOCH).getTime())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user