@0xtld/tair-node
Version:
A Node.js package for Tair functionality with configuration, core, and helper modules.
60 lines (59 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
/**
* @param seconds
* @description Sleep for a number of seconds
* @returns Promise<void>
*/
const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1000));
/**
* @param email
* @description Check if an email is valid
* @returns boolean
*/
const isEmail = (email) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
/**
* @param array
* @description Shuffle an array
* @returns T[]
*/
function shuffleArray(array) {
const shuffledArray = [...array];
let currentIndex = shuffledArray.length, randomIndex, tempValue;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
tempValue = shuffledArray[currentIndex];
shuffledArray[currentIndex] = shuffledArray[randomIndex];
shuffledArray[randomIndex] = tempValue;
}
return shuffledArray;
}
function randomString(length = 5) {
const str = "abcdefghijklmnopqrstuvwxyz";
let nonce = "";
for (let i = 0; i < length; i++) {
nonce += str.charAt(Math.floor(Math.random() * str.length));
}
return nonce;
}
function randomNonce(length = 17) {
// return '6MJkEoS4Q1hPxW3OZ';
const str = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let nonce = "";
for (let i = 0; i < length; i++) {
nonce += str.charAt(Math.floor(Math.random() * str.length));
}
return nonce;
}
exports.Utils = {
sleep,
isEmail,
shuffleArray,
randomString,
randomNonce,
};