Utilsbox is your go-to toolkit, offering a wide array of practical utility functions to enhance your application's functionality and streamline development.
Returns the current system time.
const Utils = require("utilsbox");
const time = Utils.getTime();
console.log(`The time is ${time});
Returns the current time represented in milliseconds.
const Utils = require("utilsbox");
const time = Utils.getTimeMillis();
console.log(`The time in milliseconds is ${time});
Terminates the application and generates a report in the 'crash_reports' folder within the app directory.
-
report
: The report detailing the circumstances of the application exit.
const Utils = require("utilsbox");
Utils.exitApp(`Something went wrong!`);
Delays code execution by a specified duration.
-
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!");
})();
Waits until the specified callback
function returns a true
value before continuing execution.
-
ms
: The time interval, in milliseconds, between successive checks of thecallback
. -
callback
: The function that will be executed repeatedly until it returns atrue
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!");
})();
Finds an element within an array based on a provided condition.
-
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!");
})();
Converts dashed arguments into a JSON Object.
-
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}!");
})();
Encrypts the provided string using the specified encryption key and initialization vector (IV).
-
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}`);
})();
Decrypts the specified string using the provided decryption key and initialization vector (IV).
-
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
})();
Generates a non-reversible cryptographic hash of the provided string, commonly used for secure storage of passwords and sensitive information.
-
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}`);
})();
Creates a randomly generated string of the specified length.
-
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}`);
})();
Generates a random number within a specified range or with a certain number of digits.
-
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}`);
})();
Generates a random number within the specified range.
-
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}`);
})();
Generates a TOTP secret.
const Utils = require("utilsbox");
(async () => {
const totp_secret = await Utils.generateTOTPSecret();
console.log(`TOTP Secret: ${totp_secret}`);
})();
Generates a Time-Based One-Time Password (TOTP) code using the provided secret.
-
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}`);
})();