UNPKG

@chinmayn00b/faker-ind

Version:

Generate Fake Contextual Data For India Region

54 lines 1.49 kB
import { alphaNumericChars, hexaDecimalChars } from './common/char'; /** * Generate a random number between min and max * * @param min (optional, default: 0) * @param max (optional, default: 9) * @returns number */ export function number(min = 0, max = 9) { if (min > max) { [min, max] = [max, min]; } // Calculate the range const range = max - min + 1; // Generate random number and scale it to the range const randomInRange = Math.floor(Math.random() * range); // Shift the random number to start from the minimum value return randomInRange + min; } export function boolean() { return Boolean(number(0, 1)); } export function arrayElement(arr) { const randomIndex = number(0, arr.length - 1); return arr[randomIndex]; } export function objectElement(obj) { return arrayElement(Object.keys(obj)); } export function alphanumeric(_count) { let count = _count; if (count <= 0) { count = 1; } let alphaChar = ''; for (let i = 0; i < count; i++) { const randomChar = arrayElement(alphaNumericChars); alphaChar += randomChar; } return alphaChar; } export function hexadecimal(_count) { let count = _count; if (count <= 0) { count = 1; } let hexChar = ''; for (let i = 0; i < count; i++) { const randomChar = arrayElement(hexaDecimalChars); hexChar += randomChar; } return `0x${hexChar}`; } //# sourceMappingURL=random.js.map