Configs2026年7月22日·1 分钟阅读

Chance.js — Random Data Generator for JavaScript

Lightweight utility library for generating random strings, numbers, names, addresses, dates and other test data in JavaScript and Node.js.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Script
安装
Stage only
信任
信任等级:Established
入口
Chance.js
安全暂存命令
npx -y tokrepo@latest install f313cf9b-8565-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Chance.js is a minimalist random data generator for JavaScript. It produces realistic random values for names, addresses, phone numbers, dates, strings, and many other data types, making it useful for testing, prototyping, and seeding development databases.

What Chance.js Does

  • Generates random personal data: names, emails, phone numbers, addresses, and SSNs
  • Produces random primitives: integers, floats, booleans, strings, and characters
  • Creates random dates, timestamps, and time-related values
  • Provides domain-specific generators for colors, coordinates, IP addresses, and credit cards
  • Supports seeded randomness for reproducible test data across runs

Architecture Overview

Chance.js is a single-file library with no external dependencies. It uses a Mersenne Twister pseudorandom number generator internally, which provides high-quality distribution and supports seeding. All generator methods are attached to a Chance instance, and each instance can be independently seeded. The library is designed for both browser and Node.js environments.

Self-Hosting & Configuration

  • Install via npm or include directly via a script tag in the browser
  • Create a Chance instance with an optional seed for reproducible output
  • Call generator methods with optional configuration objects to constrain output
  • Use chance.mixin() to register custom generator functions
  • Supports UMD, CommonJS, and ES module import styles

Key Features

  • Seeded random number generator for deterministic, reproducible test data
  • Wide variety of built-in generators covering personal data, finance, geography, and web
  • Zero dependencies with a small footprint suitable for browser and server
  • Mixin system for extending with custom data generators
  • Configurable output ranges and formats for most generator methods

Comparison with Similar Tools

  • Faker.js — larger library with locale support and more data types; Chance.js is lighter with built-in seeding
  • casual — similar scope but less actively maintained; Chance.js has broader generator coverage
  • falso — modern Faker alternative with tree-shaking; Chance.js offers a simpler single-instance API
  • randexp — generates strings matching a regex; Chance.js provides structured data types, not pattern-based
  • Fishery — factory-based test data with associations; Chance.js focuses on individual random values

FAQ

Q: How do I generate the same random data across test runs? A: Pass a seed to the constructor: new Chance(42). The same seed always produces the same sequence of values.

Q: Can I use Chance.js in the browser? A: Yes. Include it via a script tag or import it with a bundler. It has no Node.js-specific dependencies.

Q: How do I add custom generators? A: Use chance.mixin({ myGenerator: function() { return ...; } }) to add new methods to the instance.

Q: Does Chance.js support internationalized data? A: Chance.js focuses on English-language data. For localized names and addresses, consider Faker.js which has extensive locale support.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产