So long, long (#1994)

* refactor: remove long dep

* fix linter issue

* remove file extensions

* optimize methods
This commit is contained in:
Drahcirius
2017-11-17 08:37:07 -05:00
committed by Crawl
parent 0cd4a92fb8
commit 8237bc054c
4 changed files with 61 additions and 16 deletions

View File

@@ -1,4 +1,3 @@
const Long = require('long');
const snekfetch = require('snekfetch');
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
@@ -299,7 +298,9 @@ class Util {
*/
static discordSort(collection) {
return collection
.sort((a, b) => a.rawPosition - b.rawPosition || Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber());
.sort((a, b) => a.rawPosition - b.rawPosition ||
parseInt(a.id.slice(0, -10)) - parseInt(b.id.slice(0, -10)) ||
parseInt(a.id.slice(10)) - parseInt(b.id.slice(10)));
}
static setPosition(item, position, relative, sorted, route, reason) {
@@ -316,6 +317,54 @@ class Util {
}
return f;
}
/**
* Transform a snowflake from a decimal string to a bit string
* @param {string} num Snowflake to be transformed
* @returns {string}
* @private
*/
static idToBinary(num) {
let bin = '';
let high = parseInt(num.slice(0, -10)) || 0;
let low = parseInt(num.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;
}
/**
* Transform a snowflake from a bit string to a decimal string
* @param {string} num Bit string to be transformed
* @returns {string}
* @private
*/
static binaryToID(num) {
let dec = '';
while (num.length > 50) {
const high = parseInt(num.slice(0, -32), 2);
const low = parseInt((high % 10).toString(2) + num.slice(-32), 2);
dec = (low % 10).toString() + dec;
num = Math.floor(high / 10).toString(2) + Math.floor(low / 10).toString(2).padStart(32, '0');
}
num = parseInt(num, 2);
while (num > 0) {
dec = (num % 10).toString() + dec;
num = Math.floor(num / 10);
}
return dec;
}
}
module.exports = Util;