0.6.3 • Published 7 months ago
random-stuff-js v0.6.3
random-stuff-js
A library that provides some random but very useful functions that are commonly used in most javascript projects.
Features
- Generate random number
- Pick random element from array
- Generate random name
- Generate OTP
- Probability
- Minimum/Maximum element of an array
- Wait/Sleep function
Installation
To install random-stuff-js
# with npm:
npm install random-stuff-js --save
# with yarn:
yarn add random-stuff-js
# with pnpm:
pnpm add random-stuff-js
# with bun:
bun add random-stuff-js
Usage
random-stuff-js
can be used in almost any javascript project to achieve the above mentioned features with no extra effort. The functions provided by random-stuff-js
may be categorised into 3 categories:
Random
import { randomNumber, randomElement, randomAdjective, randomAnimal, randomName, generateOTP } from 'random-stuff-js';
console.log(randomNumber(10, 20)) // Any number between 10 and 20 (both numbers included) will be logged
console.log(randomElement([3, 2, 10, 7, 8])) // Any one random element of the array will be logged
console.log(randomAdjective()) // Any one adjective from a list of 25 adjective will be logged
console.log(randomAnimal()) // Any one animal from a list of 200 animals will be logged
console.log(randomName('-')) // A combination of randomAdjective() and randomAnimal() will be logged with a '-' separator in between. Default separator is ' '
console.log(generateOTP(6)) // A 6 digit string OTP will be logged
Math
import { probability, minimumNumber, maximumNumber } from 'random-stuff-js';
if (probability(0.5)) console.log(true) // There is a 50% chance that true will be logged
const array = [89, 6, 99, 2, 50, 10]
console.log(minimumNumber(array)) // 2
console.log(maximumNumber(array)) // 99
Time
import { wait } from 'random-stuff-js';
async function test(time) {
const ref = Date.now()
await wait(time)
return Date.now() - ref
}
test(1000).then(time => console.log(time)) // ~1000