utilsbox

    1.0.10 • Public • Published

    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});

    exitApp(report)

    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!`);

    wait(ms)

    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!");
    })();

    repeatWaitUntil(ms, callback)

    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!");
    })();

    encryptString(key, iv, str)

    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`));
    })();

    decryptString(key, iv, str)

    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
    })();

    hashString(str)

    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"));
    })();

    randomString(len)

    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));
    })();

    randomNumber(len)

    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));
    })();

    getNumberBetween(start, end)

    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));
    })();

    isCloudflareIP(ip)

    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
    })();

    Keywords

    none

    Install

    npm i utilsbox

    DownloadsWeekly Downloads

    147

    Version

    1.0.10

    License

    ISC

    Unpacked Size

    12.9 kB

    Total Files

    4

    Last publish

    Collaborators

    • jasonkaranikyt