mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
So long, long (#1994)
* refactor: remove long dep * fix linter issue * remove file extensions * optimize methods
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const Long = require('long');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
// Discord epoch (2015-01-01T00:00:00.000Z)
|
||||
const EPOCH = 1420070400000;
|
||||
@@ -31,8 +31,9 @@ class SnowflakeUtil {
|
||||
*/
|
||||
static generate() {
|
||||
if (INCREMENT >= 4095) INCREMENT = 0;
|
||||
const BINARY = `${pad((Date.now() - EPOCH).toString(2), 42)}0000100000${pad((INCREMENT++).toString(2), 12)}`;
|
||||
return Long.fromString(BINARY, 2).toString();
|
||||
// eslint-disable-next-line max-len
|
||||
const BINARY = `${(Date.now() - EPOCH).toString(2).padStart(42, '0')}0000100000${(INCREMENT++).toString(2).padStart(12, '0')}`;
|
||||
return Util.binaryToID(BINARY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +53,7 @@ class SnowflakeUtil {
|
||||
* @returns {DeconstructedSnowflake} Deconstructed snowflake
|
||||
*/
|
||||
static deconstruct(snowflake) {
|
||||
const BINARY = pad(Long.fromString(snowflake).toString(2), 64);
|
||||
const BINARY = Util.idToBinary(snowflake).toString(2).padStart(64, '0');
|
||||
const res = {
|
||||
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
|
||||
workerID: parseInt(BINARY.substring(42, 47), 2),
|
||||
@@ -68,8 +69,4 @@ class SnowflakeUtil {
|
||||
}
|
||||
}
|
||||
|
||||
function pad(v, n, c = '0') {
|
||||
return String(v).length >= n ? String(v) : (String(c).repeat(n) + v).slice(-n);
|
||||
}
|
||||
|
||||
module.exports = SnowflakeUtil;
|
||||
|
||||
Reference in New Issue
Block a user