Utilsbox
Utilsbox is a package with many utils to make your app cleaner and better
Methods
getTime()
Returns the time
const Utils = require("utilsbox");
const time = Utils.getTime();
console.log(`The time is ${time});
getTimeMillis()
Returns the time in milliseconds
const Utils = require("utilsbox");
const time = Utils.getTimeMillis();
console.log(`The time in milliseconds is ${time});
report
)
exitApp(Exits and writes a report to the app on the crash_reports
folder
This method takes 1 argument,
report
: The report to of the exit
const Utils = require("utilsbox");
Utils.exitApp(`Something went wrong!`);
ms
)
wait(Waits before executing code
This method takes 1 argument,
ms
: The time to wait, in milliseconds
const Utils = require("utilsbox");
(async () => {
console.log("Hello!");
await Utils.wait(5 * 1000);
console.log("Hello 5 seconds later!");
})();
ms
, callback
)
repeatWaitUntil(Waits until the callback
given returns true
This method takes 2 arguments,
ms
: The time to wait between checking the callback, in milliseconds
callback
: The callback which will be executed
const Utils = require("utilsbox");
let a = 0;
(async () => {
console.log("Hello!");
await Utils.repeatWaitUntil(1000, async () => {
if (a >= 10) {
return true;
} else {
a++;
}
});
console.log("'a' is now 10, and it's been 10 seconds!");
})();
key
, iv
, str
)
encryptString(Encrypts the given string
This method takes 3 arguments,
key
: The key to use to encrypt the given string
iv
: The iv (initialization vector) to encrypt the given string
str
: The string to encrypt
const Utils = require("utilsbox");
const key = Buffer.from(await Utils.randomString(32)).toString("base64"); // random encryption key
const iv = Buffer.from(await require("crypto").randomBytes(16)).toString("base64"); // random iv (initialization vector)
(async () => {
console.log(await Utils.encryptString(key, string, `password123`));
})();
key
, iv
, str
)
decryptString(Decrypts the given string
This method takes 3 arguments,
key
: The key to use to decrypt the given string
iv
: The iv (initialization vector) to decrypt the given string
str
: The string to decrypt
const Utils = require("utilsbox");
const key = Buffer.from(await Utils.randomString(32)).toString("base64"); // random encryption key
const iv = Buffer.from(await require("crypto").randomBytes(16)).toString("base64"); // random iv (initialization vector)
(async () => {
console.log(await Utils.decryptString(key, string, await Utils.encryptString(key, string, `password123`))); // Decrypt the encrypted string, output: password123
})();
str
)
hashString(Hash the given string (usually used to store passwords, hard to be reversed)
This method takes 1 argument,
str
: The string to hash
const Utils = require("utilsbox");
(async () => {
console.log(await Utils.hashString("password123"));
})();
len
)
randomString(Generate a random string with the given length
This method takes 1 argument,
len
: The length of the string to generate
const Utils = require("utilsbox");
(async () => {
console.log(await Utils.randomString(32));
})();
len
)
randomNumber(Generate a random number with the given length
This method takes 1 argument,
len
: The length of the number to generate
const Utils = require("utilsbox");
(async () => {
console.log(await Utils.randomNumber(32));
})();
start
, end
)
getNumberBetween(Generate a random number with the given length
This method takes 2 arguments,
start
: The start of the random number
end
: The end of the random number
const Utils = require("utilsbox");
(async () => {
console.log(await Utils.getNumberBetween(1, 10));
})();
ip
)
isCloudflareIP(Returns whenever the given IP is a cloudflare-owned IP.
This method takes 1 argument,
ip
: The IP to test
const Utils = require("utilsbox");
(async () => {
console.log(await Utils.isCloudflareIP("192.168.1.1")); // Random IP
console.log(await Utils.isCloudflareIP("103.22.200.16")); // Random Cloudflare IP
})();