PixelPiggy/CS_float
0
1const https = require('https'),2 fs = require('fs');3 4/*5 Downloads the given HTTPS file6*/7exports.downloadFile = function(url, cb) {8 https.get(url, function(res) {9 let errored = false;10 11 if (res.statusCode !== 200 && !errored) {12 cb();13 return;14 }15 16 res.setEncoding('utf8');17 let data = '';18 19 res.on('error', function(err) {20 cb();21 errored = true;22 });23 24 res.on('data', function(chunk) {25 data += chunk;26 });27 28 res.on('end', function() {29 cb(data);30 });31 });32};33 34/*35 Returns a boolean as to whether the specified path is a directory and exists36*/37exports.isValidDir = function(path) {38 try {39 return fs.statSync(path).isDirectory();40 } catch (e) {41 return false;42 }43};44 45/*46 Returns a boolean as to whether the string only contains numbers47*/48exports.isOnlyDigits = function (num) {49 return /^\d+$/.test(num);50};51 52/*53 Filters the keys in the given object and returns new one54*/55exports.filterKeys = function (keys, obj) {56 return keys.reduce((result, key) => {57 if (key in obj) result[key] = obj[key];58 return result;59 }, {});60};61 62/*63 Removes keys with null values64 */65exports.removeNullValues = function (obj) {66 return Object.keys(obj).reduce((result, key) => {67 if (key in obj && obj[key] !== null) {68 result[key] = obj[key];69 }70 71 return result;72 }, {});73};74 75/*76 Converts the given unsigned 64 bit integer into a signed 64 bit integer77 */78exports.unsigned64ToSigned = function (num) {79 const mask = 1n << 63n;80 return (BigInt(num)^mask) - mask;81};82 83/*84 Converts the given signed 64 bit integer into an unsigned 64 bit integer85 */86exports.signed64ToUnsigned = function (num) {87 const mask = 1n << 63n;88 return (BigInt(num)+mask) ^ mask;89};90 91/*92 Checks whether the given ID is a SteamID6493 */94exports.isSteamId64 = function (id) {95 id = BigInt(id);96 const universe = id >> 56n;97 if (universe > 5n) return false;98 99 const instance = (id >> 32n) & (1n << 20n)-1n;100 101 // There are currently no documented instances above 4, but this is for good measure102 return instance <= 32n;103};104 105/*106 Chunks array into sub-arrays of the given size107 */108exports.chunkArray = function (arr, size) {109 return new Array(Math.ceil(arr.length / size)).fill().map(_ => arr.splice(0,size));110};111 112/*113 Shuffle array - O(N LOG N) so it shouldn't be used for super-large arrays114 */115exports.shuffleArray = function (arr) {116 return arr.map(value => ({ value, sort: Math.random() }))117 .sort((a, b) => a.sort - b.sort)118 .map(({ value }) => value)119}120 121 