utilsbox

1.1.6 • Public • Published

Utilsbox

Utilsbox is your go-to toolkit, offering a wide array of practical utility functions to enhance your application's functionality and streamline development.

Methods

getTime()

Returns the current system time.

const Utils = require("utilsbox");

const time = Utils.getTime();

console.log(`The time is ${time});

getTimeMillis()

Returns the current time represented in milliseconds.

const Utils = require("utilsbox");

const time = Utils.getTimeMillis();

console.log(`The time in milliseconds is ${time});

exitApp(report)

Terminates the application and generates a report in the 'crash_reports' folder within the app directory.

Parameters

  • report: The report detailing the circumstances of the application exit.
const Utils = require("utilsbox");

Utils.exitApp(`Something went wrong!`);

wait(ms)

Delays code execution by a specified duration.

Parameters

  • ms: The duration, in milliseconds, to pause before proceeding with code execution.
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 specified callback function returns a true value before continuing execution.

Parameters

  • ms: The time interval, in milliseconds, between successive checks of the callback.
  • callback: The function that will be executed repeatedly until it returns a true value.
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!");
})();

findIndex(arr, predicate)

Finds an element within an array based on a provided condition.

Parameters

  • arr: The array to search within.
  • predicate: A function used to test each element of the array.
const Utils = require("utilsbox");

const array = ["hello","bye"];

(async () => {
    const hello = Utils.findIndex(array, (v) => { v === "hello" });
    console.log("'hello' is at index ${hello.index} of the array!");
})();

parseDashedArgs(args)

Converts dashed arguments into a JSON Object.

Parameters

  • args: An array containing dashed arguments to be translated.
const Utils = require("utilsbox");

(async () => {
    const args = Utils.parseDashedArgs(["--a=b"]);
    console.log("Argument 'a' has the value ${args.a}!");
})();

encryptString(key, iv, str)

Encrypts the provided string using the specified encryption key and initialization vector (IV).

Parameters

  • key: The encryption key used to encrypt the given string.
  • iv: The initialization vector (IV) utilized in the encryption process.
  • str: The string to be encrypted using the provided key and IV.
const Utils = require("utilsbox");

(async () => {
	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)

	const encrypted_string = await Utils.encryptString(key, iv, "password123");

    console.log(`Encrypted String: ${encrypted_string}`);
})();

decryptString(key, iv, str)

Decrypts the specified string using the provided decryption key and initialization vector (IV).

Parameters

  • key: The decryption key used to decrypt the given string.
  • iv: The initialization vector (IV) used in the decryption process.
  • str: The string to be decrypted using the provided key and IV.
const Utils = require("utilsbox");

(async () => {
	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)
	
	const encrypted_string = await Utils.encryptString(key, iv, "password123");

	const decrypted_string = await Utils.decryptString(key, iv, encrypted_string);
	
    console.log(`Decrypted String: ${decrypted_string}`); // Output: password123
})();

hashString(str)

Generates a non-reversible cryptographic hash of the provided string, commonly used for secure storage of passwords and sensitive information.

Parameters

  • str: The string to be hashed for secure storage or comparison purposes.
const Utils = require("utilsbox");

(async () => {
	const hashed_string = await Utils.hashString("password123"); // SHA512

    console.log(`Hashed String: ${hashed_string}`);
})();

randomString(len)

Creates a randomly generated string of the specified length.

Parameters

  • len: The desired length of the generated random string.
const Utils = require("utilsbox");

(async () => {
	const random_string = await Utils.randomString(32);

    console.log(`Random String: ${random_string}`);
})();

randomNumber(len)

Generates a random number within a specified range or with a certain number of digits.

Parameters

  • len: The desired length or range of the generated random number.
const Utils = require("utilsbox");

(async () => {
	const random_number = await Utils.randomNumber(32);

    console.log(`Random Number: ${random_number}`);
})();

getNumberBetween(start, end)

Generates a random number within the specified range.

Parameters

  • start: The lower limit of the desired random number range.
  • end: The upper limit of the desired random number range.
const Utils = require("utilsbox");

(async () => {
	const random_number = await Utils.getNumberBetween(1, 10);

    console.log(`Random Number: ${random_between_number}`);
})();

generateTOTPSecret()

Generates a TOTP secret.

const Utils = require("utilsbox");

(async () => {
	const totp_secret = await Utils.generateTOTPSecret();

    console.log(`TOTP Secret: ${totp_secret}`);
})();

generateTOTPCode(secret)

Generates a Time-Based One-Time Password (TOTP) code using the provided secret.

Parameters

  • secret: The secret key used to generate the Time-Based One-Time Password (TOTP) code.
const Utils = require("utilsbox");

(async () => {
	const totp_secret = await Utils.generateTOTPSecret();

	const current_totp_code = await Utils.generateTOTPCode(totp_secret);

    console.log(`Current TOTP Code: ${current_totp_code}`);
})();

Readme

Keywords

none

Package Sidebar

Install

npm i utilsbox

Weekly Downloads

0

Version

1.1.6

License

ISC

Unpacked Size

14.4 kB

Total Files

3

Last publish

Collaborators

  • jasonkaranik