randexp.js

Generates random strings that match any given JavaScript regular expression.

Library
npm
v0.5.3
1,866stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
44/100Fair
Development Activity0
Maintenance20
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
69/100Good
Architecture75
Code Quality65
Innovation70
Learning Curve65

randexp.js takes a JavaScript RegExp object (or a pattern string plus flags) and produces a random string guaranteed to match it. Internally it parses the pattern into an abstract syntax tree with the ret library, then walks that tree to synthesize output character by character, correctly handling groups, alternation, quantifiers, negated and unioned character classes, and backreferences to earlier groups.

Because the generated strings are guaranteed matches rather than hand-picked examples, it is widely used to produce test fixtures and fuzz inputs directly from validation regexes, and it underlies string generation in tools like JSON Schema Faker. The API is intentionally tiny — construct a RandExp, call .gen() — with a few mutable properties (max, defaultRange, randInt) exposed for callers who need to tune repetition limits, character ranges, or plug in a seeded/cryptographic PRNG.

What You Get

  • A RandExp class that generates a random matching string via new RandExp(pattern).gen(), accepting either a RegExp object or a pattern string with flags
  • A static RandExp.randexp() shorthand that caches a generator instance per regex object for repeated one-off calls
  • RandExp.sugar() to patch RegExp.prototype.gen() for a terser call style
  • Overridable randInt, max, and defaultRange properties to control randomness source, infinite-repetition bounds, and the character set used for wildcards/negated classes
  • Bundled TypeScript declarations (types/index.d.ts) for editor completion and type checking

Common Use Cases

  • Generating test fixtures directly from the same validation regex used in production code, so fixtures stay in sync with real validation rules
  • Populating pattern-constrained string properties when fuzzing JSON Schema samples (the basis for json-schema-faker’s pattern support)
  • Feeding randomized-but-valid inputs into a function under test as a lightweight form of property-based testing
  • Generating bulk placeholder/seed data for a database or demo environment from a handful of regex patterns instead of a full fake-data library

Under The Hood

Architecture The entire implementation lives in one class, RandExp (lib/randexp.js), that delegates regex parsing to the ret package — which turns a pattern into a token tree of ROOT/GROUP/SET/REPETITION/REFERENCE/CHAR/POSITION nodes — and then walks that tree recursively in _gen(), accumulating output and a groups array so backreferences (\1) can resolve to previously generated group text. Character-class tokens are expanded through _expand() into a DRange (a discontinuous numeric range from the companion drange package) that supports set union, intersection, and negation, letting defaultRange act as the universe subtracted from or intersected with for negated/wildcard matches. Per-instance overrides (max, defaultRange, randInt) are reconciled in _setDefaults(), which lets both prototype-level and instance-level configuration coexist. It is a tight, single-responsibility module with parsing and range math fully delegated to sibling libraries.

Tech Stack Plain ES6 class syntax over CommonJS, with exactly two runtime dependencies: ret (regex-to-token-tree parser) and drange (discontinuous range arithmetic), both maintained by the same author’s ecosystem. The dev/build toolchain is dated relative to current norms — Gulp plus Browserify/Uglify for producing a browser bundle, mocha/istanbul for tests, and dtslint for validating the hand-written .d.ts file — with no bundler-less ESM build and a package.json engines field pinned to Node >=8.

Code Quality Tests run through mocha against a large table-driven fixture file (test/tests.js) that pairs regexes with descriptions and an optional “bad” flag for patterns expected not to match, plus focused suites for custom max limits, custom PRNGs, custom character ranges, string+flags construction, and the sugar syntax. CI (.github/workflows/nodejs.yml) runs the suite across a Node 8/10/12 matrix with Codecov reporting. There is no linter configuration in the repo, no TypeScript source (only hand-maintained public .d.ts declarations validated via dtslint), and error handling is limited to a single explicit throw for invalid constructor input — otherwise the module trusts its inputs.

API Design The public surface is deliberately minimal: construct a RandExp from a RegExp or a pattern string, call .gen(), done. Tuning knobs (max, defaultRange, randInt) are exposed as plain mutable properties rather than a constructor options object, which keeps the API easy to start with with almost no boilerplate but makes the available knobs less discoverable without reading the README. The static randexp() helper and sugar() patch both offer shorter call styles for one-off use, and bundled type definitions give editor autocomplete for the small surface area.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search