RandBox - JavaScript Random Data Generation Library
RandBox is a powerful JavaScript random data generation library, offering a rich API for generating various types of random data. It features a modular design, supporting the ES6 module system and Tree Shaking, allowing you to bundle only the functionalities you need.
import RandBox from 'randbox';
const randBox = new RandBox();
console.log(randBox.name()); // 'Sarah Johnson'
console.log(randBox.email()); // 'john.doe@example.com'
console.log(randBox.integer(1, 100)); // 42Installation and Usage
Install via npm
npm install randboxInstall via yarn
yarn add randboxCDN Inclusion
<script src="https://unpkg.com/randbox@latest/build/randbox.min.js"></script>Modular Architecture
RandBox adopts a modular design, breaking down functionality into independent ES6 modules, providing better maintainability and Tree Shaking support.
Core Module (core.js)
Contains the main RandBox constructor, constants, utility functions, and core functionality that other modules depend on.
Exports:
RandBox- Main constructor- Constants:
MAX_INT,MIN_INT,NUMBERS,CHARS_LOWER,CHARS_UPPER,HEX_POOL - Utility Functions:
initOptions,testRange,range,base64,UnsupportedError
Functional Module Overview
Each module contains related functionalities and can be imported separately:
- Basic Data (
basics.js) - Core random number generation
bool,character,floating,integer,natural,string,buffer,hex
- Helper Tools (
helpers.js) - Array and utility functions
capitalize,mixin,unique,n,pad,pick,pickone,pickset,shuffle,weighted
- Text Generation (
text.js) - Text content generation
paragraph,sentence,syllable,word,emoji
- Personal Information (
person.js) - Person-related data
age,birthday,first,last,name,gender,ssn,animal, etc.
- Mobile Devices (
mobile.js) - Mobile device identifiers
android_id,apple_token,wp8_anid2,wp7_anid,bb_pin
- Web Data (
web.js) - Web-related data
avatar,color,domain,email,ip,url,hashtag, etc.
- Geographical Location (
location.js) - Geographical data
address,city,country,coordinates,latitude,longitude,phone, etc.
- Financial Data (
finance.js) - Financial-related data
cc,currency,dollar,euro,iban,luhn_check, etc.
- Music Data (
music.js) - Music-related data
note,chord,tempo,music_genre
- Miscellaneous Tools (
miscellaneous.js) - Various utility functions
coin, dice functions (d4,d6, etc.),guid,hash, date functions,normal
Usage Methods
Import Full Library (Equivalent to Original Version)
import RandBox from 'randbox';
const randBox = new RandBox();
console.log(randBox.name()); // Works exactly the same as the original versionImport Specific Modules (Tree Shaking)
import { RandBox } from 'randbox/src/core.js';
import { name, email } from 'randbox/src/person.js';
// Use as standalone functions
console.log(name()); // Direct function call
// Or extend the RandBox instance with specific modules
const randBox = new RandBox();
Object.assign(randBox, { name, email });
console.log(randBox.name());Import Single Functions
import { name } from 'randbox/src/person.js';
import { email } from 'randbox/src/web.js';
import { natural } from 'randbox/src/basics.js';
// Use directly (Note: 'this' context needs to be bound to a RandBox instance)Advantages and Features
- Tree Shaking: Bundle only the features you need
- Maintainability: Related features are logically grouped
- Modularity: Easy to add new modules or modify existing ones
- ES6 Compatibility: Modern JavaScript module system
- Backward Compatibility:
index.jsprovides the same API as the original version
Dependencies
Each module imports only what it needs from core.js, minimizing dependencies and enabling better optimization.
๐ฒ Get Started with RandBox
Choose the import method suitable for your project and enjoy powerful random data generation.